Skip to main content
The fal Agent SDK connects your JavaScript or TypeScript application to fal Agent. Describe a task in natural language to generate images, video, audio, or 3D media through the fal model catalog. The agent chooses models, prepares prompts, and executes the task. Use the Agent SDK when your application needs the agent to coordinate a task across multiple steps. It supports conversations, editable plans, user approvals, and generated media. For a direct call to a specific model, use the fal model clients instead.
The Agent SDK is in alpha. These examples use @fal-ai/client@1.11.0-alpha.4. Media generation can incur charges. See Access and pricing.

Install the SDK

The Agent SDK is part of the @fal-ai/client npm package. It supports JavaScript and includes TypeScript types for requests, responses, and resources. Use Node.js 22.22 or later for these examples. See client configuration for runtime requirements.

Python support

The Agent SDK currently supports JavaScript and TypeScript. The fal Python client supports direct model calls. It does not provide the Agent SDK methods for conversations, plans, or approvals.

Authentication

Create a fal API key with the AGENT permission preset. Set the key in your environment:
Replace YOUR_API_KEY with your key. The SDK reads FAL_KEY when you omit credentials. Keep the API key on your server. For a browser application, use a server endpoint that enforces your users’ permissions. See browser applications.

Generate media

Use agent.run() to submit a task and wait for its result. The agent creates a response that contains the task status, assistant text, generated media, and any questions or approvals. An artifact identifies generated media and can contain files with download URLs. Create agent.mts:
Install tsx to execute the TypeScript example:
run() returns when execution stops or needs user input. Check the response before you use its media: Execution failures appear in response.status and response.error. Request failures throw AgentRequestError. See errors and troubleshooting for failure and recovery details. See media and costs to reuse media or select final results.

SDK methods

Use these methods to submit tasks and control their execution: See the method reference for all methods and the type reference for their types.

Progress and user input

Stream progress

Use agent.stream() to start a task with progress updates:
This example creates a new task. Use responses.stream(responseId) to observe a task that already exists. Each update contains the complete response state, called a snapshot. Replace the displayed state on each update. Do not append each snapshot’s output_text to the previous text.

Questions and approvals

The agent can pause to ask a question or request approval. When pending_inputs contains a request, show it to the user. Collect the user’s answer. Submit the answer with responses.answer() and the same response ID. Then resume observation with responses.wait() or responses.stream(). See questions and approvals for answer formats and examples.

Recovery and cancellation

Save the response ID to reconnect after a process restart, timeout, or lost connection. Use responses.wait() or responses.stream() with that ID to observe the existing task. For an application that stores task state, submit the task with responses.create(). Save the response ID. Then call responses.wait() or responses.stream(). See responses and recovery for submission retries and reconnection options.
A timeout, an aborted signal, or a closed stream does not cancel server execution. Use responses.cancel() to request cancellation of the task.

Next steps