REST API With Flask

Abhinav Anand
The Startup
Published in
4 min readJul 30, 2020

INTRO

API Development has become an essential skill for Backend Developers. Many companies also hire Developers for API Development only. Developing a REST API with Python is fairly simple as compared to other languages.

WHAT IS A REST API?

REST (REpresentational State Transfer) is an architectural style and an approach to communications that are often used in the development of Web services. The use of REST is often preferred over the more heavyweight SOAP (Simple Object Access Protocol) style because REST does not leverage as much bandwidth, which makes it a better fit for use over the Internet. The SOAP approach requires writing or using a provided server program (to serve data) and a client program (to request data).

In simple words a REST Architecture comprises of a REST server and a REST client, the server provides access to resources and the client accesses and presents the resources.

Architecture for REST APIs

The following HTTP methods are most commonly used in a REST-based architecture.

  • GET − Provides read-only access to a resource.
  • POST − Used to create a new resource.
  • DELETE − Used to remove a resource.
  • PUT − Used to update an existing resource or create a new resource.
  • OPTIONS − Used to get the supported operations on a resource.

In this article, we are going to perform the GET and POST methods only.

Enough talking, let’s dive into the practical things.

ESSENTIALS FOR OUR API CREATION

  • Python
  • Flask(a Python microframework)
  • Flask-RESTful

You can download the data.json file here: Download which we are going to use as our database to serve data. (The way you want to keep your database i.e. SQLite, JSON, text file or else is totally up to you.)

Now, create a folder named REST-API and put that data.json file we downloaded in that folder. In the same folder create a file named app.py.

Before starting to write code, let’s install the required libraries by typing the following commands-

$  pip install flask
$ pip install flask-restful

That’s it. Let’s build our API. You can find the code used in this article here: https://github.com/ab-anand/Flask-RESTful

Firstly we have loaded the JSON file and assigned the 3 components in JSON file to variables.

Next, we’ve done Resourceful Routing, resources give you ease to access multiple HTTP methods just by defining methods on your resource.

Now let’s finish the code.

We’re done. Let’s run the file as:

$ python app.py

That’s it. Our API is up and running now on localhost, port 5000. There are three endpoints we have defined-

  • /services — GET method only
  • /complaints — GET and POST method both
  • /hospital — GET and POST method both

Let’s check for the hospital section,

Visit: http://localhost:5000/hospital

You should see this,

Similarly, you can check for other endpoints.

You can also check our API by curl:

$ curl http://127.0.0.1:5000/hospital

Now you can see how easily we can serve our database into JSON format by the API we created.

Code walk-through

  • Download the data.json file.
  • Install the required libraries.
  • Load the JSON file and use the data
  • Do the Resourceful Routing
  • Returned the data in JSON format.

Wow! Creating an API using Flask is so easy, isn’t it? Similarly, you can add PUT, DELETE support to our API. We can also have the authenticating system for data fetching through API. Github link for the code used above:

--

--

Abhinav Anand
The Startup

Hello World! I’m a Software Engineer. Loves toying the underlying structures of technologies in the world of zero’s and one’s.