Deploy a FastAPI Python App

Deploy a Python FastAPI application on Koyeb and benefit from native auto-healing, automatic HTTPS (SSL), and global load-balancing across our edge network with zero configuration.

This guide explains how to deploy a Python FastAPI application on Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

To successfully follow this documentation, you will need to have a Koyeb account. You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the sample Python FastAPI application that we will run on Koyeb in this guide using the Deploy to Koyeb button below.

Deploy to Koyeb

You can access the repository used for this documentation here.

Create the Python FastAPI app

Get started by creating a minimalistic Python FastAPI application. You will need Python installed on your machine. In your terminal, run the following commands to create the directory that will hold the application code:

mkdir example-fastapi cd example-fastapi

Koyeb detects Python applications when one of the following requirements is met. In this guide, we will use requirements.txt to trigger the detection.

In the folder you created, create a new virtual environment folder using python -m venv venv. Virtual environments provide isolation from the system environment allowing each virtual environment you create to have its own installation directories, dependencies, etc. Then to activate and load your virtual environment, type:

. venv/bin/activate

Install FastAPI and create a requirements.txt file to store the dependencies and version of each package required to run our application:

pip install fastapi "uvicorn[standard]" pip freeze > requirements.txt

Next, create a main.py file and copy the following FastAPI application code:

from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello from Koyeb"}

The code above defines a single route / that returns a JSON response with the message Hello from Koyeb when a GET / request is made.

Run the Python FastAPI app locally

Launch the application locally to make sure everything is running as expected.

uvicorn main:app --reload

You can now access the application at http://localhost:8000 and the automatic interactive API documentation at http://localhost:8000/docs.

Deploy the Python FastAPI app on Koyeb using git-driven deployment

In the project directory, initialize a new git repository by running the following command:

git init

We will use this repository to version the application code and push the changes to a GitHub repository. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:

git add requirements.txt main.py git commit -m "Initial commit" git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git git push -u origin main

Via the Koyeb control panel

To deploy the Python FastAPI app on Koyeb, using the control panel, click Create App and follow the steps below:

  1. Select GitHub as the deployment method
  2. Choose the GitHub repository and branch containing your application code
  3. Under Build and deployment settings, set the run command to web: uvicorn main:app
  4. Name your app, for instance, koyeb-fastapi
  5. Click the Deploy button

A Koyeb App and Service have been created. Your application is now going to be built and deployed on Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Via Koyeb CLI

To deploy the Python FastAPI app on Koyeb using the Koyeb CLI, run the following command in your terminal:

koyeb app init example-fastapi \ --git github.com/<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> \ --git-run-command "uvicorn --host 0.0.0.0 main:app" \ --git-branch main \ --ports 8000:http \ --routes /:8000 \ --env PORT=8000

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Access deployment logs

To track the app deployment and visualize build logs, execute the following command:

koyeb service logs example-fastapi/example-fastapi -t build

Access your app

Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:

$ koyeb app get example-fastapi ID NAME STATUS DOMAINS CREATED AT 2b3c4170 fastapi-service HEALTHY ["example-fastapi-myorg.koyeb.app"] 16 Feb 23 09:06 UTC

Access runtime logs

With your app running, you can track the runtime logs by running the following command:

koyeb service logs example-fastapi/example-api -t runtime

Deploy the FastAPI app on Koyeb using a pre-built container

Alternatively to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.

To dockerize the Python FastAPI application, create a Dockerfile in your project root directory and copy the content below:

FROM python:3-alpine AS builder WORKDIR /app RUN python3 -m venv venv ENV VIRTUAL_ENV=/app/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" COPY requirements.txt . RUN pip install -r requirements.txt # Stage 2 FROM python:3-alpine AS runner WORKDIR /app COPY --from=builder /app/venv venv COPY main.py main.py ENV VIRTUAL_ENV=/app/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" ENV FLASK_APP=app/main.py EXPOSE 8000 CMD [ "uvicorn", "--host", "0.0.0.0", "main:app" ]

The Dockerfile above provides the minimum requirements to run the Python FastAPI application. You can easily extend it depending on your needs.

To build and push the Docker image to a registry and deploy it on Koyeb, refer to the Deploy an app from a Docker image.