Header-based routing for API versioning in FastAPI
Project description
verselect
Header-based version routing for FastAPI.
verselect routes requests based on a date header (for example, X-API-VERSION: 2022-02-02). If a
client sends a version that does not exist, the request falls back to the closest lower version.
Unversioned routes are supported alongside versioned ones.
Русская версия: README.ru.md.
Highlights
- Header-driven routing with a "closest lower version" fallback
- Per-version OpenAPI schemas with a simple versions dashboard
- Works with standard FastAPI routers and middleware
Install
pip install verselect
Quickstart
from contextvars import ContextVar
from datetime import date
from fastapi import APIRouter
from starlette.responses import Response
from verselect import HeaderRoutingFastAPI
api_version_var: ContextVar[date] = ContextVar("api_version")
router = APIRouter(prefix="/v1")
webhook_router = APIRouter(prefix="/v1")
@router.get("/users")
def users():
return Response(f"Hello from {api_version_var.get()}", media_type="text/plain")
@webhook_router.post("/webhooks")
def webhooks():
return {"saved": True}
app = HeaderRoutingFastAPI(
api_version_header_name="X-API-VERSION",
api_version_var=api_version_var,
)
app.add_header_versioned_routers(router, header_value="2022-01-10")
app.add_header_versioned_routers(router, header_value="2022-02-11")
app.add_unversioned_routers(webhook_router)
Send a request with a version header:
curl -H "X-API-VERSION: 2022-02-11" http://localhost:8000/v1/users
Version rules
- Version format: ISO 8601 date (
YYYY-MM-DD). - Exact matches win.
- If there is no exact match, the closest lower version is selected.
- If the requested version is older than the oldest available, a
404is returned.
OpenAPI and docs
/openapi.json?version=YYYY-MM-DDreturns the schema for a specific version./docsshows a dashboard of all available versions and links to their docs.
Development (uv)
uv sync --dev
uv run ruff check .
uv run ruff format --check .
uv run mypy .
uv run pytest
More examples live in tests/_resources.
Project details
Release history Release notifications | RSS feed
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 verselect-0.3.0.tar.gz.
File metadata
- Download URL: verselect-0.3.0.tar.gz
- Upload date:
- Size: 71.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
299a787fd85e08a3e7232bc283c1ec6be2c0dc4e361406b1e0f7b220c9093453
|
|
| MD5 |
a108d267b380ca91b31ecd1a578f7809
|
|
| BLAKE2b-256 |
f0021965df608d00f8193be205d7ca37c2cc090d0c91b9e08dfab0b1560b1494
|
Provenance
The following attestation bundles were made for verselect-0.3.0.tar.gz:
Publisher:
publish.yaml on evstratbg/verselect
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
verselect-0.3.0.tar.gz -
Subject digest:
299a787fd85e08a3e7232bc283c1ec6be2c0dc4e361406b1e0f7b220c9093453 - Sigstore transparency entry: 789758609
- Sigstore integration time:
-
Permalink:
evstratbg/verselect@05495e9cf27f71e9ca6ff8814548cf1cf47d8e2f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/evstratbg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@05495e9cf27f71e9ca6ff8814548cf1cf47d8e2f -
Trigger Event:
push
-
Statement type:
File details
Details for the file verselect-0.3.0-py3-none-any.whl.
File metadata
- Download URL: verselect-0.3.0-py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfdf98c4fabf3ddf251dc791228610e170fa12686fa36c82de49ad1221c9096e
|
|
| MD5 |
0da9d9c7f39709d95bdbab0afea3b5f3
|
|
| BLAKE2b-256 |
b40c163c6621060d0d9df20878b44131a9013ab40000e3acb47478de20f6ad41
|
Provenance
The following attestation bundles were made for verselect-0.3.0-py3-none-any.whl:
Publisher:
publish.yaml on evstratbg/verselect
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
verselect-0.3.0-py3-none-any.whl -
Subject digest:
bfdf98c4fabf3ddf251dc791228610e170fa12686fa36c82de49ad1221c9096e - Sigstore transparency entry: 789758610
- Sigstore integration time:
-
Permalink:
evstratbg/verselect@05495e9cf27f71e9ca6ff8814548cf1cf47d8e2f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/evstratbg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@05495e9cf27f71e9ca6ff8814548cf1cf47d8e2f -
Trigger Event:
push
-
Statement type: