Skip to main content

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.

🚀 Features

  • Static Analysis: Uses tree-sitter to 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}
  • 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

MIT License

Metadata

Release files for fast-router 0.3.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 fast-router 0.3.0
File Size Uploaded Provenance Actions
fast_router-0.3.0.tar.gz 16.8 kB Without provenance Details for fast_router-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fast-router 0.3.0
File Interpreter ABI Platform Actions
fast_router-0.3.0-py3-none-any.whl Python 3 none any Details for fast_router-0.3.0-py3-none-any.whl

Total release size: 27.2 kB

Release files / fast_router-0.3.0.tar.gz

Download URL fast_router-0.3.0.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bde956358bb667be858acc9221a35c24df4c4ece5fda7af62ade832c863fd43b
BLAKE2b-256 checksum
How to use checksums
03b562430087d53f32d41c261392b3937a7311fc0fa32e06fbb7e89761c69cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Release files / fast_router-0.3.0-py3-none-any.whl

Download URL fast_router-0.3.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ac3e2feed2f5ce2c9ee7858004b8040020d35ae29aae5004f0433cc43a33fd1
BLAKE2b-256 checksum
How to use checksums
0e8935d1514919dac11d22896347d9f8ccddb7a3f280dba8c595102dae7f60c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Security for fast-router 0.3.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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