Image for post
Image for post
Photo by Kari Shea on Unsplash

Creating Simple REST API Using Flask (Part 1)

Step-by-step tutorial on how to create simple REST API using Python, Flask and Heroku

Table of Contents

  • (Part 1) Setting Up Heroku Application
  • (Part 1) Adding Data to Heroku PostgreSQL Database Using SQLAlchemy
  • (Part 2) Creating Simple REST API Locally Using Python and Flask
  • (Part 2) Deploying REST API to Heroku

You may have heard about API before. API or Application Programming Interface determines how application talk to each other by set of rules. REST or Representational State Transfer is the set rules that developers follow when creating their API. There are 6 guiding constraints for API can be referred as REST API that you can read here. One of these constraints implies that each specific URL should give you a piece of data called resources.

In this tutorial, we will try to build simple REST API using Python, Flask, SQLAlchemy, and Heroku. The application diagram that we will build can be described as follows:

Image for post
Image for post
Application diagram

This API will only serve GET request, but you can add other functionality so it can serve other method as well. We will be using Harry Potter data-set from Kaggle as our resources. Also, we will be using Heroku as our hosting site so our API can be accessed publicly by other people. Great! Now, let’s get to the first step!

Setting Up Heroku Application

Quoted from their website, Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It makes deploying application to cloud really easy and fast. Heroku also have free tier with several limitations, but in this tutorial Heroku’s free tier will be enough.

First thing first, if you haven’t, create Heroku account on their website, click here. After setting up your account, you will be directed to your dashboard which will list all of your application in Heroku.

Image for post
Image for post
Heroku Dashboard

To deploy our application in Heroku, you can use either Heroku CLI, Github, or other container registry. You can choose anything that you most comfortable working with. But, in this tutorial, I will use Heroku CLI. For installation guide about Heroku CLI, you can check this link. (If you decides to use Heroku CLI installed, you will need Git installed in your computer too)

Then, we need to create Heroku application that will run our API system. Just click “New” and choose “Create New App”. Type in your app name (your app name maybe isn’t available because other people already use that name, so try to use other name) and click “Create”.

Image for post
Image for post
Creating new app on Heroku

After creating new app, you will be directed your app dashboard. We need to add Heroku Postgres Add-on that will serve as our database and store our data. To do so, click “Configure Add-ons”, search for “Heroku Postgres”, choose the free tier for Heroku Postgres and click “Submit Order Form”. You will then see Heroku Postgres appear in you “Installed add-ons” list.

Image for post
Image for post
Adding Heroku Postgres Add-ons to app

Next, we need to copy the Heroku Postgres configuration in order to connecting our Flask application to our database. Click the “Heroku Postgres” to open its dashboard. To see database credentials, go to “Settings” tab and on “Database Credentials”, click “View Credentials”.

Image for post
Image for post
Heroku Postgres credentials

We will need the database URI for database connection using SQLAlchemy, so copy the credentials and save it for later.

Adding Data to Heroku PostgreSQL Database Using SQLAlchemy

We already have our database running in Heroku, but it still doesn’t have any data, yet. In this tutorial, we will use Python, Pandas, and SQLAlchemy for adding data to database since it’s the simplest method I know.

I recommend to create a new virtual environment for this project

First, open up your favorite code editor and start importing Pandas and SQLAlchemy. I will also use load_dotenv package to save the database credential in .env file.

Importing modules

In the Harry Potter data-set, there are 5 CSV files that contain different information. Later, we will add each of this file into different database table. For each file, I will read it and store it in Pandas DataFrame and store 5 DataFrames into a list.

Read data-sets into dataframes and store it all into a list

Great! Then, I do little bit data preprocessing by setting the character name in Harry Potter 1 data-set into uppercase and setting the column name in Harry Potter 3 data-set into title case. This step would help us when creating the API since all data-sets has the same pattern.

A little bit of data preprocessing

Nice! Our data is looking good and ready to be exported. Now, we will create connection to our database and export our data into database using Pandas.

Exporting data to database

In the code above, first I call load_dotenv() function to read configuration in .env file. This file contains key-value pair information for our database credential which will be stored into db_uri variable. We, then, create engine and connection object and export our data using Pandas.DataFrame.to_sql function. It may not the best method for populating database, but it saves us time to achieve our goal and it works. 🌟

After exporting process finished, we can check whether our database already filled by opening the Heroku Postgres dashboard and check the “Utilization” page.

Image for post
Image for post
Number of database’s row and table exporting data

Great! The number of row and tables are no longer zero.

Other ways to check our database data is by connecting to pgAdmin (Need to be installed in your computer) or using Python as well. Here’s the snippet if you want to check using Python and SQLAlchemy.

List all tables in database

Up until now, we’ve configured our Heroku application, added database to our application and inserted data to our database. In the next article, we will try to code REST API using Flask locally in our computer and deploy it in Heroku. 😸

A beginner venturing into data science

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store