Ready-to-use proxy implementations
We provide ready-to-use proxy implementations for the following languages/frameworks:- Node.js with Next.js: a Next.js API route handler that can be used in any Next.js app. It supports both Page and App routers. We use it ourselves in all of our apps in production.
- Node.js with Express: an Express route handler that can be used in any Express app. You can also implement custom logic and compose together with your own handlers.
The proxy formula
In case fal doesn’t provide a plug-and-play proxy implementation for your language/framework, you can use the following formula to implement your own proxy:- Provide a single endpoint that will ingest all requests from the client (e.g.
/api/fal/proxy
is commonly used as the default route path). - The endpoint must support both
GET
andPOST
requests. When an unsupported HTTP method is used, the proxy must return status code405
, Method Not Allowed. - The URL the proxy needs to call is provided by the
x-fal-target-url
header. If the header is missing, the proxy must return status code400
, Bad Request. In case it doesn’t point to a valid URL, or the URL’s domain is not*.fal.ai
or*.fal.run
, the proxy must return status code412
, Precondition Failed. - The request body, when present, is always in the JSON format - i.e.
content-type
header isapplication/json
. Any other type of content must be rejected with status code415
, Unsupported Media Type. - The proxy must add the
authorization
header in the format ofKey <your-api-key>
to the request it sends to the target URL. Your API key should be resolved from the environment variableFAL_KEY
. - The response from the target URL will always be in the JSON format, the proxy must return the same response to the client.
- The proxy must return the same HTTP status code as the target URL.
- The proxy must return the same headers as the target URL, except for the
content-length
andcontent-encoding
headers, which should be set by the your own server/framework automatically.
Use the power of LLMsThe formula above was written in a way that should be easy to follow, including by LLMs. Try using ChatGPT or Co-pilot with the formula above and your should get a good starting point for your own implementation. Let us know if you try that!
Configure the client
To use the proxy, you need to configure the client to use the proxy endpoint. You can do that by setting theproxyUrl
option in the client configuration: