How to Create API in Python using Flask

How to Create API in Python using Flask

There are many powerful frameworks to create an API like Django and Flask. in this tutorial I’ll show you How to Create API in Python using Flask.

Before creating an API let’s firstly talk about what is an API. API stands for Application Programming Interface. The traditional definition of an API is,

An API is a computing interface that defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.

As I mentioned we’ll use flask so to use it first we have to install it. To install flask just open terminal and type,

pip install Flask

Now let’s jump into the actual code. we will start by creating a simple app in flask.

CODE:

import flask

# creating a Flask app
app = flask.Flask(__name__)

#Home route
@app.route('/', methods=['GET'])
def home():
    return "<h1>First App</h1>"

app.run()

Explanation:

  1. We started by importing the flask.
  2. Then we created an app. Here the Flask is the constructor that takes the name of the current module (__name__) as an argument.
  3. The route() method is a decorator that tells Flask what URL should trigger our function. we also specified the method GET. read more about methods
  4. the run() method is used to run an app.

When we run this code at the home route(‘/’). we’ll see something like this in a localhost,

How to Create API in Python using Flask - papaprogrammer

Now let’s create an basic API that reads the URL and return a specific information.

CODE:

import flask
from flask import jsonify

#creating a Flask app
app = flask.Flask(__name__)

cars = [
    {'id': 0,
     'name': 'Porsche 911'},
    {'id': 1,
     'name': 'Porsche Cayman'}
]

#Home route
@app.route('/', methods=['GET'])
def home():
    return "<h1>First App</h1>"

@app.route('/car/', methods=['GET'])
def api():
    return jsonify(cars)

@app.route('/car/<id>', methods=['GET'])
def api_id(id):
    return jsonify(cars[int(id)])

app.run()

Explanation:

In the second line, we imported jsonify because later on, we are dealing with JSON (JavaScript Object Notation) data also we can say dictionaries.

We created a list named cars inside that we have two dictionaries each contains Id and name of the car. Assume that as a small database.

After then, we have created another route ‘/car/’ that will call api() function and this function returns the whole data stored in cars variable.

How to Create API in Python using Flask - papaprogrammer

To get specific information by giving a specific id, we have created another route ‘/car/<id>’ here id is a variable section that’s why we have put it in a < >.

How to Create API in Python using Flask - papaprogrammer
How to Create API in Python using Flask - papaprogrammer

When we specify the id in the URL then the function api_id() will return the data at the index of id. Learn more about accessing elements from a list.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like