Building APIs with Python+Flask #1

Sogo Ogundowole
Hacktive Devs
Published in
4 min readApr 20, 2019

In the beginning…

Having a solid understanding of how to create flexible and scalable APIs is one of the components of core backend engineering career but before we become that badass engineer doesn’t happen overnight, however, I’m here to help start out the journey.

What are APIs?

An application programming interface (API) is a set of routines, protocols, and tools for building software applications. Basically, an API specifies how software components should interact.

It is actually a part of an application (server-side) that sends and receives requests to perform various actions. It allows interactions with the server-side of software.

A lot of programming languages can be used to create APIs, examples of these languages are:

  • Java
  • JavaScript(Nodejs)
  • Go
  • Ruby
  • Python
  • Clojure

This list goes on but in this tutorial, we’ll be focusing on using python. There a ton of frameworks in python available for building APIs, examples include Django, pyramid, masonite, webpy, bottle, flask and many more.
In this tutorial we’ll be using flask because it is:

  • Light
  • Easy to use
  • Flexible

Environment Setup

The first thing we need to do is to set up an environment (virtual environment) for this project so that we can install necessary packages into it and not alter our global environment. To do this we need virtualenv. In this tutorial, we’ll be using python3.

If you do not have virtualenv already, open your terminal and install:

$ pip3 install virtualenv

After this, create a folder for this project and change directory to that folder

$ cd api_tutorial

To set up our virtual environment for this tutorial, we need a name for the virtual environment, most times it’s called venv but we can name it anything we want.

$ virtualenv venv

It takes a few seconds/minutes to set up, then we activate it

$ source venv/bin/activate

Now we’re ready to sail.

Next thing to do is to install Flask.

(venv)$ pip3 install Flask

Create a file for this project, app.py

(venv)$ touch app.py

Let’s Get Started!

We’ll be building a simple API for a book store. We’ll be able to add items, list items and delete items. Basically, these are going to be possible by request types namely POST, DELETE and GET requests respectively. There many more kinds of requests, we’ll talk more about requests later in this series.

Let’s import Flask and initialize our flask app. For this tutorial, we’ll also be using request and jsonify methods in Flask. request will help with getting data from the request to the API while jsonify will help in converting our response to JSON(Javascript Object Notation).

Next thing here is to create our store schema, which in this case would be a dictionary.

The next thing to do is to create routes usually known as endpoints for APIs.

An endpoint is a route via which a request can be sent to an application and responses based on that request can be received

We’ll be using decorators to create these routes.

Here we have @app.route(‘/item’) this is the decorator that creates the endpoint and the function below it is what action we would like to perform. In this route, we would like to get all items in the store, which implies a list of all items in the store.

Great! Our first endpoint is setup.

Now the second endpoint, which will be responsible for adding a new item to the list in the store.

Here we see inside our decorator’s parenthesis that there is a parameter methods=['POST']this signifies that we’re sending a POST request. For the GET items, we did not specify it because it is GET by default in flask. So a POST request helps us to add a new item to our app.

request.get_json helps us to get the data from the request being sent. So based on the structure of this endpoint, this endpoint will always require the name of item r['name'] and r['price'] so when the request is being sent, these parameters are sent with it.

Next endpoint would be for getting a specific item with a specific name in our store.

Here we see a new style introduced <'string:name'> this denotes we are requesting for that item by name, we could also request by id.

The last endpoint here is for deleting an item in the store.

Voila! Our API is ready. Attach to the end of the file a run method for our app.

port=4098 specifies which port we want it to run on in our(local) environment.

This is a basic introduction into building APIs, more concepts will be discussed in subsequent parts of this series. Keep being awesome! The whole script can be found here

Thanks for reading.

--

--