Deploying your first API on Heroku

Christien Kelly
6 min readJun 28, 2022

--

What to expect

  • An Introduction to Platform as a Service
  • Creating a Python API
  • Deployment of the API to Heroku

Pre-requisites

  • Python
  • An IDE to write code
  • Git
  • A Heroku account

In this article, I am operating on a Mac and my IDE is Visual Studio Code. The goal of this article is to provide context on Platform as a Service solution (PAAS) as well as a bit of guidance to host your application on the web!

What’s Platform as a Service, and why would I use it?

When you enter the world of programming, I think it is a safe assumption that the beginnings are focused on:

  1. Learning the Fundamentals (Data Structures, Frameworks, Concepts)
  2. Learning one or two languages

But you’ll soon realize this is scraping the barrel of full-on software projects. When you want your work to be seen by the external world you have to begin thinking about things like

  • where do I host my app? (Share it with the world)
  • How do I track KPIs? (What are people doing in my app)
  • How do I create a secure app? (How do I not get hacked)

Some of these topics are beyond the scope of this article but we will be going into the question of “where do I host my app”?

In my opinion, the lightest solution for deploying code is using a Platform as a Service (PAAS) solution. For this article we will be using Heroku. Depending on what you are launching this solution could be perfect and free if you are deploying something small like a Twitter Bot or a small API backend.

On PAAS platforms, you are essentially purchasing a container on your service provider’s platform and uploading your code to it. By purchasing a container it leaves the actual “server” management up to the platform taking away separating you from the responsibility of resolving a server crash or other DevOps work.

Let’s build the app

This app has one endpoint / and when hit it will return the text saying "the application is running!"… note that the purpose of the article is the deployment and not the api. For more details on FastApi see their site (https://fastapi.tiangolo.com/).

Project Setup

Open a new project directory and terminal.

# create a new python virtual environment
python3 -m venv env
# activate your virtual environment
source env/bin/activate
# install fast api
pip install fastapi
pip install "uvicorn[standard]"

This installs the dependencies needed to run the Python app. Now run the following line to set up your main file.

nano main.py and paste the following:

from fastapi import FastAPIapp = FastAPI()@app.get("/")def read_root():    return "The application is running"

This will create the main file, and we should be able to start the server using the command:

uvicorn main:app — reload

After running this if you navigate to https://localhost:8000 you should see the message!

Proof the application is running

This is the scope of coding for this article, the next step is to deploy this so that our application is accessible by anyone in the world (that can access our URL).

Finally, before cleaning up let’s add a requirements.txt file so that anyone can build our environment. In the root directory of your project type:

pip freeze > requirements.txt

This command pipes the specific packages that you actively use in your projects into a file called requirements.txt. Now we’re ready to deploy our application.

Let’s Deploy It!

We are going to be deploying our application on Heroku, the cloud Platform as a Service software. The free version is great for proof of concept apps to test that a solution is working. I’ve used the free version several times and have had no problems.

For more info on different pricing plans see their page -> https://www.heroku.com/pricing

For this section make sure you already have:

  • A Heroku Account
  • The Heroku CLI installed

The building of the app will be done using terminal and the heroku CLI. Breaking down what we are about to do…

  1. We will start by creating a new Heroku app using the Heroku CLI
  2. We are then going to configure our app so that it knows that we will be running it on Python
  3. We will be deploying our API application onto Heroku using GIT.

Creating the app

In terminal let’s navigate to where our app is stored and activate the heroku CLI by logging in.

# navigate to the directorycd ./fast-api-project# log in heroku login

The log in command will direct you to the web portal to sign in. After you have signed in successfully, let’s create our app. The command for this is heroku create <app-name>

heroku create christien-fast-api-app

You will see a message that looks like the following letting you know that your app has been created.

Creating the app

Notice our app points to the URL -> https://christien-fast-api-app.herokuapp.com/ this is where our new app lives! If you navigate there you’ll see that there’s a blank site there :D. Time to configure it so it’s compatible with Python.

Our empty app

Configuring our app

The configuration requires 3 pieces, one we have already done.

We have to define the language we are working in, Heroku is good in that we can work in over 10+ different languages, but we need to give the backend a heads up as to which language we are using. In Heroku, this is done by adding build packs to our app. In our case, we will use heroku/python. (For more info on buildpacks for different languages https://devcenter.heroku.com/articles/buildpacks).

# configure buildpack for Pythonheroku buildpacks:set heroku/python

Secondly, we have to tell Heroku which command is needed to run our app. This data will go into a file called Procfile, which exists inside our root directory.

sudo nano Procfile

This will open up a dialogue to add text to a file, then paste our startup command.

web: uvicorn main:app

Exit here using control + x and then y when prompted to save.

Finally, Heroku needs to know which packages to install, but we already solved this by creating a requirements.txt file.

Deploying our app

To summarize, at this point our directory should look something like this:

Files in our app

You may not have the .gitignore file but that is less important for this article.

Now let’s deploy using git. First, we will initialize a git repository and then we will deploy our code directly to Heroku using:

# initialize the git repositorygit init# push our code to herokugit push heroku main# or if you're on an earlier verison of git and 
# the default branch is master git push heroku <branch name>:main
git push heroku master:main

Just like that, we should see our code deploy to our Heroku app. Now let’s check to see if it’s working!!

# open the heroku appheroku open

Conclusion

This app is literally a shell of what you may build or want to build in the future, but hopefully, it helps to lay the foundation on how you can get your own webpages and web APIs onto the web for anyone to see.

Stay tuned for other articles and be sure to see the full code on GitHub!

https://github.com/christien-kelly/demo-fast-api

Happy Hacking!

--

--

Christien Kelly
Christien Kelly

Written by Christien Kelly

Just a guy learning new technologies.

No responses yet