Building a REST API using Python, FastAPI, and Heroku
By: Hugo Teixeira
Tags: Python, Geolocation, Heroku, Rest Api, Fastapi

First of all, who never had to build and REST API in a few days or even hours? I bet you did, so I had this job, to create a geolocation API that works as a microservice.

I have been using python recently, so, the first idea was to use Flask for this job, but my boss was asking me to look for new approaches as I already had experience using flask. So in my researches, I found some options, and the two top ones were Django and FastAPI.

First decided to test if Django, the one I already met before, fits our needs. It ended to be a very powerful framework and used to do a lot of things that would not be necessary, so let’s see how FastAPI has worked for me.

So, let's begin learning how to install it. in this post, I will be using pipenv, but you can use any package manager that you enjoy:

pipenv install fastapi

pipenv install uvicorn

We’ll use uvicorn as an ASGI server.

Now, you have uvicorn and FastAPI installed, you can now begin to code.

Let's create the architecture:

├── main.py

├── manager.py

├── app

│ ├── router.py

│ ├── configs.py

│ └── geolocation

│ ├── services.py

│ ├── controller.py

│ ├── models.py

│ └── routes.py

└── pipfile

Let me say that every folder needs an __init__.py file to be considered as a module.

Now, starting to code. The main.py is the one responsible for running your server and setting this up.

We set the prefix to “v1”, so we can have multiple versions API’s.

Setting up the manager.py now:

This one is only a helpful file that I created that helps to create the modules, to use it you should run the following command:

python manager.py module MODULE_NAME

In the router.py file, you need to set up all the primary routes, one to each module. We use the API router so we can group all the routes together in the same place.

Now, creating the file that will be responsible for the endpoints for the geolocation module the routes.py inside the geolocation folder.

Remember, this only one way to deal with the parameters of the endpoint, other ways you can find it HERE.

Next step, the controller, here you need to take the response body or parameters and organize it to provide the data to the services:

Now, the services, the one responsible for all the magic, here you can make all the data processing, requests, DB management, anything that you need. We are going to make a request to a geolocation by IP provider.

You can see that we used a file named configs.py, in this file we can store any static data, an API token, for example.

API_TOKEN = "YOUR_API_TOKEN_HERE"

Now, after all this coding we can build this project to Heroku or any cloud platform. After creating a Heroku app you need to deploy using a Procfile.

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

The Procfile should look like this. Now, looking for the URL of your project in Heroku: https://YOUR-PROJECT-NAME.herokuapp.com/v1/geolocation/

Resulting in something like this:

{"lat":"-8.0539","long":"-34.8811"}

Now, you can improve this service saving the requests to make a database cache, you can create other services using the manager.py and adding it to the router.py