Workflow as an API
Workflow APIs work the same way as other model endpoints, you can simply send a request and get a response back. However, it is common for workflows to contain multiple steps and produce intermediate results, as each step contains their own response that could be relevant in your use-case. Therefore, workflows benefit from the streaming feature, which allows you to get partial results as they are being generated. If you submit a workflow to the queue and get the result with a webhook, you do not get the streaming events. To get the endpoint and the request ID of each step in the result, see Step request IDs.Workflow Events
The workflow API will trigger a few events during its execution, these events can be used to monitor the progress of the workflow and get intermediate results. Below are the events that you can expect from a workflow stream:The submit Event
This event is triggered every time a new step has been submitted to execution. It contains the app_id, request_id and the node_id.
The completion Event
This event is triggered upon the completion of a specific step.
The output Event
The output event means that the workflow has completed and the final result is ready.
The error Event
The error event is triggered when an error occurs during the execution of a step. The error object contains the error.status with the HTTP status code, an error message as well as error.body with the underlying error serialized.
The recovery Event
The workflow sends this event when a step fails and the workflow starts the fallback of that step.
reasonis the failure type that started the fallback:server_error,policy, orforced.outcomeissubmittedwhen the fallback started. It issubmit_failedwhen fal could not start the fallback.- After a
submittedrecovery event, asubmitevent gives the request ID of the fallback.
The warning Event
The workflow sends this event when the configuration of a step has a problem, but the workflow can continue. For example, fal ignores a fallback configuration that it cannot use. The step then runs without a fallback.
Fallbacks
A step can have one fallback. If the step fails, fal runs the fallback endpoint for that step. The workflow then continues with the output of the fallback. Add the fallback to theconfig of the node:
The failure types are:
policy: the endpoint returned HTTP 422.server_error: the endpoint returned a different 4xx or 5xx status.
video, so a next step can use $node-k3TqP9xL2a.video.url for both.
Fallback rules:
- fal tries the fallback one time. If the fallback also fails, the workflow stops with an
errorevent. - fal does not start the fallback when the input of the step is not valid. For example, a
$reference that does not resolve stops the workflow. - If
fallbackshas more than one step, fal ignores the configuration and sends awarningevent. The same occurs when the fallback step is not valid. - When a step has a fallback, fal disables the automatic model fallbacks of the endpoint for that step.
Test the Fallbacks
To test the fallbacks of a workflow, addfal_fallbacks=1 to the request URL. fal then runs the fallback of each step that has one, instead of the endpoint of the step. The reason of the recovery event is forced.
Step Request IDs
When you submit a workflow to the queue, the result and the webhook payload contain only the final output. To get the endpoint and the request ID of each step, sendX-Fal-Workflow-Envelope: true with the request.
Use the request IDs to get the cost of each step from billing events. You can also find each step in your request history.
output is the result that you get without the header. nodes has one entry for each step that fal submitted. The keys of nodes are the node IDs of your workflow.
To get the total cost of a run:
- Collect all request IDs in
nodes. Include eachfailed_request_id. - Get the billing events with the
request_idfilter. One call accepts up to 50 request IDs. - Set
startandendto cover the time of the run. If you do not setstart, you get only the last 24 hours. - Add the cost of the billing events.
When a Step Fails
If the workflow stops with an error, the error body also containsnodes. The HTTP status code does not change. The steps that were still running when the workflow stopped are also in nodes, because they continue to run.
Limits
- Without the header, the result does not change.
- The header does not change the streaming events. In a stream, use the
submitandrecoveryevents. nodesshows only the fallback that you configured. It does not show the retries that fal does on the platform.- If the workflow stops without a result, for example after a timeout, you do not get
nodes.
Example
A cool and simple example of the power of workflows isworkflows/fal-ai/sdxl-sticker, which consists of three steps:
1
Generates an image using
fal-ai/fast-sdxl.2
Remove the background of the image using
fal-ai/imageutils/rembg.3
Converts the image to a sticker using
fal-ai/face-to-sticker.- Javascript
- python
- python (async)
- Swift
Type Definitions
Below are the type definition in TypeScript of events that you can expect from a workflow stream:X-Fal-Workflow-Envelope: true header: