RESTful API with Python and Flask

Hasitha Chandula
Analytics Vidhya
Published in
4 min readDec 29, 2019


Python is one of the most popular Programming Language. There are many frameworks and libraries in python. Flask is one of them. In this article, I’m going to show you how to build a simple RESTful API using Flask.

Before start, you need to install python 3 on your computer. You can download and install python 3 by clicking here. After that, you can verify that by type python — version on your terminal or cmd in windows.

To build Flask App First we need to install Flask by type pip install flask in terminal and press enter. After flask installed successfully now we can build our RESTApi.

Create Server

First, we need to create a server using flask. To do that we need to create a new python file. I named it and the code looks like bellow.

In here what I did was import the Flask from flask and create our server in port 5000. Now we can access our server in http://localhost:5000. Now in the project directory open the terminal and enter python and press enter. Now if you see something like bellow we are good to go.

We can create routes using our app variable. I will show you how to create GET, POST, PUT, DELETE routes.

GET Request

“/users” is our endpoint so we can get the response if we goto “http://localhost/users” using postman and request type is get.

Get Request With Parameter

We can pass parameters like this. We need to put our parameter’s type the rute path.

POST Request

In post requests, we need to import request from flask.

Then we can access the request body. And if we need to return data as JSON then we need to convert data as JSON. We can do the convert using “jsonify”. First, we need to import that too.

Now we can do post request and return data as JSON to client. Code looks like bellow.

PUT Request

Put Request is more likely POST Request. Code is like this,

DELETE Request

In Delete Request, we pass parameters and find the element by the parameter and delete that item. Code like this,

Now I show you a full CRUD App using flask. In this example, my all data save in a variable but in the real world, we need to save data in a database.

In My Example, I will create users with name, age and job and display all users, display user by their name, if user not found display error messages, edit users by their name, and finally delete users by their name. Here is my Full Example.

Source code here.

Thank you.



Hasitha Chandula
Analytics Vidhya

Senior Full Stack Engineer specializing in TypeScript & Go. I create scalable web apps, love learning new tech, and thrive on teamwork.