David's Blog

Building RESTful APIs with Flask-RESTful in Python

By David Li on 2024-07-01T14:01:55.000Z

Building RESTful APIs with Flask-RESTful in Python

Flask-RESTful is an extension for the Flask web framework that simplifies the process of building RESTful APIs. In this tutorial, we’ll explore how to build a simple RESTful API using Flask-RESTful in Python. By the end of this tutorial, you’ll have a basic understanding of how to create, retrieve, update, and delete (CRUD) resources using this library.

Prerequisites

Before we begin, you’ll need the following installed on your system:

  1. Python 3.6 or later
  2. Flask web framework
  3. Flask-RESTful extension

Installation

First, let’s create a virtual environment and install the necessary packages:

$ python3 -m venv env
$ source env/bin/activate
(env) $ pip install Flask Flask-RESTful

With the packages installed, we can now start building our API.

Creating a Simple RESTful API

Let’s build a simple API for managing a list of tasks. We’ll create endpoints for adding, retrieving, updating, and deleting tasks.

1. Setting up the project

Create a new file called app.py and import the necessary libraries:

from flask import Flask, request
from flask_restful import Resource, Api

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

Here, we’ve imported Flask and request from the Flask library and Resource and Api from the Flask-RESTful extension. We’ve also initialized a new Flask app and a new API instance.

2. Defining the Task resource

Now, let’s create a Task resource by subclassing Resource and implementing the necessary HTTP methods:

class Task(Resource):
    tasks = []

    def get(self, task_id):
        task = next((task for task in Task.tasks if task['id'] == task_id), None)
        if task is None:
            return {"error": "Task not found"}, 404
        return task

    def post(self):
        data = request.get_json()
        task = {"id": len(Task.tasks) + 1, "title": data["title"]}
        Task.tasks.append(task)
        return task, 201

    def put(self, task_id):
        data = request.get_json()
        task = next((task for task in Task.tasks if task['id'] == task_id), None)
        if task is None:
            return {"error": "Task not found"}, 404
        task.update({"title": data["title"]})
        return task

    def delete(self, task_id):
        task = next((task for task in Task.tasks if task['id'] == task_id), None)
        if task is None:
            return {"error": "Task not found"}, 404
        Task.tasks.remove(task)
        return {"result": "Task deleted"}

In this class, we’ve implemented the get, post, put, and delete methods to handle the respective HTTP methods for our Task resource. We’re also using a simple in-memory list called tasks to store our tasks.

3. Adding the Task resource to the API

Now that we have a Task resource, let’s add it to our API:

api.add_resource(Task, "/tasks/<int:task_id>")

This line maps the Task resource to the /tasks/<int:task_id> endpoint, where <int:task_id> is a dynamic URL parameter representing the task ID.

4. Running the API

Finally, let’s add the entry point for our application:

if __name__ == "__main__":
    app.run(debug=True)

This will start the Flask development server when we run the script.

Testing the API

With our API implemented, let’s test the endpoints using curl:

  1. POST: Create a new task

    $ curl -X POST -H "Content-Type: application/json" -d '{"title": "First task"}' http://localhost:5000/tasks/1
    ```
    
  2. GET: Retrieve a task by its ID

    $ curl -X GET http://localhost:5000/tasks/1
    ```
    
  3. PUT: Update a task

    $ curl -X PUT -H "Content-Type: application/json" -d '{"title": "Updated task"}' http://localhost:5000/tasks/1
    ```
    
  4. DELETE: Delete a task

    $ curl -X DELETE http://localhost:5000/tasks/1
    ```

Conclusion

In this tutorial, we’ve built a simple RESTful API using Flask-RESTful in Python. Flask-RESTful makesit easy to create, retrieve, update, and delete resources by providing a convenient Resource class and allowing you to define HTTP methods as class methods.

While our example used an in-memory list to store tasks, you can easily replace it with a persistent data store, such as a database, to build a more robust RESTful API.

Flask-RESTful also provides additional features, such as request parsing, input validation, and custom error messages, which can be used to further improve your API. To learn more about these features and how to use them, check out the official Flask-RESTful documentation.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-25.