Skip to main content

Build and document REST APIs with Flask and apispec

Project description

Latest version Documentation status Travis-CI Code coverage

flask-apispec is a lightweight tool for building REST APIs in Flask. flask-apispec uses webargs for request parsing, marshmallow for response formatting, and apispec to automatically generate Swagger markup. You can use flask-apispec with vanilla Flask or a fuller-featured framework like Flask-RESTful.

Install

pip install flask-apispec

Quickstart

from flask import Flask
from flask_apispec import use_kwargs, marshal_with

from marshmallow import Schema
from webargs import fields

from .models import Pet

app = Flask(__name__)

class PetSchema(Schema):
    class Meta:
        fields = ('name', 'category', 'size')

@app.route('/pets')
@use_kwargs({'category': fields.Str(), 'size': fields.Str()})
@marshal_with(PetSchema(many=True))
def get_pets(**kwargs):
    return Pet.query.filter_by(**kwargs)

flask-apispec works with function- and class-based views:

from flask import make_response
from flask_apispec.views import MethodResource

class PetResource(MethodResource):

    @marshal_with(PetSchema)
    def get(self, pet_id):
        return Pet.query.filter(Pet.id == pet_id).one()

    @use_kwargs(PetSchema)
    @marshal_with(PetSchema, code=201)
    def post(self, **kwargs):
        return Pet(**kwargs)

    @use_kwargs(PetSchema)
    @marshal_with(PetSchema)
    def put(self, pet_id, **kwargs):
        pet = Pet.query.filter(Pet.id == pet_id).one()
        pet.__dict__.update(**kwargs)
        return pet

    @marshal_with(None, code=204)
    def delete(self, pet_id):
        pet = Pet.query.filter(Pet.id == pet_id).one()
        pet.delete()
        return make_response('', 204)

flask-apispec generates Swagger markup for your view functions and classes. By default, Swagger JSON is served at /swagger/, and Swagger-UI at /swagger-ui/.

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec.extension import FlaskApiSpec

app.config.update({
    'APISPEC_SPEC': APISpec(
        title='pets',
        version='v1',
        plugins=[MarshmallowPlugin()],
    ),
    'APISPEC_SWAGGER_URL': '/swagger/',
})
docs = FlaskApiSpec(app)

docs.register(get_pets)
docs.register(PetResource)

Documentation

https://flask-apispec.readthedocs.io/

Notes

flask-apispec is strongly inspired by Flask-RESTful and Flask-RESTplus, but attempts to provide similar functionality with greater flexibility and less code.

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-apispec-0.11.2.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

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

flask_apispec-0.11.2-py2.py3-none-any.whl (28.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flask-apispec-0.11.2.tar.gz.

File metadata

  • Download URL: flask-apispec-0.11.2.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/26.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.48.2 importlib-metadata/4.11.3 keyring/21.3.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.5

File hashes

Hashes for flask-apispec-0.11.2.tar.gz
Algorithm Hash digest
SHA256 aaa0c002281b7c0aeee30a8e98949f68ffd3550d31eea49271b73f0f1769ac01
MD5 64c43040e5aeec6103c1d1c6be58f4b4
BLAKE2b-256 fc5112e37093d24070ced27ad91a90d4323965b2503963336a803a7c3b606197

See more details on using hashes here.

File details

Details for the file flask_apispec-0.11.2-py2.py3-none-any.whl.

File metadata

  • Download URL: flask_apispec-0.11.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/26.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.48.2 importlib-metadata/4.11.3 keyring/21.3.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.5

File hashes

Hashes for flask_apispec-0.11.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 faae4bba1b0be315ff7e112ce0061e8efc8696e099d0923ce64a593c0184de51
MD5 896749336d4ab3664977247dd7b38126
BLAKE2b-256 2b8f14663a738016246676eb3a938c369398ae3fd4d7cc18b20c52f88efa07ff

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