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.1.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.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_easy_versioning-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c9a869e79b3a5b894425f52266904a7e3961892bdcc61302f9663bdff8c40bc
MD5 b579bd70477f4c5058f5664319d5da7c
BLAKE2b-256 402e19b4ea71506a7fe8e0d66a0cd15756183794e792e31af614b3155fb25c51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_easy_versioning-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed9efac6e16ca0492d1962d76ff252963b0c6d795084e1ac907d6c8d3db1f721
MD5 a8c110acdd2b606e3011c7dac074b258
BLAKE2b-256 b7c26788b1474e967b0e921c9203feb440800e86a18e8f691487571e7739fed0

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