fal_webhook query parameter.
To utilize webhooks, your requests should be directed to the queue.fal.run endpoint, instead of the standard fal.run. This distinction is crucial for enabling webhook functionality, as it ensures your request is handled by the queue system designed to support asynchronous operations and notifications.
request_id and gateway_request_id:
gateway_request_id will have the value of the last tried request, while
request_id will be the value used in the queue API.
Once the request is done processing in the queue, a POST request is made to the webhook URL, passing the request info and the resulting payload. The status indicates whether the request was successful or not.
When to use it?Webhooks are particularly useful for requests that can take a while to process and/or the result is not needed immediately. For example, if you are training a model, which is a process than can take several minutes or even hours, webhooks could be the perfect tool for the job.
Successful result
The following is an example of a successful request:Response errors
When an error happens, thestatus will be ERROR. The error property will contain a message and the payload will provide the error details. For example, if you forget to pass the required model_name parameter, you will get the following response:
Payload errors
For the webhook to include the payload, it must be valid JSON. So if there is an error serializing it,payload is set to null and a payload_error will include details about the error.
Retry policy
If the webhook fails to deliver the payload, it will retry 10 times in the span of 2 hours.Verifying Your Webhook
To ensure the security and integrity of incoming webhook requests, you must verify that they originate from the expected source. This involves validating a cryptographic signature included in the request using a set of public keys. Below is a step-by-step guide to the verification process, followed by example implementations in Python and JavaScript.Verification Process
1
Fetch the JSON Web Key Set (JWKS)
- Retrieve the public keys from the JWKS endpoint:
https://rest.alpha.fal.ai/.well-known/jwks.json. - The JWKS contains a list of public keys in JSON format, each with an
xfield holding a base64url-encoded ED25519 public key. - Note: The JWKS is cacheable to reduce network requests. Ensure your implementation caches the keys and refreshes them after the cache duration expires. Do not cache longer than 24 hours since they can change.
2
Extract Required Headers
- Obtain the following headers from the incoming webhook request:
X-Fal-Webhook-Request-Id: The unique request ID.X-Fal-Webhook-User-Id: Your user ID.X-Fal-Webhook-Timestamp: The timestamp when the request was generated (in Unix epoch seconds).X-Fal-Webhook-Signature: The cryptographic signature in hexadecimal format.
- If any header is missing, the request is invalid.
3
Verify the Timestamp
- Compare the
X-Fal-Webhook-Timestampwith the current Unix timestamp. - Allow a leeway of ±5 minutes (300 seconds) to account for clock skew and network delays.
- If the timestamp differs by more than 300 seconds, reject the request to prevent replay attacks.
4
Construct the Message
- Compute the SHA-256 hash of the request body (raw bytes, not JSON-parsed).
- Concatenate the following in strict order, separated by newline characters (
\n):X-Fal-Webhook-Request-IdX-Fal-Webhook-User-IdX-Fal-Webhook-Timestamp- Hex-encoded SHA-256 hash of the request body
- Encode the resulting string as UTF-8 bytes to form the message to verify.
5
Verify the Signature
- Decode the
X-Fal-Webhook-Signaturefrom hexadecimal to bytes. - For each public key in the JWKS:
- Decode the
xfield from base64url to bytes. - Use an ED25519 verification function (e.g., from PyNaCl in Python or libsodium in JavaScript) to verify the signature against the constructed message.
- Decode the
- If any key successfully verifies the signature, the request is valid.
- If no key verifies the signature, the request is invalid.
Example Implementations
Below are simplified functions to verify webhook signatures by passing the header values and request body directly. These examples handle the verification process as described above and include JWKS caching.- python
- javascript
Install dependencies:Verification function:
Usage Notes
- Caching the JWKS: The JWKS can be cached for 24 hours to minimize network requests. The example implementations include basic in-memory caching.
- Timestamp Validation: The ±5-minute leeway ensures robustness against minor clock differences. Adjust this value if your use case requires stricter or looser validation.
- Error Handling: The examples include comprehensive error handling for missing headers, invalid signatures, and network issues. Log errors appropriately for debugging.
- Framework Integration: For frameworks like FastAPI (Python) or Express (JavaScript), ensure the raw request body is accessible. For Express, use
express.raw({ type: 'application/json' })middleware before JSON parsing.