Python

How to create a simple flask API in Python

REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data.

In this article, we will build a REST API in Python using the Flask framework. Flask is a popular micro framework for building web applications. Since it is a micro-framework, it is very easy to use and lacks most of the advanced functionality which is found in a full-fledged framework. Therefore, building a REST API in Flask is very simple.

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

  1. Using Flask without any external libraries
  2. Using flask_restful library

Libraries required:

flask_restful can be installed via the pip command:

pip install flask_restful

flask can be installed via the following pip command:

pip install flask

Create a new folder and create a python file named main.py inside it

from distutils.log import debug
import json
from pickle import TRUE
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return json.dumps({'name': 'GeekyCodes',
                       'email': 'geekycomail@gmail.com'})
app.run(debug=TRUE)

Save the file and then open command prompt and run the python file using the following command

python main.py

Now open the browser and paste the following address http://127.0.0.1:5000/

Leave a Reply

%d bloggers like this: