Deploy a FastAPI app from GitHub, step by step

· 5 min read

A FastAPI app is simpler to host than a Django one, because there is no admin, no migrations command and no static file collection to get wrong. It still trips up first time deployers in a few predictable places. This guide walks through a deploy on Pharos, and the code advice applies to any container host.

What the platform needs from your repository

Put the project in a GitHub repository with a requirements.txt that lists fastapi and whatever else you import, plus a file with a line like app = FastAPI(). The platform looks in main.py, app.py, app/main.py, src/main.py, api.py and server.py, and then in any Python file at the top of the repository. If it cannot find the app object, it stops with a message that says so.

You do not need a Dockerfile. If there is none, the platform writes one for a Python 3.12 image, installs requirements.txt plus uvicorn, and starts the app with uvicorn. If you add your own Dockerfile, yours is used.

Here is a complete small app that works as is:

import os
import sqlite3
from contextlib import closing

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

DATABASE_PATH = os.environ.get("DATABASE_PATH", "/data/db.sqlite3")

app = FastAPI()


def count_visit() -> int:
    with closing(sqlite3.connect(DATABASE_PATH)) as connection:
        connection.execute("CREATE TABLE IF NOT EXISTS visits (name TEXT PRIMARY KEY, count INTEGER NOT NULL)")
        connection.execute("INSERT INTO visits (name, count) VALUES ('home', 1) ON CONFLICT(name) DO UPDATE SET count = count + 1")
        connection.commit()
        return connection.execute("SELECT count FROM visits WHERE name = 'home'").fetchone()[0]


@app.get("/")
def home():
    return {"visits": count_visit()}


@app.get("/healthz", response_class=PlainTextResponse)
def healthz():
    return "ok"

Its requirements.txt is two lines: fastapi and uvicorn.

Four things to get right

  • Listen on port 8000. The port is fixed. The generated start command binds to 0.0.0.0 and uses the PORT variable the platform sets, falling back to 8000. If you write your own Dockerfile, do the same and never bind to 127.0.0.1, because nothing outside the container can reach that.
  • Keep your data in /data. Each app has a persistent /data folder, and the DATABASE_PATH variable points to a file in it. Everything else is thrown away on the next deploy, including files your app writes next to its code. SQLite is included. For an app people depend on, the paid Postgres add-on gives your app its own database and we set DATABASE_URL for it. The generated image uses Python 3.12; for another version, add your own Dockerfile.
  • Secrets go in the Environment tab. API keys, tokens and passwords belong there, not in the repository. Values are stored encrypted and never shown again after you save them, so keep your own copy.
  • Trust the forwarded address carefully. HTTPS is handled before your app. If your code builds absolute links, read the public address from an environment variable such as SITE_URL instead of from the request, because the request carries the internal address.

Create the app

Choose New app in the dashboard. Give it a name and paste the repository address, for example https://github.com/you/yourapp. Leave the branch and the framework on their defaults, which detects FastAPI for you. If you connect GitHub you can choose from your own repositories, including private ones. Pick the framework by hand only if detection gets it wrong. If you pick the wrong one, the platform says what it found instead.

Deploy

The Automatic deploys tab gives you a short workflow file for your repository, and each push to the chosen branch builds on GitHub and goes to the platform. To deploy right away, open the Actions tab in your repository, pick the Pharos deploy workflow and click Run workflow. You can choose between going live by itself and waiting for your approval.

The Deploys tab shows each stage and its output. After the new version starts, the platform checks that it answers before it switches over. If it does not, the old version keeps running and the tab says what failed. The App logs tab shows the traceback.

When it works, open the address shown at the top of the app page. The Overview tab starts to fill in with CPU, memory and request graphs, and the Request logs tab lists the requests your app has served.

Close the API docs

FastAPI serves interactive documentation at /docs and /redoc, and the full schema at /openapi.json. On a public site that is a free map of every route you have. For a private tool or a hobby app, turn them off in production:

app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)

If you want them on your laptop only, read a DEBUG variable and set the three options from it. The FastAPI SEO guide has a version of that. The scan that runs on your live site looks for these paths and tells you if they answer.

When it does not work

  • The platform could not find a FastAPI app. The FastAPI() line is in a file that is not one of the usual names, or it is inside a function. Move it to main.py at the top of the repository.
  • The app started but did not answer. Check the App logs for an import error, a missing package in requirements.txt, or a server bound to the wrong address.
  • Data gone after a deploy. The database file was outside /data.
  • A package fails to build. Pin the version in requirements.txt, and check it ships a wheel for Python 3.12.

What it costs

You get a 14-day trial, a card is required to start, and you pay nothing if you cancel before day 14. After that it is A$27 a month for one app. Before you share the address, read the pre-launch checklist.