Flask-RESTX

1.3.2 · active · verified Fri Apr 10

Flask-RESTX is a powerful extension for Flask that adds support for quickly building REST APIs. It encourages best practices with minimal setup and provides automatic documentation via Swagger UI. As a community-driven fork of Flask-RESTPlus, it is actively maintained, currently at version 1.3.2, with a consistent release cadence addressing compatibility with newer Flask, Werkzeug, and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Flask-RESTX application with a single API, a namespace, and a resource for managing 'todo' items. It includes defining a data model, handling GET, POST, PUT, and DELETE operations, and leverages Flask-RESTX's automatic Swagger UI documentation features.

from flask import Flask
from flask_restx import Api, Resource, fields

app = Flask(__name__)
api = Api(app, version='1.0', title='Example API', description='A simple API example')

# Define a namespace
ns = api.namespace('todos', description='TODO operations')

# Define a model for request/response serialization
todo_model = ns.model('Todo', {
    'id': fields.Integer(readonly=True, description='The task unique identifier'),
    'task': fields.String(required=True, description='The task details')
})

# A simple in-memory data store
class TodoDAO:
    def __init__(self):
        self.counter = 0
        self.todos = []

    def get(self, id):
        for todo in self.todos:
            if todo['id'] == id:
                return todo
        ns.abort(404, "Todo {} doesn't exist".format(id))

    def create(self, data):
        todo = data
        self.counter += 1
        todo['id'] = self.counter
        self.todos.append(todo)
        return todo

    def update(self, id, data):
        todo = self.get(id)
        todo.update(data)
        return todo

    def delete(self, id):
        todo = self.get(id)
        self.todos.remove(todo)

DAO = TodoDAO()
DAO.create({'task': 'Build an API'})
DAO.create({'task': '?????'})
DAO.create({'task': 'profit!'})

@ns.route('/<int:id>')
@ns.param('id', 'The task identifier')
class Todo(Resource):
    @ns.doc('get_todo')
    @ns.marshal_with(todo_model)
    def get(self, id):
        '''Fetch a single todo item'''
        return DAO.get(id)

    @ns.doc('update_todo')
    @ns.expect(todo_model)
    @ns.marshal_with(todo_model)
    def put(self, id):
        '''Update a todo item given its identifier'''
        return DAO.update(id, api.payload)

    @ns.doc('delete_todo')
    @ns.response(204, 'Todo deleted')
    def delete(self, id):
        '''Delete a todo item given its identifier'''
        DAO.delete(id)
        return '', 204

@ns.route('/')
class TodoList(Resource):
    @ns.doc('list_todos')
    @ns.marshal_list_with(todo_model)
    def get(self):
        '''List all todo items'''
        return DAO.todos

    @ns.doc('create_todo')
    @ns.expect(todo_model)
    @ns.marshal_with(todo_model, code=201)
    def post(self):
        '''Create a new todo item'''
        return DAO.create(api.payload), 201


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

view raw JSON →