MCPcopy Index your code
hub / github.com/simstudioai/sim / estimateFalAICallCost

Function estimateFalAICallCost

apps/sim/lib/tools/falai-pricing.ts:121–158  ·  view source on GitHub ↗
(
  apiKey: string,
  endpointId: string
)

Source from the content-addressed store, hash-verified

119}
120
121async function estimateFalAICallCost(
122 apiKey: string,
123 endpointId: string
124): Promise<{ costDollars?: number; error?: string }> {
125 let response: Response
126 try {
127 response = await fetch('https://api.fal.ai/v1/models/pricing/estimate', {
128 method: 'POST',
129 headers: {
130 Authorization: `Key ${apiKey}`,
131 'Content-Type': 'application/json',
132 },
133 body: JSON.stringify({
134 estimate_type: 'historical_api_price',
135 endpoints: {
136 [endpointId]: {
137 call_quantity: 1,
138 },
139 },
140 }),
141 })
142 } catch (error) {
143 return { error: getErrorMessage(error, 'Unknown error') }
144 }
145
146 if (!response.ok) {
147 const error = await response.text().catch(() => '')
148 return { error: `Fal.ai pricing estimate failed: ${response.status} ${error}` }
149 }
150
151 const data = (await response.json()) as unknown
152 const totalCost = isRecordLike(data) ? getNumber(data.total_cost) : undefined
153 if (totalCost === undefined) {
154 return { error: 'Fal.ai pricing estimate missing total_cost' }
155 }
156
157 return { costDollars: totalCost }
158}
159
160export async function getFalAICostMetadata({
161 apiKey,

Callers 1

getFalAICostMetadataFunction · 0.85

Calls 4

getErrorMessageFunction · 0.90
isRecordLikeFunction · 0.90
textMethod · 0.80
getNumberFunction · 0.70

Tested by

no test coverage detected