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.2.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.2.0
File Size Uploaded Provenance Actions
fastapi_dynamic_route_registration-0.2.0.tar.gz 14.7 kB With provenance Details for fastapi_dynamic_route_registration-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 24.7 kB

Release files / fastapi_dynamic_route_registration-0.2.0.tar.gz

Download URL fastapi_dynamic_route_registration-0.2.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0a896bab27b5a987dbbdd55dccab5fc518e0ae6b0f7723589929d3ad65f547cc
BLAKE2b-256 checksum
How to use checksums
821400be21ee936338274783cc9297c2cfce16c206fee6c710ad63992b250098
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 8, 2026.

Transparency log

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

Download URL fastapi_dynamic_route_registration-0.2.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd9dc6056e99f41a0c312cb33cb82be1f458e049f43821e19b864338068955fa
BLAKE2b-256 checksum
How to use checksums
c208725dcd4fed88051c63dbcdfa2f08adf3118078b44d24d3d3707cd3c752f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 8, 2026.

Transparency log

Security for fastapi-dynamic-route-registration 0.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_dynamic_route_registration-0.2.0.tar.gz

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 Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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