Skip to main content
This example demonstrates how to build a production-ready text-to-image API using the fal SDK with the Sana diffusion models. We’ll create two endpoints: a standard endpoint and a faster “sprint” endpoint that shares resources efficiently.

🚀 Try this Example

View the complete source code on GitHub. Steps to run:
  1. Install fal:
  1. Authenticate (if not already done):
  1. Copy the code below into sana.py
  1. Run the app:
Or clone this repository:
Before you run, make sure you have:
  • Authenticated with fal: fal auth login
  • Activated your virtual environment (recommended): python -m venv venv && source venv/bin/activate (macOS/Linux) or venv\Scripts\activate (Windows)

Key Features

  • Multiple endpoints with shared model components
  • Input validation using Pydantic models
  • Safety checking for generated content
  • Flexible image generation with customizable parameters

Project Structure

Input Models

Define your input schemas with clear documentation and examples:

Output Models

Create output models with example data for the playground:

Main Application Class

Shared Generation Logic

Create a reusable generation method for both endpoints:

Endpoint Definitions

Define multiple endpoints using the shared generation logic:

Running the Application

Development

Using pyproject.toml

Add to your pyproject.toml:
Then run:

Testing Your Endpoints

After fal deploy, open the app in the Apps dashboard. Follow Testing Your Own Apps to test the base or sprint endpoint. For fal run, use the Playground URL printed for that temporary run.

Best Practices Demonstrated

  1. Resource Sharing: The text encoder is shared between pipelines to save memory
  2. Input Validation: Comprehensive Pydantic models with examples and constraints
  3. Error Handling: Safety checking and proper response formatting
  4. Billing Integration: Resolution-based pricing
  5. Endpoint Flexibility: Different configurations for different use cases
  6. Documentation: Rich field descriptions and examples for auto-generated docs

Key Takeaways

  • Use setup() to load models once and cache them
  • Share components between models when possible to optimize memory
  • Create endpoint-specific input models for different use cases
  • Implement proper billing with x-fal-billable-units
  • Use the fal image toolkit for safety checking and processing
  • Pin all dependency versions for reliability
This pattern works well for any multi-model or multi-endpoint image generation service where you want to optimize for both performance and cost efficiency. You can visit fal_demos to view the full file and other examples.