Create and deploy a flask API in Heroku (Part 1)

A guide for creating a flask API and deploying in Heroku

Sachinjose
3 min readJan 26, 2021

Hey, Welcome All to the series on developing a Flask API and deploying them. I will try writing this in two parts.

In this series, we will look into how we can create a flask API, deploy it in Heroku and access it in postman. Follow the series to complete the entire flow. Let's dive in.

This tutorial assumes you know:

  • Python Programming
  • Basics of Rest API
  • Basic concepts of Flask and how it works
  • Knowledge of how GitHub works.
  • An Heroku

What's a Flask Restful API?

API (Application Programming Interface) is an intermediatory software that allows communication of one application with another.

REST is a type of API, that has a set of standard to build a web API. Not all APIs are REST, but all REST services are APIs.

Flask is a widely used micro web framework in Python. Flask-RESTful is an extension in Flask that helps in building REST APIs easily. It is a lightweight abstraction that works with your existing ORM/libraries.

Installation

Before we start let's begin with installing the below python libraries. It is always recommended to create a python environment and install these libraries inside it

pip install Flask
pip install flask-restful

Minimal Flask APP

Let's create a hello world in the flask API.

You can find the details of codes in inline commands.

Now open up a browser and test out your API by searching http://127.0.0.1:5000/

You can go ahead and test it by calling the same API in postman.

Before we dig deeper, let's look into a concept of argument parsing in flask API.

Argument Parsing

Flask provides easy access to form data, still it’s a pain to validate form data. Flask-RESTful has built-in support for data validation using a library called reqparse.

from flask_restful import reqparseparser = reqparse.RequestParser()
parser.add_argument('name', type= str, help= 'Name of the book')
parser.add_argument('author', type= str, help= 'Author of the book')
args = parser.parse_args()

parser.add_argument accepts three variables, name of the argument, type of the argument and the help statement for the argument, which will help indicate the user when users forget to input the argument.

Flask Restful-API

Now that we have created a base API application, let’s go ahead and build the next level of code which supports get, post, put functionality.

First, let's try a POST function to add a new item into our database

you can try using GET, PUT, DELETE methods for the same API.

Summary

So today you took your first steps and learned about how to create a flask API and about request parser in flask restful which helps in validating form data easily.

What’s Next?

In the upcoming post, we will deploy in Heroku and access it using postman.

--

--