How to create REST APIs with Flask

Flask-RESTful is an excellent Flask extension that can be used for quickly building REST APIs. In this tutorial we will see how to develop REST APIs with Flask-RESTful. For the demo purpose this article is using dummy dictionary, however same API can be configured for data coming from Databases or any other sources.
Pre-requisites

  • Python 3.4 or above
  • Postman for making REST API calls
  • Install Flask and Flask-RESTfullibraries

    
    
    pip install Flask
    
    pip install Flask-RESTful
    
    

    Create a file app.py and write below code snippet

    
    from flask import Flask, request, jsonify
    from flask_restful import Resource, Api
    
    app = Flask(__name__)
    api = Api(app)
    
    dummy_data = {
        "FirstName": "Test-User",
        "LastName": "user",
        "Age": "25"
    }
    
    
    class DummyAPI(Resource):
        def get(self):
            data = jsonify(dummy_data)
            return data
    
    
    api.add_resource(DummyAPI, '/dummydata')
    
    if __name__ == '__main__':
        app.run(debug=True)
    
    
    
    

    Run Python app with below command

    
    
    python app.py
    
    
    

    Open Postman and execute GET request as shown below.

    pm-1

    You should see below output after executing the GET Request

    pm-2

    Categories: Python