Skip to main content

Fully featured framework for fast, easy and documented API development with Flask

Project description

Tests status Code coverage Documentation status License Supported Python versions Join the chat at https://gitter.im/python-restx Code style: black

Flask-RESTX is a community driven fork of Flask-RESTPlus.

Flask-RESTX is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTX encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTX should be easy to pick up. It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly using Swagger.

Compatibility

Flask-RESTX requires Python 3.9+.

On Flask Compatibility

Flask and Werkzeug moved to versions 2.0 in March 2020. This caused a breaking change in Flask-RESTX.

RESTX and Flask / Werkzeug Compatibility

Flask-RESTX version

Flask version

Note

<= 0.3.0

< 2.0.0

unpinned in Flask-RESTX. Pin your projects!

== 0.4.0

< 2.0.0

pinned in Flask-RESTX.

>= 0.5.0

< 3.0.0

unpinned, import statements wrapped for compatibility

== 1.2.0

< 3.0.0

pinned in Flask-RESTX.

>= 1.3.0

>= 2.0.0 (Flask >= 3.0.0 support)

unpinned, import statements wrapped for compatibility

trunk branch in Github

>= 2.0.0 (Flask >= 3.0.0 support)

unpinned, will address issues faster than releases.

Installation

You can install Flask-RESTX with pip:

$ pip install flask-restx

or with easy_install:

$ easy_install flask-restx

Quick start

With Flask-RESTX, you only import the api instance to route and document your endpoints.

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

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

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

todo = api.model('Todo', {
    'id': fields.Integer(readonly=True, description='The task unique identifier'),
    'task': fields.String(required=True, description='The task details')
})


class TodoDAO(object):
    def __init__(self):
        self.counter = 0
        self.todos = []

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

    def create(self, data):
        todo = data
        todo['id'] = self.counter = self.counter + 1
        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('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @ns.doc('list_todos')
    @ns.marshal_list_with(todo)
    def get(self):
        '''List all tasks'''
        return DAO.todos

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


@ns.route('/<int:id>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    @ns.doc('get_todo')
    @ns.marshal_with(todo)
    def get(self, id):
        '''Fetch a given resource'''
        return DAO.get(id)

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

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


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

Contributors

Flask-RESTX is brought to you by @python-restx. Since early 2019 @SteadBytes, @a-luna, @j5awry, @ziirish volunteered to help @python-restx keep the project up and running, they did so for a long time! Since the beginning of 2023, the project is maintained by @peter-doggart with help from @ziirish. Of course everyone is welcome to contribute and we will be happy to review your PR’s or answer to your issues.

Documentation

The documentation is hosted on Read the Docs

Contribution

Want to contribute! That’s awesome! Check out CONTRIBUTING.rst!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-restx-1.3.2.tar.gz (2.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_restx-1.3.2-py2.py3-none-any.whl (2.8 MB view details)

Uploaded Python 2Python 3

File details

Details for the file flask-restx-1.3.2.tar.gz.

File metadata

  • Download URL: flask-restx-1.3.2.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.12.1.2 readme-renderer/44.0 requests/2.32.5 requests-toolbelt/1.0.0 urllib3/2.5.0 tqdm/4.67.1 importlib-metadata/8.7.0 keyring/25.6.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.23

File hashes

Hashes for flask-restx-1.3.2.tar.gz
Algorithm Hash digest
SHA256 0ae13d77e7d7e4dce513970cfa9db45364aef210e99022de26d2b73eb4dbced5
MD5 3fe0cd948ae1a0ebcbca569daf069586
BLAKE2b-256 43899b9ca58cbb8e9ec46f4a510ba93878e0c88d518bf03c350e3b1b7ad85cbe

See more details on using hashes here.

File details

Details for the file flask_restx-1.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: flask_restx-1.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.12.1.2 readme-renderer/44.0 requests/2.32.5 requests-toolbelt/1.0.0 urllib3/2.5.0 tqdm/4.67.1 importlib-metadata/8.7.0 keyring/25.6.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.23

File hashes

Hashes for flask_restx-1.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6e035496e8223668044fc45bf769e526352fd648d9e159bd631d94fd645a687b
MD5 eb5c136f3b66aec74ef510ebb7bdc3ab
BLAKE2b-256 7a3fb82cd8e733a355db1abb8297afbf59ec972c00ef90bf8d4eed287958b204

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page