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

A library for building versioned APIs with FastAPI. Each API version is a separate FastAPI sub-application mounted under a common application. The library automatically inherits endpoints from older versions into newer ones and rebuilds each version's OpenAPI schema, so every version's /docs always shows its complete, current set of endpoints.

Documentation

Features

  • Endpoint inheritance between versions: declare an endpoint once and it is available in all subsequent versions.
  • Precise availability range control with the until parameter.
  • Redefining an endpoint in a newer version shadows the inherited one — both at runtime and in the OpenAPI schema.
  • HTTP and WebSocket endpoints are supported.
  • Versioning metadata (origin, until) is readable right inside the endpoint.
  • Several independent versioned APIs within one application.
  • The only runtime dependency is fastapi (versions from 0.95 up to the latest are supported).

Installation

pip install fastapi-easy-versioning

PyPI

Quick Start

Versioning is built from two pieces that only work together:

  • VersioningMiddleware — added to the application that mounts the versions;
  • versioning() — a dependency factory that marks an endpoint as versioned.
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 result:

  • /v1/only-v1 responds while /v2/only-v1 returns 404 — the endpoint is limited by until=1.
  • /v1/all-versions and /v2/all-versions both respond — the endpoint is declared in v1 and inherited into v2.
  • /v2/from-v2 responds while /v1/from-v2 returns 404 — the endpoint only appeared in v2.
  • /v1/docs and /v2/docs show exactly the endpoints available in the corresponding version.

How It Works

  1. Each version is a FastAPI(api_version=N) sub-application mounted under a common application: app.mount("/v1", app_v1). api_version must be an integer.
  2. On its first ASGI event (server startup or the first request) VersioningMiddleware builds the inheritance once: it copies the marked endpoints from older versions into newer ones and rebuilds each version's OpenAPI schema.
  3. Only endpoints with the versioning() dependency are inherited. An endpoint without it stays only in the version where it is declared.
  4. Every inheriting version receives its own copy of the route — changes in one version do not affect the others.

More details — the versioning dependency, middleware behavior, adding routes at runtime, FastAPI version compatibility and complete examples — are in the documentation.


Pre-commit uv Ruff ty 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.4.0.tar.gz (8.5 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.4.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_easy_versioning-0.4.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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}

File hashes

Hashes for fastapi_easy_versioning-0.4.0.tar.gz
Algorithm Hash digest
SHA256 623b95baad5f67607ba7364d0233c673d6dbd57e53ae8d42501ec94ec2f81f46
MD5 5af220487cfb685373b77a0b8068decd
BLAKE2b-256 fe1623f6ec71f9b7e1c8c3ddc9c9d705d8c66ad7dd881085ee4800c1470c9b34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_easy_versioning-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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}

File hashes

Hashes for fastapi_easy_versioning-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3569f022319027c3ccee0ee6ee33f85513c7fe0accfa99ec1033389fd4b9d6e9
MD5 7f14b17f7fc0ae6b37b42abebd93f21f
BLAKE2b-256 f67606bc0b272db721dbf7a9bfe3f474f8ee6d72363e131b93cf383a8c0e4088

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