How to Deploy FastApi to

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python language. It is a user-friendly, modular, and easy-to-extend framework that makes it easy to get started building APIs with minimal setup and configuration.

In this article, you will learn how to deploy a FastAPI application to, a cloud platform for hosting and managing modern web applications. offers a highly scalable, secure, and performant platform for hosting web applications, making it a great choice for deploying FastAPI applications.

Additionally, it offers a free plan, making it an ideal option for you who are in the early stages of building a prototype for their application.

We will go through the process of setting up a FastAPI application, configuring it for deployment to, and deploying the application to the platform. By the end of this article, you will have a fully functional FastAPI application running on

Create FastApi project

To create a FastAPI project, you will first need to make sure you have Python 3.7 or higher installed on your system. You can check your Python version by running the following command in a terminal:

python --version

Once you have Python installed, you can use the pip package manager to install FastAPI and its dependencies. Run the following command to install FastAPI:

pip install fastapi

With FastAPI installed, you can now create a new FastAPI project. This time we will creating the FastApi project from scratch.

To create a new FastAPI project from scratch, create a new directory for your project and create a file inside it. This will be the entry point for your FastAPI application. You can then import FastAPI and create a new FastAPI instance as follows:

from fastapi import FastAPI

app = FastAPI()

You can now define your API endpoints like this:

def read_root():
    return {"Hello": "World"}

Add healthcheck

In order to be able to deploy FastApi project to we need to provide healthcheck endpoint.

To add a healthcheck endpoint to your FastAPI application, you can use the @app.get decorator to define a function that will be called when the endpoint is accessed. For example:

 def read_root():
     return {"status": "ok"}

This will create an endpoint at /healthcheck that returns a JSON object with a status field set to ok. You can then use this endpoint to monitor the health of your application by making a request to it periodically.

Add requirement.txt file

In order to deploy a FastAPI application to, you will need to create a requirement.txt file that lists all of the dependencies required by your application. This file is used by to install the necessary packages and libraries when deploying your application.

For FastApi project this is the most important pip package you need to provide.


It is important to make sure that the requirement.txt file is up to date and includes all of the necessary dependencies for your application. This will ensure that your application can be deployed and run smoothly on

You don't want to manually generate the requirements.txt you can use the pip freeze command to generate a list of installed packages and their versions.

pip freeze > requirements.txt

One thing you need to keep in mind is to include uvicorn package, this package will be used to run the server later in

Enable Cors

While this is optional, if you plan on consuming the API from a different domain, you may wish to consider enabling CORS in your FastAPI project in order to facilitate cross-origin requests.

To enable CORS for a FastAPI project, you can use the FastAPI-CORS library, which provides an easy way to add CORS support to a FastAPI application. To install FastAPI-CORS, run the following command:

pip install fastapi-cors

Please don't forget to also update your requirements.txt file with this new pip package.


With FastAPI-CORS installed, you can then use the @app.middleware("http") decorator to add a CORS middleware to your application. For example:

from fastapi_cors import CORS

app = FastAPI()

origins = ["*"]


You also can define allowed domain to acces your API like this.

origins = [

Add Dockerfile supports deployment using a Dockerfile, which is useful for configuring custom dependencies for your application. The following is a basic Dockerfile that can be used for a FastAPI project:

FROM python:3.10-slim-bullseye

COPY . ./

RUN pip install --no-cache-dir -r requirements.txt

CMD ["uvicorn", "main:app", "--host", "", "--port", "8080"]

You are welcome to modify the Dockerfile to meet the specific requirements of your application. Please feel free to make any necessary updates to ensure that the Dockerfile accurately reflects the needs of your application.

Launch Your Application

Once you have configured your FastAPI application and created a Dockerfile, now it's time for you deploy your project to

First, make sure that you have the Fly CLI installed on your system. You can install it using the following command:

curl | sh

Next, navigate to your FastAPI project directory and run the following command to login to your account:

fly login

You will be prompted to enter your account email and password. Once you have logged in, you can use the following command to deploy your application:

fly launch

This command will automatically generate the necessary configuration files for deploying your application to

Onces you done this you can open the public url of your application by running this command.

fly open

If you make any changes to your project, you can simply run the following command to deploy a new version to

fly deploy


Deploying a FastAPI application to is a straightforward process that allows you to host and manage your application in the cloud. By following the steps outlined in this article, you can set up a FastAPI application, configure it for deployment to, and launch it on the platform.

By using, you can take advantage of its scalability, security, and performance features to ensure that your application is running smoothly and efficiently. Whether you are building a prototype or a full-fledged production application, is a great choice for deploying FastAPI applications.

If you encounter any issues while deploying your application to, or if you have any questions, please do not hesitate to contact me on Twitter.