Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

A simple API framework based on Flask.

test pypi pypistats pypi versions

Flask OpenAPI is a simple web API framework based on Flask. It uses Pydantic to verify data and automatic generation of interaction documentation.

The key features are:

Requirements

Python 3.11+

flask-openapi is dependent on the following libraries:

Installation

pip install -U flask-openapi[swagger]
Optional dependencies

To install these dependencies with flask-openapi:

pip install flask-openapi[yaml]
# or
pip install flask-openapi[async]
# or
pip install flask-openapi[dotenv]
# or
pip install flask-openapi[email]
# or all
pip install flask-openapi[yaml,async,dotenv,email]
# or manually
pip install pyyaml asgiref python-dotenv email-validator
# OpenAPI UI plugins
pip install -U flask-openapi[swagger,redoc,rapidoc,rapipdf,scalar,elements]

A Simple Example

Here's a simple example, further go to the Example.

from pydantic import BaseModel

from flask_openapi import Info, Tag
from flask_openapi import OpenAPI

info = Info(title="book API", version="1.0.0")
app = OpenAPI(__name__, info=info)

book_tag = Tag(name="book", description="Some Book")


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


@app.get("/book", summary="get books", tags=[book_tag])
def get_book(query: BookQuery):
    """
    to get all books
    """
    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__":
    app.run(debug=True)
Class-based API View Example
from pydantic import BaseModel, Field

from flask_openapi import OpenAPI, Tag, Info, APIView


info = Info(title="book API", version="1.0.0")
app = OpenAPI(__name__, info=info)

api_view = APIView(url_prefix="/api/v1", view_tags=[Tag(name="book")])


class BookPath(BaseModel):
    id: int = Field(..., description="book ID")


class BookQuery(BaseModel):
    age: int | None = Field(None, description="Age")


class BookBody(BaseModel):
    age: int | None = Field(..., ge=2, le=4, description="Age")
    author: str = Field(None, min_length=2, max_length=4, description="Author")


@api_view.route("/book")
class BookListAPIView:
    a = 1

    @api_view.doc(summary="get book list")
    def get(self, query: BookQuery):
        print(self.a)
        return query.model_dump_json()

    @api_view.doc(summary="create book")
    def post(self, body: BookBody):
        """description for a created book"""
        return body.model_dump_json()


@api_view.route("/book/<id>")
class BookAPIView:
    @api_view.doc(summary="get book")
    def get(self, path: BookPath):
        print(path)
        return "get"

    @api_view.doc(summary="update book")
    def put(self, path: BookPath):
        print(path)
        return "put"

    @api_view.doc(summary="delete book", deprecated=True)
    def delete(self, path: BookPath):
        print(path)
        return "delete"


app.register_api_view(api_view)

if __name__ == "__main__":
    app.run(debug=True)

API Document

Run the simple example, and go to http://127.0.0.1:5000/openapi.

OpenAPI UI plugins are optional dependencies that require manual installation.

pip install -U flask-openapi[swagger,redoc,rapidoc,rapipdf,scalar,elements]

More optional ui templates goto the document about UI_Templates.

openapi

Metadata

Release files for flask-openapi 5.0.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-openapi 5.0.0rc2
File Size Uploaded
flask_openapi-5.0.0rc2.tar.gz 587.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-openapi 5.0.0rc2
File Interpreter ABI Platform
flask_openapi-5.0.0rc2-py3-none-any.whl Python 3 none any Details

Total release size: 630.0 kB

Release files / flask_openapi-5.0.0rc2.tar.gz

Download URL flask_openapi-5.0.0rc2.tar.gz
Size 587.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c2414649c10ea88b722da4574cdb8b06882a7be2e2094d82253848c3f859b23d
BLAKE2b-256 checksum
How to use checksums
edab626564a92926de5b598ce6cbce2b6de25c2690cd2d053169e9dd8ff0b9e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / flask_openapi-5.0.0rc2-py3-none-any.whl

Download URL flask_openapi-5.0.0rc2-py3-none-any.whl
Size 42.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
384bfea8f778a37df854d0261a126d248be1a442b08e040201397cb60728843e
BLAKE2b-256 checksum
How to use checksums
8b3fac5b098e90d9303e2c7c394f01b043d153cbe114a66fee45a5805981920d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page