Flask-RESTful
Flask-RESTful is an extension for Flask that simplifies the creation of REST APIs by providing building blocks like Resources for organizing endpoints and `reqparse` for input validation. The current stable version is 0.3.10, released in May 2023. While still available, the project appears to be in maintenance mode with infrequent updates and hasn't seen major feature releases since 2014.
Warnings
- breaking Flask-RESTful may encounter breaking issues or unexpected behavior with Flask versions 2.3 and newer, specifically due to changes in its underlying `werkzeug` dependency. Some unit tests may fail, indicating potential compatibility problems.
- gotcha The `flask-restful` project has seen limited development and maintenance activity since 2014, despite a recent version bump. Users seeking more active development, modern features (like auto-generated documentation via Swagger UI), or better compatibility with the latest Python/Flask ecosystems might find it lacking.
- gotcha While Flask-RESTful supports dependency injection by allowing arguments to be passed to resource constructors via `add_resource()`, it does not provide a built-in, sophisticated dependency injection framework.
- gotcha The PyPI classifiers for `flask-restful` still list compatibility with Python 2.7, which is end-of-life and no longer officially supported by the Python community. Developing new applications with Python 2.7 is strongly discouraged.
Install
-
pip install flask-restful
Imports
- Api
from flask_restful import Api
- Resource
from flask_restful import Resource
- reqparse
from flask_restful import reqparse
Quickstart
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'message': 'Hello, World!'}
class Square(Resource):
def get(self, num):
return {'square': num**2}
api.add_resource(HelloWorld, '/')
api.add_resource(Square, '/square/<int:num>')
if __name__ == '__main__':
app.run(debug=True)