Webhooks
For long-running requests, such as training jobs, you can use webhooks to receive the result asynchronously. You can specify the webhook URL when submitting a request.
The client for JavaScript / TypeScript provides a seamless interface to interact with fal.
First, add the client as a dependency in your project:
npm install --save @fal-ai/serverless-client
Endpoints requests are managed by a queue system. This allows fal to provide a reliable and scalable service.
The subscribe
method allows you to submit a request to the queue and wait for the result.
import * as fal from "@fal-ai/serverless-client";
const result = await fal.subscribe("fal-ai/flux/dev", {
input: {
prompt: "a cat",
seed: 6252023,
image_size: "landscape_4_3",
num_images: 4,
},
logs: true,
onQueueUpdate: (update) => {
if (update.status === "IN_PROGRESS") {
update.logs.map((log) => log.message).forEach(console.log);
}
},
});
You can manage the queue using the following methods:
Submit a request to the queue using the queue.submit
method.
import * as fal from "@fal-ai/serverless-client";
const { request_id } = await fal.queue.submit("fal-ai/flux/dev", {
input: {
prompt: "a cat",
seed: 6252023,
image_size: "landscape_4_3",
num_images: 4,
},
webhookUrl: "https://optional.webhook.url/for/results",
});
This is useful when you want to submit a request to the queue and retrieve the result later. You can save the request_id
and use it to retrieve the result later.
For long-running requests, such as training jobs, you can use webhooks to receive the result asynchronously. You can specify the webhook URL when submitting a request.
Retrieve the status of a specific request in the queue:
import * as fal from "@fal-ai/serverless-client";
const status = await fal.queue.status("fal-ai/flux/dev", {
requestId: "764cabcf-b745-4b3e-ae38-1200304cf45b",
logs: true,
});
Get the result of a specific request from the queue:
import * as fal from "@fal-ai/serverless-client";
const result = await fal.queue.result("fal-ai/flux/dev", {
requestId: "764cabcf-b745-4b3e-ae38-1200304cf45b",
});
Some endpoints require files as input. However, since the endpoints run asynchronously, processed by the queue, you will need to provide URLs to the files instead of the actual file content.
Luckily, the client library provides a way to upload files to the server and get a URL to use in the request.
import * as fal from "@fal-ai/serverless-client";
const file = new File(["Hello, World!"], "hello.txt", { type: "text/plain" });
const url = await fal.storage.upload(file);
Some endpoints support streaming:
import * as fal from "@fal-ai/serverless-client";
const stream = await fal.stream("fal-ai/flux/dev", {
input: {
prompt: "a cat",
seed: 6252023,
image_size: "landscape_4_3",
num_images: 4,
},
});
for await (const event of stream) {
console.log(event);
}
const result = await stream.done();
For the endpoints that support real-time inference via WebSockets, you can use the realtime client that abstracts the WebSocket connection, re-connection, serialization, and provides a simple interface to interact with the endpoint:
import * as fal from "@fal-ai/serverless-client";
const connection = fal.realtime.connect("fal-ai/flux/dev", {
onResult: (result) => {
console.log(result);
},
onError: (error) => {
console.error(error);
},
});
connection.send({
prompt: "a cat",
seed: 6252023,
image_size: "landscape_4_3",
num_images: 4,
});
The endpoints can also be called directly instead of using the queue system.
We do not recommend this use most use cases as it will block the client until the response is received. Moreover, if the connection is closed before the response is received, the request will be lost.
import * as fal from "@fal-ai/serverless-client";
const result = await fal.run("fal-ai/flux/dev", {
input: {
prompt: "a cat",
seed: 6252023,
image_size: "landscape_4_3",
num_images: 4,
},
});
For a complete list of available methods and their parameters, please refer to JavaScript / TypeScript API Reference documentation (opens in a new tab).
Check out some of the examples below to see real-world use cases of the client library:
fal.realtime
in action with SDXL Lightning: https://github.com/fal-ai/sdxl-lightning-demo-app (opens in a new tab)If you encounter any issues or have questions, please visit the GitHub repository (opens in a new tab) or join our Discord Community (opens in a new tab).