Skip to main content

FastAPI dynamic route registration

PyPI - License PyPI - Version

A library to help dynamically register routes in FastAPI applications.

It is the FastAPI successor to flask-dynamic-route-registration: endpoint modules decorate their functions with @register_route, and the application mounts whole modules from a configuration map — with per-router prefixes and shared values injected as parameter defaults.

pip install fastapi-dynamic-route-registration

How to Use

Basic Setup

from fastapi import FastAPI
from fastapi_dynamic_route_registration import register_router

app = FastAPI()

register_router(app, "healthcheck", "", {})

Inside the healthcheck/__init__.py file

from fastapi_dynamic_route_registration import register_route


@register_route("/health")
def health() -> dict:
    return {"status": "OK"}

Reusing a module with different parameters, conditional routes

api_versions = [
    {"router_kwargs": {"prefix": "/api/1"}, "params": {"version": 1}},
    {"router_kwargs": {"prefix": "/api/2"}, "params": {"version": 2}},
]

register_router(app, "api", api_versions, {})

Inside the api/__init__.py file

from fastapi_dynamic_route_registration import register_route


@register_route("/status")
def status(*, version: int = 1) -> dict:
    return {"status": "OK", "version": version}


@register_route("/foo", enabled=lambda **params: params.get("version", 1) >= 2)
def foo_a() -> dict:
    return {"hello": "world"}


@register_route("/foo/{subject}", enabled=lambda **params: params.get("version", 1) >= 2)
def foo_b(subject: str) -> dict:
    return {"hello": subject}

This registers four routes:

URL Return value Function
/api/1/status {"status": "OK", "version": 1} status
/api/2/status {"status": "OK", "version": 2} status
/api/2/foo {"hello": "world"} foo_a
/api/2/foo/{subject} {"hello": subject} foo_b

Values from params (and from the route_kwargs passed to register_router) become the default value of the parameter with the same name. FastAPI still sees that parameter: a plain version: int = 1 stays a query parameter, whose default is now the injected value. Keys a function does not declare are dropped — unless it accepts **kwargs, in which case they are passed on every call while **kwargs itself stays hidden from FastAPI. One shared mapping (e.g. an application context object) can therefore be broadcast to every route.

Other route options

register_route forwards extra keyword arguments to APIRouter.add_api_route:

@register_route("/items", methods=["POST"], status_code=201, tags=["items"])
def create_item(item: Item) -> Item:
    return item

Registering multiple modules at once

register_routers (plural) registers several modules under a shared parent router:

from fastapi import FastAPI
from fastapi_dynamic_route_registration import register_routers

app = FastAPI()

register_routers(
    app,
    "myapp.api",
    {
        "healthcheck": "",
        "users": {"router_kwargs": {"prefix": "/users"}},
    },
    route_kwargs={},
    prefix="/v1",
)

Each value accepts the same shapes as register_router: a prefix string, a dict with router_kwargs (passed to APIRouter) and params, or a list of those. A configuration map like this is easy to keep in a JSON file per application.

Differences from the Flask version

  • Path parameters use FastAPI syntax: {param}, not <param>.
  • router_kwargs replaces blueprint_kwargs; a Flask-style url_prefix key is still accepted and mapped to prefix.
  • One function registers one route.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for fastapi-dynamic-route-registration 0.1.0

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-dynamic-route-registration 0.1.0
File Size Uploaded
fastapi_dynamic_route_registration-0.1.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-dynamic-route-registration 0.1.0
File Interpreter ABI Platform
fastapi_dynamic_route_registration-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.5 kB

Release files / fastapi_dynamic_route_registration-0.1.0.tar.gz

Download URL fastapi_dynamic_route_registration-0.1.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
581ce5863a822968e9c77f7cc9cc7e22abc254d50adea166bd6b638b39e3f5a6
BLAKE2b-256 checksum
How to use checksums
b16167c880825520b128014ab8a0a763d96588a2988ce3141d0ace860497c21c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 3, 2026.

Transparency log

Release files / fastapi_dynamic_route_registration-0.1.0-py3-none-any.whl

Download URL fastapi_dynamic_route_registration-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bae49138a7a19dc0980192b01e41ade373d520dfa4fac58a919e091fa9333ac
BLAKE2b-256 checksum
How to use checksums
0670bbbbb56eff694d55e6fa033cbcc8228efebba56e91e824b61bf1c3f63edc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 3, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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