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 fields, Schema

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.8.4.tar.gz (2.3 MB view details)

Uploaded Source

Built Distribution

flask_apispec-0.8.4-py2.py3-none-any.whl (2.3 MB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: flask-apispec-0.8.4.tar.gz
  • Upload date:
  • Size: 2.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.7

File hashes

Hashes for flask-apispec-0.8.4.tar.gz
Algorithm Hash digest
SHA256 df6505fd04162881c04959094474bb2097d4992436a54f58e31ca79305f1f235
MD5 6ec7c69076aca19262bdd2f63be55407
BLAKE2b-256 0c5af88fb87f13f382f56d74b240575a3d2297a381e82357eeb977c8f72287d1

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: flask_apispec-0.8.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.7

File hashes

Hashes for flask_apispec-0.8.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 51647ab0b4491e7d84626f52a88e726599aa4de5c5bce0680e4d71edef6cd439
MD5 eec3a01a52963795e85ad86c97b61960
BLAKE2b-256 81adb251f8924efb52a3d20e232eff10e38c58d136f65c228a377a6522ee3916

See more details on using hashes here.

Provenance

Supported by

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