v0.1.1
Core Concepts
FastAPI Route is built on a small set of powerful concepts. Once you understand these, you'll be able to build complete APIs without ever writing a router decorator or registering a route manually.
File-based Routing
This is the foundation of FastAPI Route. Your file system becomes your router. Every folder and file you create automatically maps to a URL path.
routes/index.py→/routes/users/route.py→/usersroutes/users/profile/route.py→/users/profile
You never need to write @app.get() or app.include_router(). Just create files and folders.
Dynamic Routes
When you need to capture variable parts of a URL, use square bracket syntax [param] in your folder names. FastAPI Route automatically extracts the value and passes it to your handler.
routes/users/[user_id]/route.py→/users/{user_id}
The captured value becomes a parameter in your function, complete with automatic type conversion based on type hints.
Route Groups
Sometimes you want to organize your code without affecting the URL structure. Parentheses (group) let you create organizational folders that are ignored in the final URL.
routes/(auth)/login/route.py→/loginroutes/(api)/v1/users/route.py→/v1/users
This is perfect for separating public routes from authenticated routes, or organizing by feature without changing your API design.
HTTP Methods
Inside any route file, define functions named after HTTP methods. FastAPI Route routes the correct HTTP verb to the matching function automatically.
def GET(request):- Handles GET requestsdef POST(request):- Handles POST requestsdef PUT(request):- Handles PUT requestsdef PATCH(request):- Handles PATCH requestsdef DELETE(request):- Handles DELETE requests
One file can handle multiple HTTP methods. All of them share the same URL path.
Middleware
Create a middleware.py file in your project root to intercept every request before it reaches your route handlers. This is perfect for:
- Logging all incoming requests
- Checking authentication tokens
- Rate limiting by IP address
- Adding custom response headers
- Request validation
The middleware runs for every request, regardless of which route handles it.
Build Cache
For production deployments, FastAPI Route can pre-compile all your routes into an optimized cache. This eliminates filesystem scanning at startup and significantly reduces memory usage.
fastapi-route buildThe build process compresses your route metadata and stores it in the .cache/ directory. When you run fastapi-route run in production, routes load from this cache instantly.
Development Server
The development server (fastapi-route dev) gives you hot reload out of the box. Any change to your route files, configuration, or custom handlers triggers an automatic rebuild. You never need to restart the server manually during development.
Error Pages
FastAPI Route generates beautiful error pages for development. When something breaks, you see:
- The exact file and line number where the error occurred
- The problematic code with syntax highlighting
- Helpful suggestions for fixing the issue
- Full traceback filtered to show only user code
In production, these detailed error pages are disabled for security.
How Everything Works Together
Here's a complete example showing all concepts in one project:
my-api/
├── routes/
│ ├── index.py # GET /, POST /
│ ├── users/
│ │ ├── route.py # GET /users, POST /users
│ │ └── [user_id]/
│ │ └── route.py # GET /users/123, PUT /users/123, DELETE /users/123
│ └── (auth)/ # Route group (doesn't affect URL)
│ └── profile/
│ └── route.py # GET /profile
├── middleware.py # Runs on every request
└── config.py # Server and build configurationWhen you run fastapi-route dev, FastAPI Route:
- Loads your configuration from
config.py - Scans the
routes/folder and maps every file to a URL - Loads
middleware.pyto wrap every request - Starts the server with hot reload enabled
- Serves documentation at
/docsshowing all discovered routes
Next Steps
- File-based Routing - Learn the mapping rules in detail
- Dynamic Routes - Master path parameters and catch-all routes
- Route Groups - Organize code without changing URLs
- HTTP Methods - Explore all supported methods and the Request object
- Middleware - Add authentication, logging, and rate limiting