Lesson 41 of 45 ยท FastAPI Basics
Bigger Applications - Multiple Files
Learn to structure larger FastAPI applications using APIRouter, multiple files, and proper Python package organization.
Open this lesson in the editor โ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
APIRouterto organize routes into modules - Understanding Python package structure with
__init__.pyfiles - 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__.pyfiles 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!
Now write it
The editor runs a real FastAPI server and grades your code against this lesson's own endpoints.
Start Bigger Applications - Multiple Files