The fal client
The client libraries offer convenient ways to interact with fal functions. Now that you know how to run functions, let's explore the client capabilities and how it can enable you to get the job done.
Installation
The fal client is available through the standard package manager for each supported language:
npm install --save @fal-ai/serverless-client
Initialize the client
The client assumes by default that you have a FAL_KEY
, or a combination of FAL_KEY_ID
and FAL_KEY_SECRET
, environment variables so API authorization is handled automatically.
If you want to manually provide credentials, the JS client allows for manually set credentials so you can handle it yourself in your app code.
fal.config({
credentials: "FAL_KEY_ID:FAL_KEY_SECRET", // or a function that returns a string
});
Protected your API keys
Although the client works in a browser environment, it is not recommended to keep the credentials in the browser. Instead, you should use a server-side proxy to handle the credentials. Check out our server-side guide for guidance.
Subscribe to queue updates
The client offers a way for you to subscribe to queue updates. This is useful if you want to get notified when a function is done running, or if you want to get the logs as they are being generated.
import * as fal from "@fal-ai/serverless-client";
const result = await fal.queue.subscribe(FUNCTION_ID, {
input: {
seed: 176400,
},
pollInterval: 5000,
onQueueUpdate: (update) => {
console.log(update.status);
if (update.status === "IN_PROGRESS") {
update.logs.map((log) => log.message).forEach(console.log);
}
},
});
console.log(result.url);
The onQueueUpdate
callback will be called every time the queue status changes. The update
object contains the queue status data as documented on the status types section.
Built-in type definitions
The client contains all type definitions built-in, so regardless if you use TypeScript or JavaScript, you will get type hints and autocompletion.
Run functions
The client offers a way for you to run functions. This is useful if you want to run a function that execute fast and wait for the result.
import * as fal from "@fal-ai/serverless-client";
const result = await fal.run(FUNCTION_ID);
console.log(result);
Prefer queue over run for long requests!
The run
method is a convenience for short running functions, it will simply run the function and wait for the result. For long running functions, such as ML flows, it is recommended to use the queue instead.