Skip to main content

Path variants, API versioning and per-version OpenAPI documentation for FastAPI routers

Project description

fastapi-router-variants

Latest Version MIT License Build Status Codecov semantic-release

Declare a route once and get path variants, API versioning and per-version OpenAPI documentation for FastAPI — for free.

fastapi-router-variants wraps APIRouter with a RouterWrapper that expands a single route declaration into every variant it should serve — multiple paths, multiple version prefixes (/v1/..., /v2/...), multiple mount prefixes, with deprecation handled automatically — then builds a separate OpenAPI schema per version and mounts Swagger UI / ReDoc / openapi.json for each of them.

Install

pip install fastapi-router-variants
# or
uv add fastapi-router-variants

Usage

from fastapi import FastAPI

from fastapi_router_variants import RouterDefaults, RouterWrapper


class ApiDefaults(RouterDefaults):
    prefix = "/api"           # mount every route under /api
    version = True            # force versioning on every route
    version_range = (1, 3)    # generate v1, v2, v3
    version_default = 3       # the version served on unversioned doc URLs


RouterWrapper.defaults = ApiDefaults()

router = RouterWrapper()


@router.get("/users/{user_id}")
def get_user(user_id: int) -> dict[str, int]:
    return {"id": user_id}


app = FastAPI()
app.include_router(router.base)

The single get declaration above registers GET /api/v1/users/{user_id}, GET /api/v2/users/{user_id} and GET /api/v3/users/{user_id}.

Path variants and flavors, versioning helpers, routing specs, per-version OpenAPI documents, custom categories and HTTP-error auto-documentation are all covered in the documentation.

Documentation

Full documentation is available at toilal.github.io/fastapi-router-variants. A preview of the in-progress develop branch is published at toilal.github.io/fastapi-router-variants/dev.

Requirements

  • Python ≥ 3.12
  • FastAPI ≥ 0.115

License

MIT © Rémi Alvergnat

CHANGELOG

v0.2.1 (2026-07-20)

Bug Fixes

  • Flatten lazily-mounted routers to avoid FastAPI >=0.139 memory regression (#15, 21db307)

v0.2.0 (2026-07-05)

Features

v0.1.0 (2026-07-05)

  • Initial Release

Changelog entries are generated automatically by python-semantic-release from Conventional Commits on release.

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_router_variants-0.2.1.tar.gz (154.1 kB view details)

Uploaded Source

Built Distribution

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

fastapi_router_variants-0.2.1-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_router_variants-0.2.1.tar.gz.

File metadata

  • Download URL: fastapi_router_variants-0.2.1.tar.gz
  • Upload date:
  • Size: 154.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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_router_variants-0.2.1.tar.gz
Algorithm Hash digest
SHA256 db2376553fcc0b902e5c499ab268e45567474133dbae4be3c911633c17be8b76
MD5 d44fc0f06c5bbc412528be0dcef615eb
BLAKE2b-256 3c22977a0c9de0d302e358a183e250a9c84b8a9fa7f04f87561aa1a9f37c12cb

See more details on using hashes here.

File details

Details for the file fastapi_router_variants-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_router_variants-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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_router_variants-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a14bc21e78ea1054ea4c99cf7c5e01fb1ff096fc94aded060236e5a61d309b28
MD5 f72e2c2f1294d9e5d44b8c94986a2560
BLAKE2b-256 88ee027c67537f1743a3ecc3c1c2a5564a7aa2e5b1ad573127eb4136b1751252

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