Skip to main content

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 404 is returned.

OpenAPI and docs

  • /openapi.json?version=YYYY-MM-DD returns the schema for a specific version.
  • /docs shows 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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

verselect-0.3.0.tar.gz (71.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

verselect-0.3.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

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

Hashes for verselect-0.3.0.tar.gz
Algorithm Hash digest
SHA256 299a787fd85e08a3e7232bc283c1ec6be2c0dc4e361406b1e0f7b220c9093453
MD5 a108d267b380ca91b31ecd1a578f7809
BLAKE2b-256 f0021965df608d00f8193be205d7ca37c2cc090d0c91b9e08dfab0b1560b1494

See more details on using hashes here.

Provenance

The following attestation bundles were made for verselect-0.3.0.tar.gz:

Publisher: publish.yaml on evstratbg/verselect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for verselect-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfdf98c4fabf3ddf251dc791228610e170fa12686fa36c82de49ad1221c9096e
MD5 0da9d9c7f39709d95bdbab0afea3b5f3
BLAKE2b-256 b40c163c6621060d0d9df20878b44131a9013ab40000e3acb47478de20f6ad41

See more details on using hashes here.

Provenance

The following attestation bundles were made for verselect-0.3.0-py3-none-any.whl:

Publisher: publish.yaml on evstratbg/verselect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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