How to deploy a FastAPI API on Heroku

Joël-Steve N.
3 min readJan 16, 2024

--

Herok Deploy FastAPI

FastAPI is a modern, high-performance web framework for building APIs with Python. Heroku is a cloud deployment platform that makes it easy to deploy applications. In this blog, we’ll guide you through the process of deploying a FastAPI API on Heroku.

Prerequisites

Before starting the deployment process, please make sure you have the following prerequisites:

  1. Heroku account: Create a Heroku account on their website at heroku.com if you don’t already have one.
  2. Git: Install Git on your machine if you haven’t already. You can download it from the official Git website.
  3. FastAPI: Make sure you’ve developed your own FastAPI API and that it’s working properly locally.

Steps for deploying the FastAPI API on Heroku

Follow the steps below to deploy your FastAPI API on Heroku :

Step 1: Create a new Git repository

Make sure you have initialized a Git repository in your FastAPI project directory using the following command:

git init

Step 2: Create a `Procfile`

Create a file called Procfile (without extension) at the root of your project. Open it in your favorite text editor and add the following line:

web: uvicorn main:app --host=0.0.0.0 --port=$PORT

This file specifies the startup command for launching your FastAPI application with Uvicorn.

Step 3: Create a `requirements.txt` file

Create a requirements.txt file at the root of your project, and add all the dependencies required for your FastAPI API. You can automatically generate the list of dependencies using the following command:

pip freeze > requirements.txt

Step 4: Create a Heroku application

Log in to your Heroku account from the command line using the following command:

heroku login

Enter your Heroku credentials to log in.

Next, create a new Heroku application by executing the following command:

heroku create name-of-your-application

Replace name-of-your-application with your desired application name, or leave it blank to generate an automatic name.

Step 5: Adding and overwriting files

Add all the files in your Git project using the following command:

git add .

Then commit the files using the following command:

git commit -m "Initial commit"

Step 6: Deploy your application

Finally, deploy your FastAPI application on Heroku by executing the following command:

git push heroku master

This will upload your files to Heroku, install the necessary dependencies and launch your FastAPI application.

Step 7: Check deployment

After deployment, Heroku will generate a URL to access your deployed application. You can run the following command to open the URL in your browser:

heroku open

Check that your FastAPI API is working properly by accessing the URL provided by Heroku.

Conclusion

Congratulations! You’ve successfully deployed your FastAPI API on Heroku. Now your application is accessible from a production environment. You can continue to develop and enhance your API using the scalability and management features offered by Heroku.

In this blog, we’ve explained the basic steps involved in deploying a FastAPI API on Heroku. Heroku makes it easy to deploy and manage applications in the cloud. Take advantage of this platform to deliver fast, reliable API services to your users.

--

--

Joël-Steve N.

Chief Technology Officer | Community Manager | Backend Developper (Python, Javascript, Java) | Motivational Coach