fashn-logo
FASHNAI

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:

Server-side usage

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:

npm install fashn

Generate an API Key

Go to the Developer API dashboard and click Create new API key.

Store your key securely 🔒

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.

import Fashn from "fashn";
 
const client = new Fashn({
  apiKey: process.env.FASHN_API_KEY, // This is the default and can be omitted
});
 
async function runGeneration() {
  const response = await client.predictions.subscribe({
    model_name: "tryon-v1.6",
    inputs: {
      model_image: "https://example.com/path/to/model.jpg",
      garment_image: "https://example.com/path/to/garment.jpg",
    },
  });
 
  console.log("Generation completed!");
  console.log("Result:", response);
}
 
// Run the generation
runGeneration();

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.

import Fashn from "fashn";
 
const client = new Fashn({
  apiKey: process.env.FASHN_API_KEY,
});
 
async function runGeneration() {
  try {
    const response = await client.predictions.subscribe({
      model_name: "tryon-v1.6",
      inputs: {
        model_image: "https://example.com/model.jpg",
        garment_image: "https://example.com/garment.jpg",
      },
    });
 
    // 1. Check for Runtime Errors (during model execution). Status can be failed, canceled or time_out.
    if (response.status !== "completed") {
      console.error("Runtime Error - Status:", response.status);
      console.error("Error message:", response.error?.message);
      return;
    }
 
    // 2. Success case (status is completed)
    console.log("Generation completed successfully!");
    console.log("Generated:", response.output);
  } catch (error) {
    // 3. Handle API-Level Errors (before request processing)
    if (error instanceof Fashn.APIError) {
      console.error("API-Level Error - Status:", error.status);
      console.error("Message:", error.message);
    } else {
      console.error("Network or unexpected error:", error);
    }
  }
}
 
runGeneration();

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 and error.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 and response.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:

  1. Submit a request
  2. Poll for the status of the request until it is completed
Note

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.

import Fashn from "fashn";
 
const client = new Fashn({
  apiKey: process.env.FASHN_API_KEY,
});
 
const response = await client.predictions.run({
  model_name: "tryon-v1.6",
  inputs: {
    model_image: "https://example.com/model.jpg",
    garment_image: "https://example.com/garment.jpg",
  },
});
 
console.log("Prediction ID:", response.id);

Fetch request status

Use the prediction ID returned from the run method to fetch the status of the request.

import Fashn from "fashn";
 
const client = new Fashn({
  apiKey: process.env.FASHN_API_KEY,
});
 
const response = await client.predictions.status(
  "9dafef71-6e90-4bc9-ac05-d0d97c612722" // Prediction ID
);
 
console.log("Prediction Status:", response.status);

On this page