fashn-logo
FASHNAI

Python

This guide shows you how to get started with FASHN's API using our official Python 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:

Installation

First, install the FASHN Python SDK:

pip 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.

Synchronous

import os
from fashn import Fashn
 
client = Fashn(
    api_key=os.environ.get("FASHN_API_KEY"), # This is the default and can be omitted
)
 
result = client.predictions.subscribe(
    model_name="tryon-v1.6",
    inputs={
        "garment_image": "https://example.com/garment.jpg",
        "model_image": "https://example.com/model.jpg",
    },
)
 
print("Generation completed!")
print("Result:", result)

Async

import os
import asyncio
from fashn import AsyncFashn
 
async def main():
    client = AsyncFashn(
        api_key=os.environ.get("FASHN_API_KEY"),  # This is the default and can be omitted
    )
 
    result = await client.predictions.subscribe(
        model_name="tryon-v1.6",
        inputs={
            "garment_image": "https://example.com/garment.jpg",
            "model_image": "https://example.com/model.jpg",
        },
    )
 
    print("Generation completed!")
    print("Result:", result)
 
asyncio.run(main())

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 os
import fashn
from fashn import Fashn
 
client = Fashn(
    api_key=os.environ.get("FASHN_API_KEY"),  # This is the default and can be omitted
)
 
try:
    result = 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 result.status != "completed":
        print("Runtime Error - Status:", result.status)
        if result.error:
            print("Error message:", result.error.message)
    else:
        # 2. Success case (status is completed)
        print("Generation completed successfully!")
        print("Generated:", result.output)
 
except fashn.APIError as e:
    # 3. Handle API-Level Errors (before request processing)
    print("API-Level Error - Status:", e.status_code)
    print("Message:", e.message)
except Exception as e:
    # Network or unexpected error
    print("Network or unexpected error:", e)

API-Level Errors (handled in except blocks):

  • Occur before your request is accepted for processing
  • Examples: invalid API key (401), rate limits (429), bad request format (400)
  • Thrown as exceptions with e.status_code and e.message

Runtime Errors (check result.status):

  • Occur during model execution after successful request submission
  • Examples: image loading failures, content moderation, pose detection issues
  • Available in result.error fields when present

Configuration

Advanced configuration options (retries, timeouts, proxies, custom HTTP client) are available in our open source GitHub repository: fashn-python-sdk.

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 os
from fashn import Fashn
 
client = Fashn(api_key=os.environ.get("FASHN_API_KEY"))
 
response = client.predictions.run(
    model_name="tryon-v1.6",
    inputs={
        "model_image": "https://example.com/model.jpg",
        "garment_image": "https://example.com/garment.jpg",
    },
)
 
print("Prediction ID:", response.id)

Fetch request status

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

import os
from fashn import Fashn
 
client = Fashn(api_key=os.environ.get("FASHN_API_KEY"))
 
status = client.predictions.status(
    "9dafef71-6e90-4bc9-ac05-d0d97c612722"  # Prediction ID
)
 
print("Prediction Status:", status.status)

On this page