How to Create a RESTful API Using Flask

1310 VIEWS

· ·

API is also known as an application programming interface that serves as a bridge that allows two programs(client and server) to exchange data with each other using a set of definitions and protocols. The application that is sending the request is known as the client. In contrast, the application that receives and processes the request is referred to as the server. We will create a RESTful API using flask.

Flask is a Python micro-framework for building web applications and web APIs. The framework provides pared-down core functionality, however, it is highly extensible.

Flask Restful is an extension for Flask that adds support for building REST APIs in Python using Flask as the back-end. It encourages best practices and is very easy to set up. Flask restful is very easy to pick up if you’re already familiar with flask. This guide shows you how to use Flask to build a REST API that can form CRUD operations on user data.

REST API

REST API stands for Representational state transfer and is the most popular and flexible api. A client can access the server data by making use of a defined set of functions such POST, GET, PUT, DELETE etc. The data is exchanged by making use of HTTP.

In this tutorial, we will use Flask-Restful to build a REST API that will make use of the defined set of functions (POST, GET, PUT, DELETE) to access data.

Setup

We will start by installing  Flask-Restful from Python Package Index (PyPI) using the terminal/command line. 

pip install flask_restful

Let’s create a database that will contain three fields: first_name, last_name and email.

class User(db.Model):  
    id = db.Column(db.Integer, primary_key=True)  
    first_name = db.Column(db.String())  
    last_name = db.Column(db.String())  
    email = db.Column(db.String())

POST Request

Post request allows a client to send  data to the server, which will be processed by the server and saved to the database. 

from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

class CreateUser(Resource):
    response = {"status": 400, "message": "User not created"}

    def post(self):
        user_data = request.get_json()
        first_name = user_data["first_name"]
        last_name = user_data["last_name"]
        email = user_data["email"]
        user = User(first_name=first_name, last_name=last_name, email=email)
        db.session.add(user)
        db.session.commit()

        self.response["status"] = 201
        self.response["message"] = "User created successfully"

        return self.response, 201

api.add_resource(CreateUser, "/api/create")

The user_data variable stores the request body, which can be accessed by other variables(first_name, last_name and email). The data was added to a Python object, added to a Flask-SQLAlchemy session and we committed the session.  The default response was updated before returning it to the user with the 201 http status code.

The request will be received by the URL http://127.0.0.1:5000/api/create we was created using flask restful Resource.

GET Request

Get requests, allows us to retrieve data from the data, the data can contain a single record or multiple records. We will use this method to retrieve the all the users that we have in out database.

class Users(Resource):
    response = {"status": 404, "message": "Users not available"}

    def get(self):
        users = User.query.all()
        if users:
            all_users = []
            for user in users:
                user_details = {
                    "id": user.id,
                    "first_name": user.first_name,
                    "last_name": user.last_name,
                    "email": user.email,
                }
                all_users.append(user_details)
            self.response["status"] = 200
            self.response["message"] = all_users
            return self.response, 200
        else:
            return self.response, 404

api.add_resource(Users, "/api/users")

The Users class contains a default response which will be returned when no users records are stored in the database and  get method. The database was queried to retrieve all the users and stored in the users variable. We looped through both the users variable and each user record in the user_details variable. Each user_details is appended to the all_users variable which we returned to the user in the updated response message.

Let’s send a get request that will retrieve a single user using the user ID.

class GetUser(Resource):
    response = {"status": 404, "message": "Users not available"}

    def get(self, user_id):
        user = User.query.filter_by(id=user_id).first()
        if user:
            user_details = {
                "id": user.id,
                "first_name": user.first_name,
                "last_name": user.last_name,
                "email": user.email,
            }
            self.response["status"] = 200
            self.response["message"] = user_details

            return self.response, 200
        else:
            return self.response, 404

api.add_resource(GetUser, "/api/user/<int:user_id>/")

The user_id was passed into the get method, which was used to query the database. If a user with the ID exists, the user details are added to the response and returned but if not the default response is returned.

PUT Request

The Put request allows us to update or create a record in the database. We will need to retrieve the record from the database before we can update it.

class UpdateUser(Resource):
    response = {"status": 404, "message": "Users not available"}

    def put(self, user_id):
        user = User.query.filter_by(id=user_id).first()
        if user:
            user_data = request.get_json()
            first_name = user_data["first_name"]
            last_name = user_data["last_name"]
            email = user_data["email"]
            user.first_name = first_name
            user.last_name = last_name
            user.email = email

            db.session.commit()
            self.response["status"] = 200
            self.response["message"] = "User updated successfully"
            return self.response, 200
        else:
            return self.response, 404

api.add_resource(UpdateUser, "/api/update/<int:user_id>")

The put method accepts the user_id which was used to retrieve the user record. If the user record exists in the database, the new data that was sent by the client will replace the existing record.

Delete Request

The delete method is used to delete a single or multiple records from the database. It will also delete the changes that were made to the database will be saved. The delete method will send a request to the server so that the record attached to the endpoint will be deleted from the database.

class DeleteUser(Resource):
    response = {"status": 404, "message": "User not available"}

    def delete(self, user_id):
        user = User.query.filter_by(id=user_id).first()
        if user:
            db.session.delete(user)
            db.session.commit()
            self.response["status"] = 200
            self.response["message"] = "User deleted successfully"
            return self.response, 200
        else:
            return self.response, 404

api.add_resource(DeleteUser, "/api/delete/<int:user_id>")

Just like the PUT and GET methods, this method also makes use of the user_id to retrieve a user from the database, if the record exists the user will be deleted and the changes will be saved to the database. But if the user does not exit the default response message will be returned to the user and no record will be deleted.

Conclusion

Now you know how to create a RESTful API using Flask. Some of the reasons why REST API is very popular are because it’s lightweight, independent, scalable and flexible. Unlike other APIS such as XML-RPC, JSON-RPC and SOAP impose a strict framework. REST API supports different types of data formats and can be easily developed with almost all programming languages.

APIs play a significant role in web development because they allow programs to communicate easily and multiple users can have access to the program. It is also a source of income for the developers because users can be charged for using the API.


I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, HTML, CSS, JavaScript, AWS and Git. I also write technical articles where I explain web development and software engineering.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published.

Menu
Skip to toolbar