fastapi-file-routing
Nuxt/Nitro-style file-based routing for FastAPI. The layout of a routes/
directory becomes your URL structure; each file exposes plain get, post,
... functions.
uv add fastapi-file-routing # or: pip install fastapi-file-routing
Usage
# main.py
from fastapi import FastAPI
from fastapi_file_routing import add_file_routes
app = FastAPI()
add_file_routes(app, "routes")
routes/
├── index.py → /
├── users/
│ ├── index.py → /users
│ ├── me.py → /users/me
│ ├── {id}.py → /users/{id}
│ └── {path...}.py → /users/{path:path} (catch-all)
└── _utils.py → ignored ("_" prefix = private helper)
Each route file exposes module-level functions named after HTTP methods
(get, post, put, patch, delete, head, options), sync or async:
# routes/users/{id}.py
async def get(id: int):
return {"user": id}
def delete(id: int):
return {"deleted": id}
These are ordinary FastAPI endpoint functions: typing, Depends,
response models and OpenAPI docs work as usual. Static routes are
registered before dynamic ones (/users/me wins over /users/{id}),
and uvicorn main:app --reload picks up route file changes out of the box.
[!NOTE] Route files are loaded from their file path, not imported as a package: use absolute imports (
from myapp.db import ...), not relative ones (from .db import ...). See examples/basic for a working layout.
Middleware, auth and the rest
The library does one thing: map files to routes. Cross-cutting concerns use FastAPI's own mechanisms:
- global middleware:
app.add_middleware(...) - per-route logic:
Depends(...)in the handler signature - per-tree config:
add_file_routes(app, "routes", prefix="/api", tags=["v1"], dependencies=[...])(keyword arguments are forwarded toinclude_router)
Errors at startup
add_file_routes raises before the app serves a single request when:
- the directory does not exist (
FileNotFoundError) - a route file defines no HTTP handler (
ValueError) - two files map to the same method and path, for example
users.pyandusers/index.py(ValueError)
Development
uv sync
uv run pytest
uv run ruff check . && uv run ruff format --check . && uv run ty check
See CONTRIBUTING.md.
License
MIT
Release files for fastapi-file-routing 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_file_routing-0.1.0.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_file_routing-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / fastapi_file_routing-0.1.0.tar.gz
| Download URL | fastapi_file_routing-0.1.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
082efe3d813a3ce6dd37a8d1d770569b87de7f07bcdd5cbb8d2917c30a4c260d
|
|
BLAKE2b-256 checksum How to use checksums |
c3eab86c63badbb9f5714bfd7fa9097c6ac9dce56785cc0f90e55da52275ba10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","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_file_routing-0.1.0-py3-none-any.whl
| Download URL | fastapi_file_routing-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae4ce16e6cc8e9519ae77cd52832e194c073a580fc0158919e1bf429e4f04d95
|
|
BLAKE2b-256 checksum How to use checksums |
cb4c7224c93fda3f4355975541825a993c09e9e222d3b6426ffe6c4e1bd37cec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","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}
|