Skip to main content

Easy way to build versioning APIs by FastAPI

Project description

FastAPI Easy Versioning

PyPI - Python Version PyPI - Downloads GitHub Release GitHub Repo stars Test results Coverage Status Documentation

This is a solution for building versioned APIs automatically using FastAPI. It enables automatic inheritance of endpoints from previous FastAPI sub-applications into newer versions based on configuration, and correctly reflects them in the OpenAPI schema.

Documentation

Installation

pip install fastapi-easy-versioning

PyPI

Usage

To implement versioning, use the VersioningMiddleware and the dependency factory versioning.

Example:

from fastapi import FastAPI, Depends
from fastapi_easy_versioning import VersioningMiddleware, versioning

app = FastAPI()
app_v1 = FastAPI(api_version=1)
app_v2 = FastAPI(api_version=2)

app.mount("/v1", app_v1)
app.mount("/v2", app_v2)
app.add_middleware(VersioningMiddleware)

@app_v1.get('/only-v1', dependencies=[Depends(versioning(until=1))])
def only_v1() -> str:
    return "Available only in version v1"

@app_v1.get('/all-versions', dependencies=[Depends(versioning())])
def all_versions() -> str:
    return "Available in all versions starting from v1"

@app_v2.get('/from-v2', dependencies=[Depends(versioning())])
def from_v2() -> str:
    return "Available starting from v2 and in all future versions"

The endpoint /only-v1 is available only in version v1 at /v1/only-v1. The endpoint /from-v2 becomes available starting from version v2 at /v2/from-v2 and is automatically inherited in all subsequent versions. The endpoint /all-versions, defined in v1, is accessible at both /v1/all-versions and /v2/all-versions due to the inheritance mechanism.

Using the versioning dependency factory, you can specify the last version in which an endpoint remains available by setting the until parameter to a version number. If until is set to None or omitted, the endpoint will be available in the version it was declared and in all later versions.

To associate a sub-application with a specific version, use the api_version parameter when creating the FastAPI instance. It must be an integer. Sub-applications without the api_version parameter will be ignored during versioning processing.


Pre-commit uv Ruff Checked with mypy Semantic Versions

GitHub License

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

fastapi_easy_versioning-0.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_easy_versioning-0.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_easy_versioning-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8606c641a21767c9f2c1460ed15b19a56b7ef7ceac448027e1f0f8b72c89a354
MD5 45fd75845c638a8e93b5bb9bba659f66
BLAKE2b-256 cc306cf8c42a7de91ca5d469927183f18d8fdc432ff2bc00ee67b986d56ee816

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_easy_versioning-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bcd0db247845b0f032ff8e4e71db69eaa82d71b69ee7771f05fdbcea18cf04e6
MD5 ca73a457a63b8b87cf33b5cf4142a607
BLAKE2b-256 91164ebb4e06a34a85735e583ec4ec1b250c1121dedda36fad65a0e0ee4ceb22

See more details on using hashes here.

Supported by

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