Skip to main content
In this lesson, we guide you through one of the primary methods for deploying your FastAPI application—using Heroku. With Heroku’s simplicity and efficiency, you can launch your application and push updates quickly. We will cover installing the Heroku CLI, creating your Heroku app, configuring a Procfile, deploying your code, troubleshooting common issues, and managing your production PostgreSQL database using Alembic.

Setting Up the Heroku CLI

Before you start the deployment, ensure you have the following prerequisites:
  • A Heroku account.
  • Git installed on your machine.
  • The Heroku CLI installed.
Download the Heroku CLI installer for your operating system. After installation, restart your terminal (or VS Code) for the changes to take effect. Verify the installation by running the following command:
Next, log in to Heroku:
Upon logging in (a web browser will open for authentication), confirm the installation with:
The output should resemble:

Creating Your Heroku App

Heroku provides comprehensive tutorials (search for “Heroku Python” online) but here are the essential steps to create your app:
  1. If you’re using your own application instead of the demo from the Heroku tutorial, skip the repository cloning.
  2. Create your app with the following command:
    This generates a globally unique random name and sets up a Git remote pointing to your Heroku app. Verify your Git remotes using:
    You should see both your GitHub origin and a new Heroku remote. To deploy your code, simply push to Heroku:
For custom app names, execute:
App names must be globally unique. If the name is already in use, try a different name (e.g., append numbers).
After creating your app, visit your Heroku dashboard to see your newly created app, even if no resources are configured yet. Below is an example image of a Heroku dashboard:
The image shows a Heroku dashboard for an app named "fastapi-sanjeev," displaying sections for resources, deployment, metrics, and activity, with no add-ons or recent activity.

Pushing Code and Viewing Logs

To deploy your code to Heroku, push your application to the Heroku remote with:
During deployment, Heroku installs dependencies, sets up the Python runtime (defaulting to python-3.9.7 unless specified otherwise), and builds your application. A successful build might output messages similar to:
If your browser opens the app URL and you encounter an error, it may indicate FastAPI has not been correctly set up for Heroku. To monitor the deployment process in real time, use the logs command:
A “Boot timeout” error may mean that Heroku is unsure how to start your FastAPI application. Ensure your configuration is correct.

Configuring the Procfile

Heroku requires a Procfile in your project’s root directory to define how to run your application. Create a file named Procfile (with a capital P) and add the following line:
This command launches Uvicorn without the --reload flag (which is intended for development only) and instructs it to use the port provided by Heroku via the PORT variable (defaulting to 5000 if unspecified). After updating your Procfile, add, commit, and push the changes to both GitHub and Heroku:
Finally, restart your Heroku dynos to load the new configuration:

Testing and Troubleshooting Your Deployment

After deploying the code, visit your app URL (e.g., https://fastapi-sanjeev.herokuapp.com/) to verify the application is running. If the app hangs or displays an error, check the logs using:
Common issues include missing environment variables that were available in a local .env file. For example, your application may require variables such as:
Heroku does not automatically read a local .env file. You must manually set these Config Vars via the Heroku dashboard or CLI.

Setting Up a PostgreSQL Database

If your application uses PostgreSQL, set up the database as follows:
  1. Create a free PostgreSQL instance (hobby-dev plan) using this command:
    Example output:
  2. Navigate to your app’s Settings > Config Vars on the Heroku Dashboard. Notice that Heroku has automatically created a DATABASE_URL variable containing your PostgreSQL connection string.
  3. To work with individual environment variables (hostname, port, etc.), visit the Heroku PostgreSQL dashboard to retrieve the details, then add them as separate Config Vars:
    • DATABASE_HOSTNAME
    • DATABASE_PORT (default is 5432)
    • DATABASE_USERNAME
    • DATABASE_PASSWORD
    • DATABASE_NAME
    • Plus other variables such as SECRET_KEY, ALGORITHM, and ACCESS_TOKEN_EXPIRE_MINUTES.
Below is an example snippet using Pydantic for your settings:
After updating your Config Vars, restart your dyno:
You can view your app’s information by running:
The output will include details such as your app URL (Web URL), for example:

Updating the Database Schema with Alembic

Suppose your application uses Alembic for database migrations. In that case, your production PostgreSQL instance might start without tables. When you attempt to create a user or perform a database operation, you may encounter errors like:
To update your production database schema, run the Alembic migrations using:
Monitor the output to ensure each migration—such as creating tables or adding columns—is applied successfully. You can verify the database schema with tools like pgAdmin afterward. Below is an image showcasing the pgAdmin interface:
The image shows a pgAdmin interface displaying database statistics, including server sessions, transactions per second, and tuples in/out, with a list of databases on the left.
After completing the migrations, restart the dynos:
Now, your application should function correctly when accessing database tables. Test your app by navigating to its URL, or use tools like Postman. For instance, to create a new user, send a POST request with a JSON payload:
A successful creation (HTTP 201) might return:
Verify the new record in your database via your preferred PostgreSQL management tool. Below is an updated Heroku dashboard image showing the app details and configuration variables:
The image shows a Heroku dashboard for an app named "fastapi-sanjeev," displaying app information and configuration variables such as database URL, hostname, and secret key.
Additionally, your Swagger UI (accessible at /docs) should list all your API endpoints:
The image shows a Swagger UI interface displaying API endpoints for managing posts and users, including actions like GET, POST, PUT, and DELETE. It also includes an authentication section with a login endpoint.

Pushing Updates to Production

When you update your application code or introduce new Alembic revisions, follow these steps:
  1. Commit and push your changes to GitHub:
  2. Deploy the changes to Heroku:
  3. If new migrations are included, apply them:
  4. Restart your dynos if necessary:
Your app should now reflect the latest updates.

Conclusion

Congratulations! You have successfully deployed your FastAPI application to Heroku. You configured essential environment variables, set up a PostgreSQL database, applied Alembic migrations to update your schema, and learned how to deploy subsequent updates. Use these procedures as a foundation to further expand and enhance your application’s functionality. Happy coding!

Watch Video