Reference¶
VersionT, in the signatures on this page, is the version type of the format in use: tuple[int, int] for SemVer, str for CalVer.
RouterVersioner
¶
RouterVersioner(
app,
routers,
version_format=SEMVER,
prefix_format=None,
semantic_version_format=None,
default_version=None,
latest_prefix=None,
include_version_docs=True,
include_version_openapi_route=True,
include_versions_route=False,
versions_route_path="/versions",
include_versions_dashboard=False,
versions_dashboard_path="/dashboard",
versions_dashboard_hook=None,
deprecation_headers=False,
version_info=None,
sort_routes=False,
callback=None,
webhook_routers=None,
openapi_hook=None,
swagger_js_url=None,
swagger_css_url=None,
swagger_favicon_url=None,
redoc_js_url=None,
redoc_favicon_url=None,
redoc_with_google_fonts=True,
)
Versions a FastAPI app in place: one router per active version, each mounted under its own prefix with its own OpenAPI schema and documentation pages.
The configuration is validated here, in __init__; nothing is mounted until versionize()
is called, and it may only be called once.
| PARAMETER | DESCRIPTION |
|---|---|
app
|
The main FastAPI application instance.
TYPE:
|
routers
|
A single
TYPE:
|
version_format
|
The versioning strategy, which fixes the type every version must have. For
TYPE:
|
prefix_format
|
Template for each version's URL prefix; supports
TYPE:
|
semantic_version_format
|
Template for the version label in the Swagger/ReDoc titles; same placeholders. Defaults to
TYPE:
|
default_version
|
Version given to routes that are not decorated with
TYPE:
|
latest_prefix
|
If set (for example
TYPE:
|
include_version_docs
|
Create a Swagger UI and a ReDoc page for each version.
TYPE:
|
include_version_openapi_route
|
Create a separate
TYPE:
|
include_versions_route
|
Add a
TYPE:
|
versions_route_path
|
Path of that endpoint; must start with
TYPE:
|
include_versions_dashboard
|
Add an HTML page listing every active version with links to its docs (and to its
TYPE:
|
versions_dashboard_path
|
Path of that page; must start with
TYPE:
|
versions_dashboard_hook
|
Renderer replacing the built-in dashboard page. Receives the aggregated version models (the same dicts the versions endpoint returns) and the request
TYPE:
|
deprecation_headers
|
If
TYPE:
|
version_info
|
Mapping of version to its
TYPE:
|
sort_routes
|
Sort all routes alphabetically by path.
TYPE:
|
callback
|
Called with
TYPE:
|
webhook_routers
|
A single
TYPE:
|
openapi_hook
|
Applied to the OpenAPI schema generated for each version. Receives the schema dict and the version, and must return the (possibly modified) schema. Use it to add extensions, logos or version-specific metadata that the per-version schema generation would otherwise bypass.
TYPE:
|
swagger_js_url
|
URL of the Swagger UI JS bundle. Defaults to FastAPI's CDN URL.
TYPE:
|
swagger_css_url
|
URL of the Swagger UI CSS. Defaults to FastAPI's CDN URL.
TYPE:
|
swagger_favicon_url
|
URL of the Swagger UI favicon. Defaults to FastAPI's favicon.
TYPE:
|
redoc_js_url
|
URL of the ReDoc JS bundle. Defaults to FastAPI's CDN URL.
TYPE:
|
redoc_favicon_url
|
URL of the ReDoc favicon. Defaults to FastAPI's favicon.
TYPE:
|
redoc_with_google_fonts
|
If
TYPE:
|
Source code in fastapi_router_versioning/versioner.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
versionize
¶
Reads the configured routers, groups their routes by version, and mounts one versioned router per active version on the app. Returns the versions that were actually mounted.
May only be called once per instance: a second call raises RuntimeError. If it
raises, the app failed to load: don't catch the exception and retry, since some
versions may already be mounted and this package makes no attempt to undo that.
Raises ValueError if a route's @api_version does not match the configured
version_format, or if version_info dates a route's remove_in before its
deprecate_in while deprecation_headers is on. Raises RuntimeError if a
prefix is already taken, by this instance or by another RouterVersioner on the same
app. Raises TypeError if a router holds a route that is neither an HTTP nor a
WebSocket route.
Source code in fastapi_router_versioning/versioner.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
api_version
¶
Annotate an endpoint with the version that introduces it, and optionally the versions that deprecate and remove it.
The values are set as attributes on the decorated function, which is returned unchanged: there is no wrapper, so the signature FastAPI introspects stays the original one.
Raises TypeError at decoration time if an argument is neither a tuple nor a str.
Whether the type matches the configured VersionFormat is checked later, by
RouterVersioner.
| PARAMETER | DESCRIPTION |
|---|---|
version
|
The version this route first appears in:
TYPE:
|
deprecate_in
|
From this version on the route is flagged
TYPE:
|
remove_in
|
The first version that no longer serves the route.
TYPE:
|
Source code in fastapi_router_versioning/_versions.py
VersionInfo
dataclass
¶
Per-version metadata for RouterVersioner(version_info=...). Both fields are optional;
only the ones you set have an effect. Nothing here changes routing.
release_date
class-attribute
instance-attribute
¶
The calendar date this version goes live (datetime.date or datetime; a date is read as midnight UTC, an aware datetime keeps its own offset and a naive one is read as UTC, so a naive datetime from a non-UTC local clock shifts the value clients see). Used only with deprecation_headers=True: on a route whose deprecate_in is this version it becomes the Deprecation header (RFC 9745); on a route whose remove_in is this version, the Sunset header (RFC 8594).
guide
class-attribute
instance-attribute
¶
URL of this version's upgrade guide. With deprecation_headers=True it is emitted as Link: <guide>; rel="deprecation" (RFC 9745) on routes deprecated at this version; it is also listed for the version on the versions endpoint and the dashboard, regardless of deprecation_headers.
VersionFormat
¶
Bases: str, Enum
The versioning strategy a RouterVersioner enforces, and with it the type every version
must have: SEMVER takes tuple[int, int] (e.g. (1, 0)), CALVER takes str
(e.g. "2025-01-01", "v1").