Skip to main content

FastAPI Versionizer

    ## Credit
    This was inspired by [fastapi_versioning](https://github.com/DeanWay/fastapi-versioning).
    This project addresses issues with `fastapi_versioning` and adds some additional features.
    
    ## Installation
    `pip install fastapi-versionizer`
    
    ## Examples
    You can find examples in the [examples](https://github.com/alexschimpf/fastapi-versionizer/tree/main/examples) directory.
    
    ## Summary
    <b>FastAPI Versionizer</b> makes API versioning easy.
    
    Here is a simple (and rather contrived) example:
    
    ```python
    from typing import List
    from fastapi import FastAPI, APIRouter
    from pydantic import BaseModel
    
    from fastapi_versionizer.versionizer import Versionizer, api_version
    
    
    class User(BaseModel):
        id: int
        name: str
    
    
    class UserV2(BaseModel):
        id: int
        name: str
        age: int
    
    
    db = {
        'users': {}
    }
    app = FastAPI(
        title='test',
        redoc_url=None
    )
    users_router = APIRouter(
        prefix='/users',
        tags=['Users']
    )
    
    
    @app.get('/status', tags=['Status'])
    def get_status() -> str:
        return 'Ok'
    
    
    @api_version(1)
    @users_router.get('', deprecated=True)
    def get_users() -> List[User]:
        return list(user for user in db['users'].values() if isinstance(user, User))
    
    
    @api_version(1)
    @users_router.post('', deprecated=True)
    def create_user(user: User) -> User:
        db['users'][user.id] = user
        return user
    
    
    @api_version(2)
    @users_router.get('')
    def get_users_v2() -> List[UserV2]:
        return list(user for user in db['users'].values() if isinstance(user, UserV2))
    
    
    @api_version(2)
    @users_router.post('')
    def create_user_v2(user: UserV2) -> UserV2:
        db['users'][user.id] = user
        return user
    
    
    app.include_router(users_router)
    
    versions = Versionizer(
        app=app,
        prefix_format='/v{major}',
        semantic_version_format='{major}',
        latest_prefix='/latest',
        sort_routes=True
    ).versionize()
    ```
    
    This will generate the following endpoints:
    - <b>GET /openapi.json</b>
      - OpenAPI schema with endpoints from all versions
    - <b>GET /docs</b>
      - Swagger page with endpoints from all versions
    - <b>GET /v1/openapi.json</b>
      - OpenAPI schema for v1 endpoints
    - <b>GET /v1/docs</b>
      - Swagger page for v1 endpoints
    - <b>GET /v1/status</b>
    - <b>GET /v1/users</b>
    - <b>POST /v1/users</b>
    - <b>GET /v2/openapi.json</b>
      - OpenAPI schema for v2 endpoints
    - <b>GET /v2/docs</b>
      - Swagger page for v2 endpoints
    - <b>GET /v2/status</b>
      - This gets carried on from v1, where it was introduced, but has the same implementation
    - <b>GET /v2/users</b>
    - <b>POST /v2/users</b>
    - <b>GET /latest/openapi.json</b>
      - OpenAPI schema for latest (i.e. v2) endpoints
    - <b>GET /latest/docs</b>
      - Swagger page for latest (i.e. v2) endpoints
    - <b>GET /latest/status</b>
    - <b>GET /latest/users</b>
    - <b>POST /latest/users</b>
    
    ## Details
    <b>FastAPI Versionizer</b> works by modifying a FastAPI app in place, adding versioned routes and proper docs pages.
    Routes are annotated with version information, using the `@api_version` decorator.
    Using this decorator, you can specify the version (major and/or minor) that the route was introduced.
    You can also specify the first version when the route should be considered deprecated or even removed.
    Each new version will include all routes from previous versions that have not been overridden or marked for removal.
    An APIRouter will be created for each version, with the URL prefix defined by the `prefix_format` parameter described below,
    
    ## Versionizer Parameters
    - <b>app</b>
      - The FastAPI you want to version
    - <b>prefix_format</b>
      - Used to build the version path prefix for routes.
      - It should contain either "{major}" or "{minor}" or both.
      - Examples: "/v{major}", "/v{major}_{minor}"
    - <b>semantic_version_format</b>
      - Used to build the semantic version, which is shown in docs.
      - Examples: "{major}", "{major}.{minor}"
    - <b>default_version</b>
      - Default version used if a route is not annotated with @api_version.
    - <b>latest_prefix</b>
      - If this is given, the routes in your latest version will be a given a separate prefix alias.
      - For example, if latest_prefix='latest', latest version is 1, and you have routes: "GET /v1/a" and "POST /v1/b", then "GET /latest/a" and "POST /latest/b" will also be added.
    - <b>include_main_docs</b>
      - If True, docs page(s) will be created at the root, with all versioned routes included
    - <b>include_main_openapi_route</b>
      - If True, an OpenAPI route will be created at the root, with all versioned routes included
    - <b>include_version_docs</b>
      - If True, docs page(s) will be created for each version
    - <b>include_version_openapi_route</b>
      - If True, an OpenAPI route will be created for each version
    - <b>include_versions_route</b>
      - If True, a "GET /versions" route will be added, which includes information about all API versions
    - <b>sort_routes</b>
      - If True, all routes will be naturally sorted by path within each version.
      - If you have included the main docs page, the routes are sorted within each version, and versions are sorted from earliest to latest. If you have added a "latest" alias, its routes will be listed last.
    - <b>callback</b>
      - A function that is called each time a version router is created and all its routes have been added.
      - It is called before the router has been added to the root FastAPI app.
      - This function should not return anything and has the following parameters:
        - Version router
        - Version (in tuple form)
        - Version path prefix
    
    ## Docs Customization
    - There are various parameters mentioned above for controlling which docs page are generated.
    - The swagger and redoc URL paths can be controlled by setting your FastAPI app's `docs_url` and `redoc_url`.
      - If these are set to None, docs pages will not be generated.
    - Swagger UI parameters can be controlled by setting your FastAPI app's `swagger_ui_parameters`
    - If you want to customize your docs beyond what <b>FastAPI Versionizer</b> can handle, you can do the following:
      - Set `include_main_docs` and `include_version_docs` to False
      - Set `include_main_openapi_route` and `include_version_openapi_route` to False if you need to customize the OpenAPI schema.
      - Pass a `callback` param to `Versionizer` and add your own docs/OpenAPI routes manually for each version
      - If you want a "main" docs page, with all versioned routes included, you can manually add a docs/OpenAPI route to the versioned FastAPI app returned by `Versionizer.versionize()`.
    - See the [Docs Customization](https://github.com/alexschimpf/fastapi-versionizer/tree/main/examples/docs_customization.py) example for more details
    
    ## Gotchas
    
    ### Static file mounts
    
    If you need to [mount static files](https://fastapi.tiangolo.com/tutorial/static-files/), you'll have to add those to
    your FastAPI app **after** instantiating Versionizer. See the [Static file mount](https://github.com/alexschimpf/fastapi-versionizer/tree/main/examples/with_static_file_mount.py)
    example for more details.

Release files for fastapi-versionizer 4.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-versionizer 4.0.2
File Size Uploaded
fastapi_versionizer-4.0.2.tar.gz 10.2 kB Details

Release files / fastapi_versionizer-4.0.2.tar.gz

Download URL fastapi_versionizer-4.0.2.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0f3bd1a17d8be7bb9d830d19396e8235478bbe1f872ea314a8969868f5750ba4
BLAKE2b-256 checksum
How to use checksums
5b58ce437607e9c0127651171c06dbc95edfa3113e42ccfb123ddbc879dc203f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

4.0.2 This release

1 release file

4.0.1

1 release file

4.0.0

1 release file

3.0.4

1 release file

3.0.3

1 release file

3.0.2

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page