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 (@input(), @output(), @app.get(), @app.post() and more)
  • Automatic request validation and deserialization (with Webargs)
  • Automatic response formatting and serialization (with Marshmallow)
  • Automatic OpenAPI Specification (OAS, formerly Swagger Specification) document generation (with APISpec)
  • Automatic interactive API documentation (with Swagger UI and Redoc)
  • API authentication 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. Thank you!

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, Schema, input, output, abort_json
from apiflask.fields import Integer, String
from apiflask.validators import Length, OneOf

app = APIFlask(__name__)

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


class PetInSchema(Schema):
    name = String(required=True, validate=Length(0, 10))
    category = String(required=True, validate=OneOf(['dog', 'cat']))


class PetOutSchema(Schema):
    id = Integer()
    name = String()
    category = String()


@app.get('/pets/<int:pet_id>')
@output(PetSchema)
def get_pet(pet_id):
    if pet_id > len(pets) - 1:
        abort_json(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) - 1:
        abort_json(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.

Files for APIFlask, version 0.3.0
Filename, size File type Python version Upload date Hashes
Filename, size APIFlask-0.3.0-py3-none-any.whl (36.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size APIFlask-0.3.0.tar.gz (48.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page