fastapi-easy-versioning
English · Русский
Versioned APIs for FastAPI — one sub-application per version, automatic inheritance of endpoints from older versions into newer ones, and an up-to-date OpenAPI schema for every version.
Once an API lives in several versions, routes have to be copied between them by hand: /v2 must serve everything /v1 served, minus what was deliberately dropped. The copies drift apart, /v1/docs and /v2/docs start lying, and "which versions is this endpoint even in?" becomes a question for git blame. This package makes the availability range a property of the endpoint: declare it once, mark it with a dependency, and it shows up in every later version by itself.
app = FastAPI()
app.add_middleware(VersioningMiddleware)
app.mount("/v1", app_v1)
app.mount("/v2", app_v2)
@app_v1.get("/items", dependencies=[Depends(versioning())])
def items() -> list[Item]:
return get_items()
Declared once in v1 — served at /v1/items and /v2/items, and present in both versions' Swagger. No duplicated route, no hand-maintained schema.
Install
pip install fastapi-easy-versioning
Requires Python 3.10+ and FastAPI ≥ 0.95 (0.137.0 and 0.137.1 are excluded). fastapi is the only runtime dependency.
How to use it
Versioning is built from two pieces that work only together.
1. Mount one sub-application per version and add the middleware. The middleware goes on the aggregating application — the one that directly mounts the versions, never on the versions themselves.
from fastapi import Depends, FastAPI
from fastapi_easy_versioning import VersioningMiddleware, versioning
app = FastAPI()
app_v1 = FastAPI(api_version=1) # the version number is an int; 0 is valid
app_v2 = FastAPI(api_version=2)
app.mount("/v1", app_v1)
app.mount("/v2", app_v2)
app.add_middleware(VersioningMiddleware)
2. Mark the endpoints. Marking is opt-in: an endpoint without the dependency stays in its own version only.
@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"
versioning() without until means "through the latest version", not "in this one only" — a common source of surprise. The dependency is accepted anywhere FastAPI accepts one, including APIRouter(dependencies=[...]) to version a whole router at once.
3. Read the version metadata where you need it. Injected into the signature, the same dependency yields the resolved range:
@app_v1.get("/where-am-i")
def where_am_i(version: Annotated[VersionInfo, Depends(versioning())]) -> str:
return f"Available from v{version.origin} through v{version.until}"
The result: /v1/only-v1 responds while /v2/only-v1 is a 404; /all-versions responds in both; /v2/from-v2 responds while /v1/from-v2 is a 404 — and each version's /docs shows exactly what that version serves.
Why it's cool
- Declare once, inherit forward — an endpoint declared in v1 is served by every later version, each with its own copy of the route.
- An exact availability range —
untilcaps the last version an endpoint lives in; several markers on one route resolve to the smallest. - Redefinition shadows — an endpoint of your own on the same path in a newer version replaces the inherited one, at runtime and in OpenAPI alike.
- Honest per-version docs — every version's OpenAPI schema is regenerated after inheritance, so Swagger never drifts from what is actually served.
- HTTP and WebSocket —
APIRouteandAPIWebSocketRouteare versioned with identical semantics, and shadowing is kind-aware. - Several independent APIs — public and private APIs in one application version separately, one middleware per aggregating app.
- Nothing but FastAPI — a single runtime dependency, fully typed (
py.typed), tested against FastAPI 0.95 through the latest on Python 3.10–3.14.
Documentation
📖 Full documentation — quickstart, the guide (dependency, middleware, recipes, limitations), runnable examples and an auto-generated API reference. Available in English and Russian.
License
MIT.
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.1.tar.gz.
File metadata
- Download URL: fastapi_easy_versioning-0.4.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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 |
44ddae877b4fe484ec3db9f1d86737e2e8f69d62bce0a42f563768d641d653ef
|
|
| MD5 |
69454324c2de58340b9a75d8277cd01a
|
|
| BLAKE2b-256 |
9d03e523037304f9db886ec5d03771378c1fe7fe5fda1a3083a8fe4ef37986dd
|
File details
Details for the file fastapi_easy_versioning-0.4.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_easy_versioning-0.4.1-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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 |
9ef8282d464c6bdf7e8f75eac4a13e36a20db6006570017dd70c22d2e572c02d
|
|
| MD5 |
f672e123baea6d6d2d29305dc0b4a25b
|
|
| BLAKE2b-256 |
4103d625ea7d86a869c2767492f515e969637f5b89e9d28722f27b78ca07843f
|