Skip to main content

A lightweight Web API framework based on Flask and marshmallow-code projects.

Project description

APIFlask

Build status

APIFlask is a lightweight Python web API framework based on Flask and marshmallow-code projects. It's easy to use, high customizable, and 100% compatible with the Flask ecosystem. It starts as a fork of APIFairy and inspired by FastAPI.

With APIFlask, you will have:

  • More sugars for view function (@app.get(), @app.post(), @input(), @output(), @doc() and more)
  • Automatic request validation and serialization (with Webargs)
  • Automatic response validation and deserialization (with Marshmallow)
  • Automatic OpenAPI Specification (OAS, formerly Swagger Specification) document generation (with APISpec)
  • Automatic interactive API documentation (with Swagger UI and Redoc)
  • API Authorization support (with Flask-HTTPAuth)
  • Automatic JSON response for HTTP errors

Currently this project is in active development stage, bugs and break changes are expected. Welcome to leave any suggestions or feedbacks in this issue or just submit a pull request to improve it.

Requirements

  • Python 3.7+
  • Flask 1.1.0+

Installation

For Linux and macOS:

$ pip3 install apiflask

For Windows:

> pip install apiflask

Links

Example

from apiflask import APIFlask, input, output, Schema, api_abort
from apiflask.fields import Integer, String

app = APIFlask(__name__)

pets = [
    {
        'id': 0,
        'name': 'Kitty',
        'category': 'cat'
    },
    {
        'id': 1,
        'name': 'Coco',
        'category': 'dog'
    }
]


class PetSchema(Schema):
    id = Integer(required=True, dump_only=True)
    name = String(required=True)
    category = String(required=True)


@app.get('/pets/<int:pet_id>')
@output(PetSchema)
def get_pet(pet_id):
    if pet_id > len(pets):
        api_abort(404)
    return pets[pet_id]


@app.post('/pets/<int:pet_id>')
@input(PetSchema)
@output(PetSchema)
def update_pet(pet_id, pet):
    if pet_id > len(pets):
        api_abort(404)
    pet['id'] = pet_id
    pets[pet_id] = pet
    return pet

Save the file as app.py, then run it with:

$ flask run

Now visit the interactive API documentation (Swagger UI) at http://localhost:5000/docs:

Or you can visit the alternative API documentation (Redoc) at http://localhost:5000/redoc:

The auto-generated OpenAPI spec file is available at http://localhost:5000/openapi.json.

For a more complete example, see /examples.

Relationship with Flask

APIFlask is a thin wrapper on top of Flask, you only need to remember two differences:

  • When creating an application instance, use APIFlask instead of Flask.
  • When creating a blueprint instance, use APIBlueprint instead of Blueprint.

For a minimal Flask application:

from flask import Flask, request, escape

app = Flask(__name__)

@app.route('/')
def hello():
    name = request.args.get('name', 'Human')
    return f'Hello, {escape(name)}'

Now change to APIFlask:

from apiflask import APIFlask  # step one
from flask import request, escape

app = APIFlask(__name__)  # step two

@app.route('/')
def hello():
    name = request.args.get('name', 'Human')
    return f'Hello, {escape(name)}'

In a word, to make Web API development in Flask more easily, APIFlask provided APIFlask and APIBlueprint to extend Flask's Flask and Blueprint objects, it also shipped with some helpful utilities. Other than that, you are actually using Flask.

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

APIFlask-0.2.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

APIFlask-0.2.0-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file APIFlask-0.2.0.tar.gz.

File metadata

  • Download URL: APIFlask-0.2.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.0

File hashes

Hashes for APIFlask-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8d1815a827436e3db41879a049f57fe10b91a08cad8837a27f133e8ef5165025
MD5 3ed1433633aeea18665ba8f33ce57e11
BLAKE2b-256 b1b2bec54b63116abcf5815e0472eb7f6f101c8ea89fe8c07b1dc5b71d2a217f

See more details on using hashes here.

File details

Details for the file APIFlask-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: APIFlask-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.0

File hashes

Hashes for APIFlask-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 221e43e2d4404637d7063f889433cf4565ad9b7ffd1535f5d7a19745268bb11a
MD5 5bfb6a4f77556325986fd95cdd0c0792
BLAKE2b-256 f33e5bf67da5eb3d4abf57a2f14f322bf6d403e0a7fd6a9af6cce69175d3bd7f

See more details on using hashes here.

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