How to Deploy a FastAPI App on Heroku for Free | by Shinichi Okada | Jan, 2021


How to keep your app active 24/7

Heroku is a cloud Platform As A Service (PaaS) supporting Ruby on Rails, Java, Node.js, Scala, Clojure, Python, PHP, and Go. Heroku’s free version offers 550–1000 dyno hours per month. Your apps sleep after 30 mins if no one is using your app. Your apps will be always free if your monthly dyno hours is within the limit.

The Dynos are isolated, virtualized Linux containers on the Heroku platform.

In this article, you will learn how easy it is to set up and run your FastAPI project and making it active 24/7 on Heroku.

I use Gitstart to automate the Git initialization. From your terminal, it runs git init. Add .gitignore and README file and commit with a message. This creates a new repo in Github, and it will push all the files.

$ mkdir my_new_project
$ cd my_new_project
$ gitstart

Personal accounts are given a base of 550 free dyno hours each month. In addition to these base hours, accounts that verify with a credit card [1] will receive an additional 450 hours added to the monthly free dyno quota. [2]

You need to add SSH Keys to Heroku.[3]
Install the Heroku CLI [4]
Then log in to Heroku from your terminal.

$ heroku login

You can add a custom domain to your Heroku app.[5]

Heroku requires the requirement.txt to install your Python packages. pipreqs [6] generates it based on imports in your project.

$ pip install pipreqs
$ cd my_new_project
$ pipreqs ./
# if you already have the requirement.txt
$ pipreqs --force ./

Since pipreqs generates it based on import, you need to add the followings to the requirements.txt manually.


You need the uvicorn to start an ASGI server on Heroku. You need the aiofiles for your static files. If your project has a form, add the python-multipart. If your project uses a template engine, add the jinja2.

Heroku uses the default Python version. In order to use a certain Python version, add runtime.txt with your Python version. For example:


Heroku requires the Procfile.[7]

If you have the in the app directory, add the following:

web: uvicorn app.main:app --host= --port=${PORT:-5000}

If you have the in the root directory:

web: uvicorn main:app --host= --port=${PORT:-5000}

Heroku doesn’t use the .env file. Instead, add values in your .env file to Config Vars. You can find it under the Settings tab.

Now you are ready to push your project to Heroku:

$ git push heroku main
$ heroku open

The heroku open commands open your project in your browser.

I am using Cloudflare and this is what I had to do.

Select CNAME for the type. Add your subdomain name for the Name. Add your Heroku domain for the Content. Your Heroku domain name is something like,

The image below shows how to add subdomain.

After 30min of inactivity, the Heroku app goes to idle. You can find your app process using heroic ps.

You can find the “idle” in the last line.

This my other app process status showing “up”. [8] and Kaffeine [9] provide a free service. Both visit your URL at a scheduled time.

You can create a cronjob by adding your URL and Execution schedule.

For your cronjob, you need to select Every 30 minutes.

You can edit or delete your cronjob at ease.


Kaffeine pings your Heroku app every 30 minutes so it will never go to sleep.

How to remove your app from Kaffeine

The link in the website doesn’t work but if you click the link below it worked for me.

$ heroku apps:rename new_app_name

When you rename your app, don’t forget to update your cron job.

You may want to ssh to Heroku. heroku ps:exec does that.

$ heroku ps:exec
Establishing credentials... done
Connecting to web.1 on ⬢ demo-desuyo...
~ $

Heroku provides max 1000 hrs of free hosting for your project which uses under 512MB of RAM usage. You can use your domain name. If you require SSL on custom domains, you need to upgrade to the Hobby tier. and Kaffeine provide a cronjob to visit your URL to avoid sleeping.

  1. Account verification
  2. Free Dyno Hours
  3. SSH Keys
  4. Heroku CLI
  5. Custom Domain Names for Apps
  6. pipreqs
  7. Procfile
  9. Kaffeine

Read More …


Write a comment