Run a FastAPI app

Getting an application running on Fly.io is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Fly.io platform.

In this guide we’ll learn how to deploy a FastAPI application on Fly.io.

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints.

Deploying a FastAPI app on Fly.io is… well it’s fast! You can be up and running in less than a minute.

Speedrun


First, install flyctl, the Fly.io CLI, and sign up to Fly.io if you haven’t already.

The fastest way to get a basic Fast Api server on Fly.io is to use our FastAPI template:

git clone git@github.com:fly-apps/hello-fastapi.git fastapi-app
cd fastapi-app
fly launch --generate-name

Deploy a FastAPI app from scratch


For managing our project, we use Poetry. For more information on the initial setup with poetry, refer to setting up a python environment. We can initialize a new project like so:

poetry new fastapi-app
cd fastapi-app

Then we have to add the FastAPI dependency:

poetry add fastapi

Now, let’s create a simple FastAPI app in main.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def hello_fly():
    return 'hello from fly.io'

We can then serve the development version of the app using the fastapi cli tool:

fastapi dev

This will display a ‘hello from fly.io!’ message when you visit the root URL. With the development environment we also get cool features like hot-reloading.

To run in production mode, we can amend the command:

fastapi run

The fastapi assumes your app is stored in main.py or app.py - if this is not the case, you must specify that when running the app:

`fastapi run hello/entrypoint.py

And with that you can deploy the app!

fly launch
Scanning source code
Detected a FastAPI app
Warning: This organization has no payment method, turning off high availability
Creating app in [redacted]/[app-name]
We're about to launch your app on Fly.io. Here's what you're getting:

Organization: Your Name              (fly launch defaults to the personal org)
Name:         [app-name]             (derived from your directory name)
Region:       Amsterdam, Netherlands (this is the fastest region for you)
App Machines: shared-cpu-1x, 1GB RAM (most apps need about 1GB of RAM)
Postgres:     <none>                 (not requested)
Redis:        <none>                 (not requested)
Sentry:       false                  (not requested)

...

==> Building image
...
==> Building image with Docker
...

Watch your deployment at https://fly.io/apps/[app-name]/monitoring
...

Visit your newly deployed app at https://[app-name].fly.dev/

This will generate a fly.toml file with the configuration for your app and a Dockerfile that uses multi-stage builds. Refer to the fly.toml docs for more configuration options.

To deploy a new version of your app, simply run fly deploy in the project directory.

You can check out the full (yet minimal) example in this GitHub repository for a reference.