Deploy a FastAPI Python App
This quickstart guide explains how to deploy a FastAPI (opens in a new tab) Python application to Koyeb using:
- Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
- Pre-built containers you can deploy from any public or private registry.
You will need:
- A Koyeb account (opens in a new tab) - it's free to get started!
- Python (opens in a new tab) installed on your machine
- (Optional) The Koyeb CLI for deployment from the terminal
You can deploy and preview the Python FastAPI application from this guide using the Deploy to Koyeb button:
Consult the repository on GitHub (opens in a new tab) to view this example application.
Create the Python FastAPI app
Get started by creating a basic Python FastAPI application.
Alternatively, you can fork the repository on GitHub (opens in a new tab) to get a complete copy of the code. If you fork the repository, then you can skip to section on git-driven deployment on Koyeb.
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 Python matching criteria is met. We will use a requirements.txt
file to trigger the detection.
In the example-fastapi
folder, create a new virtual environment folder using the following command:
python -m venv venv
For some operating systems, you might need to use the command with python3
:
python3 -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.
Activate and load your virtual environment:
. venv/bin/activate
Install FastAPI and create a requirements.txt
file to store the dependencies and version of each package required to run the application:
pip install fastapi "uvicorn[standard]"
pip freeze > requirements.txt
Create a file called main.py
and add 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"}
This 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 test:
uvicorn main:app --reload
You can now access the application at http://localhost:8000
and the auto-generated interactive API documentation at http://localhost:8000/docs
.
This example application should run on any modern version of Python. If your application requires a specific
Python version, create a runtime.txt
file in your repository with the version number. Consult the build
with Python page to learn more.
Push the project to GitHub
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. Run the following commands to commit and push changes to your GitHub repository, replacing the GitHub username and repository name with values from your account and the GitHub repo name:
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
Replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name.
Deploy to Koyeb using git-driven deployment
To deploy the Python FastAPI app on Koyeb using the control panel (opens in a new tab), on the Overview tab, click Create Web Service and follow these steps:
- Select GitHub as the deployment method.
- Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public FastAPI example repository (opens in a new tab) into the Public GitHub repository:
https://github.com/koyeb/example-fastapi
. - In the Builder section, choose Buildpack and click the Override toggle associated with the Run command field. In the field, input:
uvicorn main:app --host 0.0.0.0
- Choose a name for your App. For example,
koyeb-fastapi
. - Click the Deploy button.
This creates a Koyeb App and Service which builds and deploys your application on Koyeb. You can access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
Deploy to Koyeb using a pre-built container
As an alternative 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 add the following to the file:
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"
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 page on deploying pre-built container images.
What's next
For more examples of FastAPI applications deployed on Koyeb, check out these tutorials:
- Using Dia 1.6B to Build a Text-to-Speech Application on Serverless GPUs (opens in a new tab)
- Using PydanticAI to Build AI Agents for Data Analysis (opens in a new tab)
- Build a Video Processing Pipeline with AssemblyAI on Koyeb (opens in a new tab)
To learn more about the features available for your apps, check out the following documentation:
- Autoscaling (opens in a new tab) - Dynamically adjust the number of Instances within a Service to meet the demand of your applications.
- Scale-to-Zero (opens in a new tab) - Configure your Instances to automatically scale down to zero when there is no incoming traffic, reducing usage.
- Metrics (opens in a new tab) - Learn how to monitor your Services usage.