A simple and intuitive file-based router for FastAPI applications.
Project description
FastRouter
A simple and intuitive file-based router for FastAPI. This package allows you to define your API routes by structuring your files and directories, making your project more organized and easier to navigate.
[!NOTE] You can use this to fuck with your personal or a friends repo but do not push to production :)
🚀 Features
- Static Analysis: Uses
tree-sitterto discover routes without executing your code. - Lazy Loading: Route modules are only imported when the first request hits the endpoint.
- Side-Effect Isolation: Startup is silent. Top-level code in route files only runs on demand.
- Rich OpenAPI Integration:
- Automatic Summaries: The first line of your docstring becomes the route summary.
- Detailed Descriptions: The rest of the docstring becomes the route description.
- Tag Metadata: Configure directory-level documentation with
set_tag_metadata.
- Flexible Routing:
- Static:
index.py→/ - Dynamic:
[id].py→/{id} - Typed:
[id:int].py→/{id:int} - Slug:
[slug:].py→/{slug} - Catch-all:
[...path].py→/{path:path}
- Static:
- Full FastAPI Support: Works with
Depends(), Pydantic models, and all HTTP methods.
🛠️ Quick Start
Installation
We recommend using uv for dependency management.
uv add fast-router
Or using pip:
pip install fast-router
Basic Usage
1. Create a routes directory
In your project's root directory, create a folder named routes.
.
├── main.py
└── routes/
2. Define your routes
Create Python files inside the routes directory. For example, to create a "Hello World" endpoint at the root (/), create routes/index.py:
def get():
"""Welcome to FastRouter!"""
return {"message": "Hello World"}
3. Integrate with FastAPI
In your main.py, use the create_router helper:
import uvicorn
from fast_router import create_router
# Initialize the router
router = create_router("routes")
app = router.get_app()
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
Running the Server
Run your application using uv:
PYTHONPATH=src uv run main.py
Now visit http://127.0.0.1:8000/ to see your API in action, or http://127.0.0.1:8000/docs for the interactive documentation.
📂 Directory Structure
routes/
├── index.py # GET /
├── users/
│ ├── index.py # GET /users
│ └── [id:int].py # GET /users/{id}
├── blog/
│ └── [slug:].py # GET /blog/{slug}
└── files/
└── [...path].py # GET /files/{path:path}
📝 Route Handler Example
from fastapi import Query
def get(id: int, q: str = Query(None)):
"""
Get user by ID.
This description will appear in the expanded section of the
OpenAPI documentation, while the first line is the summary.
"""
return {"user_id": id, "query": q}
⚙️ Advanced Configuration
Tag Metadata
You can customize the documentation for each directory (tag) in your router:
from fast_router import create_router
router = create_router("routes")
router.set_tag_metadata(
"users",
description="Operations with users and their profiles.",
external_docs={"description": "User Guide", "url": "https://example.com/docs"}
)
app = router.get_app()
Smart Fallback
The router is lazy by default. However, if it detects complex FastAPI features (like Depends() or Pydantic models) that require runtime introspection, it automatically falls back to immediate loading for that specific route to ensure 100% compatibility.
🧪 Running Tests
We use pytest for unit and E2E testing, managed via uv.
make test
📜 License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fast_router-0.3.0.tar.gz.
File metadata
- Download URL: fast_router-0.3.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bde956358bb667be858acc9221a35c24df4c4ece5fda7af62ade832c863fd43b
|
|
| MD5 |
c121f1eef4dbee43865c9882dbebaf70
|
|
| BLAKE2b-256 |
03b562430087d53f32d41c261392b3937a7311fc0fa32e06fbb7e89761c69cb0
|
File details
Details for the file fast_router-0.3.0-py3-none-any.whl.
File metadata
- Download URL: fast_router-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ac3e2feed2f5ce2c9ee7858004b8040020d35ae29aae5004f0433cc43a33fd1
|
|
| MD5 |
e9d2636ff2a7aa8e95b53aac1b82b7e4
|
|
| BLAKE2b-256 |
0e8935d1514919dac11d22896347d9f8ccddb7a3f280dba8c595102dae7f60c5
|