REST API With Flask

Abhinav Anand
Jul 30, 2020 · 4 min read

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:

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Abhinav Anand

Written by

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

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Abhinav Anand

Written by

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

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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