How to Deploy a FastAPI App on Heroku for Free | by Shinichi Okada | Towards Data Science

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’m currently hosting this FastAPI website at Heroku as a demo.
Table of Contents· Introduction· Git· Setting up Your Heroku Account· How to Create the requirement.txt for FastAPI on Heroku· runtime.txt· Procfile· Environment Variables· How to Upload to Heroku· How to Add a Subdomain· How to Keep Your App Active 24/7  ∘ cron-job.org· How to fix a failed cron-job  ∘ Kaffeine· How to Rename Your App· How to ssh to Heroku· Conclusion· Newsletter· References
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 and create a Heroku app:
$ heroku login$ heroku create
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 following to the requirements.txt manually.
uvicorn==0.12.3aiofiles==0.6.0python-multipart==0.0.5jinja2==2.11.2
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:
python-3.8.6
Heroku requires the Procfile.[7]
If you have the main.py in the app directory, add the following:
web: uvicorn app.main:app --host=0.0.0.0 --port=${PORT:-5000}
If you have the main.py in the root directory:
web: uvicorn main:app --host=0.0.0.0 --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 command opens 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, serene-example-4269.herokuapp.com.
The image below shows how to add toolbox.mywebsite.com subdomain.
After 30min of inactivity, the Heroku app goes idle. You can find your app process using heroic ps.
You can find the “idle” in the last line.
This is my other app process status showing “up”.
cron-job.org [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 15 minutes. If you set it Every 30 minutes, it may delay 5 to 15 seconds.
You can edit or delete your cronjob at ease.
Your cron-job may fail telling “Response data too big” or “Output too large”.
The cron-job.org accepts up to 4 kB of response data from your URL/script. To fix it, you can create an endpoint that returns a JSON string rather than an HTML page.
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.
http://kaffeine.herokuapp.com/#decaf
$ 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:execEstablishing credentials... doneConnecting to web.1 on ⬢ demo-desuyo...~ $
Heroku provides max of 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.
cron-job.org and Kaffeine provide a cronjob to visit your URL to avoid sleeping.
Get full access to every story on Medium by becoming a member.
Account verificationFree Dyno HoursSSH KeysHeroku CLICustom Domain Names for AppspipreqsProcfilecron-job.orgKaffeine
Account verification
Free Dyno Hours
SSH Keys
Heroku CLI
Custom Domain Names for Apps