Skip to main content

FastAPI Dynamic Router

FastAPI extension that provides automatic and dynamic route registration for FastAPI applications, inspired by Next.js's file-system based routing.

Installation

pip install fastapi-dynamic-router

How to Use

Basic Setup

from fastapi import FastAPI
from fastapi_dynamic_router import DynamicRouter

# Create FastAPI app
app = FastAPI()

# Initialize the router
router = DynamicRouter(app)

# Register routes from a directory
router.register_routes('routes')

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app)

Route Examples

  1. Basic Route (routes/root/__init__.py):
from fastapi import APIRouter

router = APIRouter()

@router.get('/')
def index():
    return {'message': 'Welcome to the API'}
  1. Dynamic Parameter (routes/users/[user_id]/__init__.py):
from fastapi import APIRouter

router = APIRouter()

@router.get('/')
def get_user(user_id: str):
    return {'message': f'User details for ID: {user_id}'}
  1. Nested Routes (routes/products/[productID]/reviews/__init__.py):
from fastapi import APIRouter

router = APIRouter()

@router.get('/')
def get_reviews(product_id: str):
    return {'message': f'Reviews for product ID: {product_id}'}

Configuration Options

# Make routes case-insensitive
app.state.dynamic_router_config['case_sensitive'] = False

# Add a global prefix to all routes
app.state.dynamic_router_config['prefix'] = '/api/v1'

Using with AI coding agents (Claude Code, Codex, Cursor, ...)

File-system routing is easy for agents to follow, as long as they know the convention. Two things help:

1. Tell your agent how routing works. Append the ready-made instructions to your project's CLAUDE.md / AGENTS.md:

python -m fastapi_dynamic_router agents >> CLAUDE.md

The agent then adds endpoints by creating folders, and never wires include_router into main.py by hand.

2. Give it a route map. Instead of searching the codebase for the handler behind a URL, the agent reads one file:

router.register_routes('routes', route_map='ROUTES.md')  # rewritten on every startup

or generate it without starting the app:

python -m fastapi_dynamic_router map routes -o ROUTES.md
| Method | Path | Handler | File |
|---|---|---|---|
| GET | `/` | `root` | `routes/root/__init__.py:6` |
| GET | `/users/{user_id}/` | `get_user` | `routes/users/[user_id]/__init__.py:6` |

The same data is available in code via router.get_route_map().

Directory Structure

The router follows a convention-based approach where your directory structure maps directly to URL routes:

📦 routes
├── 📂 Root
│   ├── 📄 __init__.py          ➜  🌐 /
│   ├── 📂 version
│   │   └── 📄 __init__.py      ➜  🌐 /version
│   └── 📂 about
│       └── 📄 __init__.py      ➜  🌐 /about
│
├── 📂 users
│   ├── 📄 __init__.py          ➜  🌐 /users
│   ├── 📂 [userID]             ➜  💫 Dynamic Parameter
│   │   ├── 📄 __init__.py      ➜  🌐 /users/{user_id}
│   │   └── 📂 profile
│   │       └── 📄 __init__.py  ➜  🌐 /users/{user_id}/profile
│   └── 📂 settings
│       └── 📄 __init__.py      ➜  🌐 /users/settings
│
└── 📂 products
    ├── 📄 __init__.py          ➜  🌐 /products
    └── 📂 [productID]          ➜  💫 Dynamic Parameter
        └── 📄 __init__.py      ➜  🌐 /products/{product_id}

Route Mapping Examples

Directory Structure Generated Route Type
Root/__init__.py / Static Route
users/__init__.py /users Static Route
users/[userID]/__init__.py /users/{user_id} Dynamic Route
Products/[productID]/__init__.py /products/{product_id} Dynamic Route

Dynamic Parameters

Dynamic parameters are defined using square brackets in directory names:

  • 📂 [userID] ➜ {user_id}
  • 📂 [productID] ➜ {product_id}
  • 📂 [categoryName] ➜ {category_name}

License

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

Metadata

Release files for fastapi-dynamic-router 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-router 0.2.0
File Size Uploaded
fastapi_dynamic_router-0.2.0.tar.gz 7.7 kB Details

Built distribution (wheel)

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

Total release size: 16.8 kB

Release files / fastapi_dynamic_router-0.2.0.tar.gz

Download URL fastapi_dynamic_router-0.2.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
44d04139a91985188c5bc707749e847c0f120429fb7eff8bd0cc9d1aface250d
BLAKE2b-256 checksum
How to use checksums
d39d26e2f6391bb93bad1e54452184ec9fe625409cd99bedad4b1a8ae3be9011
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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}

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

Download URL fastapi_dynamic_router-0.2.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09c3ec836037549a16de73e6582473dc5ecd14960e1781942195248fcd04b7d5
BLAKE2b-256 checksum
How to use checksums
442f7bbdf1122504598c1149a084f6607a01cae831e0eab7efde8d96d0820d17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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