flask-openapi3

4.3.1 · active · verified Sat Apr 11

Flask-OpenAPI3 is a web API framework based on Flask, currently at version 4.3.1. It simplifies the generation of REST APIs and OpenAPI documentation (including Swagger UI, ReDoc, and RapiDoc) for Flask projects. It leverages Pydantic for data validation and schema definition, and is actively maintained with frequent updates.

Warnings

Install

Imports

Quickstart

This example initializes a Flask-OpenAPI3 application, defines a Pydantic model for query parameters, and registers a GET endpoint. The OpenAPI documentation will be automatically generated and accessible at `/openapi`.

from flask_openapi3 import Info, Tag, OpenAPI
from pydantic import BaseModel

info = Info(title='Book API', version='1.0.0')
app = OpenAPI(__name__, info=info)

book_tag = Tag(name='book', description='Book management operations')

class BookQuery(BaseModel):
    age: int
    author: str

@app.get('/book', tags=[book_tag], summary='Get books by query')
def get_book(query: BookQuery):
    """Retrieves a list of books based on age and author."""
    return {
        "code": 0,
        "message": "ok",
        "data": [
            {"bid": 1, "age": query.age, "author": query.author},
            {"bid": 2, "age": query.age, "author": query.author}
        ]
    }

if __name__ == '__main__':
    # Access OpenAPI docs at http://127.0.0.1:5000/openapi
    app.run(debug=True)

view raw JSON →