TypeScript
This guide shows you how to get started with FASHN's API using our official TypeScript SDK. The SDK handles authentication, request/response parsing, error handling, and polling automatically.
For detailed documentation and how to use the FASHN API, please refer to:
You should only use this SDK on the server-side. Never expose your API key to the client-side.
Installation
First, install the FASHN SDK:
Generate an API Key
Go to the Developer API dashboard and click Create new API key.
You won't be able to view it again after closing the window.
Quick Start with the SDK
The SDK's subscribe
method handles the entire prediction lifecycle automatically - it submits your request, polls for completion, and returns the final result. You can use any model_name
from our API Reference section with their respective input parameters.
Error Handling
The FASHN API has two distinct types of errors that you need to handle in different places. For a complete list of all error types and status codes, see the API Fundamentals Error Handling section.
API-Level Errors (handled in catch
block):
- Occur before your request is accepted for processing
- Examples: invalid API key (401), rate limits (429), bad request format (400)
- Thrown as exceptions with
error.status
anderror.message
Runtime Errors (check response.status
):
- Occur during model execution after successful request submission
- Examples: image loading failures, content moderation, pose detection issues
- Available in
response.error.name
andresponse.error.message
Configuration
All configuration options are available in our open source GitHub repository: fashn-typescript-sdk. You can explore the source code, contribute, or check out advanced configuration options and examples.
Advanced Usage
The SDK provides methods to submit a request and fetch the status of a request separately. If you choose this workflow you would need to:
- Submit a request
- Poll for the status of the request until it is completed
The subscribe method described above already implements this workflow with good defaults and better error handling. We only recommend using this workflow if you have a specific use case that requires it.
Submit a request
Submit a request to the API and get a prediction ID.
Fetch request status
Use the prediction ID returned from the run method to fetch the status of the request.