Skip to main content

FastAPI

FastAPI is a modern Python web application framework that simplifies the development of server-side applications. It provides a robust set of features for building web servers and APIs.

tip

Get started in no time with the FastAPI template.

Deployment

Learn how to deploy an existing FastAPI app using Genezio, a serverless deployment platform that simplifies app management and reduces costs.

Prerequisites

1. Install genezio

Use your preferred package manager to install Genezio:

npm install genezio -g

2. Ensure you have a FastAPI App

If you don't have a FastAPI app, you can create one using the following steps:

Create a Hello World FastAPI App

1. Initialize a new Python Project

Run the following command to initialize a new Python project in an empty directory:

mkdir fastapi-app
cd fastapi-app

2. Create Environment Setup

Create a new virtual environment in the root directory of your project:

python -m venv venv

3. Activate the Virtual Environment

Next, you need to activate the virtual environment:

.\venv\Scripts\activate

4. Install FastAPI

Next, install the FastAPI package:

pip3 install fastapi uvicorn
pip3 freeze > requirements.txt

5. Create a FastAPI App

Create a new file named index.py and add the following code:

index.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
return {"Hello": "World"}

if __name__ == "__main__":
import uvicorn
uvicorn.run(app)

6. Test the FastAPI App

Run the following command to start the FastAPI app:

python index.py

Open a web browser and navigate to http://localhost:8000 to see the app running.

Deployment Guide

1. Create the Genezio Configuration File

Now, create a genezio.yaml file in the root directory of your project.

This file will contain the configuration needed to deploy your backend using Genezio. Here is an example configuration.

info
  1. You need to have a requirements.txt file in the root directory of your project for dependencies.
  2. You might need to replace the handler field with the name of your variable that holds the FastAPI app.
  3. You might need to replace the entry field with the name of your main application file.
  4. You might need to replace the path field with the path relative at genezio.yaml file.
  5. This example configuration works if genezio.yaml is in the same directory as your main application file.
genezio.yaml
# The name of the project.
name: fastapi-app
# The region where the project is deployed. Available regions: us-east-1, eu-central-1
region: us-east-1
# The version of the Genezio YAML configuration to parse.
yamlVersion: 2
backend:
# The root directory of the backend.
path: ./
# Information about the backend's programming language.
language:
# The name of the programming language.
name: python
# The package manager used by the backend.
packageManager: pip
# Information about the backend's functions.
functions:
# The name (label) of the function.
- name: hello-world-fastapi-app
# The path to the function's code.
path: ./
# The name of the wsgi application.
handler: app
# The entry point for the function.
entry: index.py
# The type of the function.
type: httpServer

This configuration file specifies the project name, deployment region, and details about the backend.

2. Test Your App Locally

Before deploying your app, you can test it locally to ensure it's working correctly.

Run the following command in your terminal:

python index.py

Open a web browser and navigate to http://localhost:8000 to see the app running.

3. Deploy your project

Finally, deploy your project. A browser window will open, and you will be prompted to log in to your Genezio account and authorize the CLI to make the deployment. Run the following command in your terminal:

genezio deploy

If your application use environment variables, you can deploy them using the following command:

genezio deploy --env <path-to-your-env-file>
info

You need to deploy your environment variables only once. After that, you can deploy your project without the --env flag.

For more information about environment variables, you can check the official documentation.

See your app in Genezio Dashboard

After deploying your application, you can test it to ensure it's running correctly. To verify that your FastAPI app is working, open a web browser and navigate to the URL provided for your deployed function.

This URL can be found in the deployment output under the Functions Deployed section.

Additionally, you can monitor and manage your app through the Genezio App Dashboard. The dashboard URL, also provided after deployment, allows you to access comprehensive views of your project's status and logs.

You can find this URL in the deployment output under the App Dashboard URL section.

Support

We invite you to join our community on Discord for further information and help.

Happy Learning!