Bigger Applications - Multiple Files
Learn to structure larger FastAPI applications using APIRouter, multiple files, and proper Python package organization.
šÆ What You'll Learn
- ā¢Understand how to structure larger FastAPI applications
- ā¢Learn to use APIRouter for modular routing
- ā¢Implement proper Python package structure with __init__.py files
- ā¢Use relative imports between modules
- ā¢Organize dependencies and routers across multiple files
Bigger Applications - Multiple Files
šÆ What You'll Learn
- How to structure larger FastAPI applications using multiple files
- Using
APIRouter
to organize routes into modules - Understanding Python package structure with
__init__.py
files - Implementing relative imports between modules
- Organizing dependencies and sharing them across routers
š Theory
When building real-world applications, putting everything in a single file becomes unwieldy. FastAPI provides excellent tools to organize your code into multiple files while maintaining all the flexibility and features you need.
Project Structure
The recommended structure for larger FastAPI applications follows Python package conventions:
app/
āāā __init__.py # Makes 'app' a Python package
āāā main.py # Main application entry point
āāā dependencies.py # Shared dependencies
āāā routers/ # Route modules
ā āāā __init__.py # Makes 'routers' a subpackage
ā āāā users.py # User-related routes
ā āāā items.py # Item-related routes
āāā internal/ # Internal modules
āāā __init__.py # Makes 'internal' a subpackage
āāā admin.py # Admin-only routes
Key Concepts
1. APIRouter
APIRouter
is like a "mini FastAPI" that you can use to organize related routes:
from fastapi import APIRouter
router = APIRouter()
@router.get("/users/")
async def get_users():
return {"users": []}
2. Router Configuration
You can configure routers with common settings:
router = APIRouter(
prefix="/items", # All routes get this prefix
tags=["items"], # OpenAPI tags for documentation
dependencies=[Depends(auth)], # Applied to all routes
responses={404: {"description": "Not found"}},
)
3. Including Routers
In your main application, include routers:
from fastapi import FastAPI
from .routers import users, items
app = FastAPI()
app.include_router(users.router)
app.include_router(items.router)
4. Relative Imports
Use relative imports to access modules within your package:
# From app/routers/items.py
from ..dependencies import get_token_header # Go up one level
from . import users # Same level
5. Dependencies Across Modules
Dependencies can be shared across the entire application or specific routers:
# Global dependencies (applied to all routes)
app = FastAPI(dependencies=[Depends(global_dep)])
# Router-specific dependencies
router = APIRouter(dependencies=[Depends(router_dep)])
# Route-specific dependencies
@router.get("/", dependencies=[Depends(route_dep)])
async def get_items():
pass
š§ Key Concepts
- APIRouter: Organizes related routes into modules
- Package Structure: Use
__init__.py
files to create Python packages - Relative Imports: Navigate between modules using
.
and..
syntax - Router Inclusion: Use
app.include_router()
to add routers to your main app - Dependency Sharing: Apply dependencies at app, router, or route level
š” Best Practices
- Group Related Routes: Put related functionality in the same router
- Use Prefixes: Apply common URL prefixes at the router level
- Share Dependencies: Define common dependencies once and reuse them
- Organize by Feature: Structure your code by business logic, not technical layers
- Keep Main App Simple: The main app should primarily orchestrate routers
š Additional Resources
š” Hint
Use APIRouter to create modular route handlers, then include them in your main FastAPI app with app.include_router(). Don't forget __init__.py files to make directories into Python packages!
Ready to Practice?
Now that you understand the theory, let's put it into practice with hands-on coding!
Start Interactive Lesson