Create REST API using Flask (Python)

Ankush kunwar
3 min readSep 30, 2022

--

Flask is a widely used micro web framework for creating APIs in Python. It is a simple yet powerful web framework that is designed to get started quickly and easily, with the ability to scale up to complex applications.

Flask Installation Process:-

pip install Flask

What is REST API

The term REST stands for REpresentational State Transfer. It is an architectural style that defines a set of rules in order to create Web Services. In a client-server communication, REST suggests to create an object of the data requested by the client and send the values of the object in response to the user.

The architectural style of REST helps in leveraging the lesser use of bandwidth to make an application more suitable for the internet.

Working: A request is sent from client to server in the form of a web URL as HTTP GET or POST or PUT or DELETE request. After that, a response comes back from the server in the form of a resource which can be anything like HTML, XML, Image, or JSON. But now JSON is the most popular format being used in Web Services.

In this article, We will build REST API using Python flask

So let's get started

There are two ways of creating a REST API in Flask:

1. Using Flask without using any other Library
2. Using flask_restful library

Method First

Using flask

use of jsonify is to automatically serialize lists and tuples to JSON Response.

run the above code in any IDE I prefer VS code

result:-

Method Second

Using flask-restful

Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up.In flask_restful, the main building block is a resource. Each resource can have several methods associated with it such as GET, POST, PUT, DELETE, etc. for example, there could be a resource that calculates the square of a number whenever a get request is sent to it. Each resource is a class that inherits from the Resource class of flask_restful. Once the resource is created and defined, we can add our custom resource to the api and specify a URL path for that corresponding resource.

first, you have to install flask restful by using the below command

pip install flask-restful

run the above code in any IDE I prefer VS code

result:-

Thank you for reading !!!

If you enjoy this article and would like to Buy Me a Coffee, please click here.

you can connect with me on Linkedin.

--

--

Ankush kunwar
Ankush kunwar

Written by Ankush kunwar

Experienced Software Engineer Skilled in Microservices, Backend Development, System Design, Python, Java, Kubernetes, Docker, AWS, and Problem Solving

No responses yet