Easy way to build versioning APIs by FastAPI
Project description
FastAPI Easy Versioning
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.
Features
- Endpoint inheritance between versions: declare an endpoint once and it is available in all subsequent versions.
- Precise availability range control with the
untilparameter. - 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
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-v1responds while/v2/only-v1returns 404 — the endpoint is limited byuntil=1./v1/all-versionsand/v2/all-versionsboth respond — the endpoint is declared inv1and inherited intov2./v2/from-v2responds while/v1/from-v2returns 404 — the endpoint only appeared inv2./v1/docsand/v2/docsshow exactly the endpoints available in the corresponding version.
How It Works
- Each version is a
FastAPI(api_version=N)sub-application mounted under a common application:app.mount("/v1", app_v1).api_versionmust be an integer. - On its first ASGI event (server startup or the first request)
VersioningMiddlewarebuilds the inheritance once: it copies the marked endpoints from older versions into newer ones and rebuilds each version's OpenAPI schema. - Only endpoints with the
versioning()dependency are inherited. An endpoint without it stays only in the version where it is declared. - 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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
623b95baad5f67607ba7364d0233c673d6dbd57e53ae8d42501ec94ec2f81f46
|
|
| MD5 |
5af220487cfb685373b77a0b8068decd
|
|
| BLAKE2b-256 |
fe1623f6ec71f9b7e1c8c3ddc9c9d705d8c66ad7dd881085ee4800c1470c9b34
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3569f022319027c3ccee0ee6ee33f85513c7fe0accfa99ec1033389fd4b9d6e9
|
|
| MD5 |
7f14b17f7fc0ae6b37b42abebd93f21f
|
|
| BLAKE2b-256 |
f67606bc0b272db721dbf7a9bfe3f474f8ee6d72363e131b93cf383a8c0e4088
|