Skip to content

Python

Deploy a FastAPI app on Enzonic

Deploy finds your FastAPI app object (app = FastAPI(...)) in main.py, app.py, server.py, api/index.py, src/main.py, src/app.py, wsgi.py or asgi.py, installs requirements.txt or pyproject.toml, and serves it as an ASGI function on Python 3.12. There is no server process to manage.

At a glance

What Enzonic Deploy does with a FastAPI project when you connect it. You can override any of it in the project settings.

Language
Python
Detected from
fastapi or starlette in requirements.txt, pyproject.toml or Pipfile; An assignment like app = FastAPI( in one of the entry files
Build command
pip install -r requirements.txt (or pip install .)
Output
Functions
Runs as
Functions

How it runs

  • Functions

    Always: the ASGI app runs in function instances that scale with traffic and to zero.

Deploy FastAPI step by step

  1. 1List dependencies in requirements.txt (or pyproject.toml).
  2. 2Keep the app object at module level in main.py (or one of the other entry files).
  3. 3Connect the repository and deploy.
main.py
from fastapi import FastAPI

app = FastAPI()

@app.get('/')
def read_root():
    return {'hello': 'world'}

Connect a database to FastAPI

Create a database on Enzonic Databases (or from the project's settings) and its connection string is added to the project's environment variables. This example uses PostgreSQL.

db.py
import os
from sqlalchemy import create_engine, text

# postgresql+psycopg:// selects psycopg 3; the URL comes from Enzonic Databases.
url = os.environ['DATABASE_URL'].replace('postgresql://', 'postgresql+psycopg://', 1)
engine = create_engine(url, pool_size=2, pool_pre_ping=True)

with engine.connect() as conn:
    print(conn.execute(text('select now()')).scalar())

Postgres projects get two variables: DATABASE_URL through the connection pooler for the app, and DATABASE_URL_UNPOOLED for migrations.

PostgreSQL on Enzonic DatabasesEvery database engine

Things to know

  • The app object must be named in an assignment Deploy can see (app = FastAPI(...)); an app created inside a factory function is not found.
  • Startup and shutdown events run per function instance, not once per deployment.

Plan limits that matter here

From the live plan data. Every plan includes previews for each branch, instant rollback, environment variables and logs.

Plan limits that matter for FastAPI
LimitFreePro
Build minutes3,000/mo, 1 concurrent10,000/mo, 3 concurrent
Function memory / timeout1 GB / 300 s4 GB / 1800 s
Function invocations2 M/mo20 M/mo
Bandwidth150 GB/mo2 TB/mo

Compare every plan

Related guides

Ship your FastAPI project tonight

Connect the repository and the build settings fill themselves in. Free plan, no card.

Start free