APIRouter & Multiple Files in Python
From the FastAPI cheat sheet · Project Structure · verified Jul 2026
APIRouter & Multiple Files
Split routes into separate files using APIRouter.
python
# app/routers/items.py
from fastapi import APIRouter
router = APIRouter(prefix="/items", tags=["items"])
@router.get("/")
async def read_items():
return []
# app/main.py
from fastapi import FastAPI
from app.routers import items, users
app = FastAPI()
app.include_router(items.router)
app.include_router(users.router)💡 APIRouter works exactly like FastAPI() — same decorators, same parameters
⚡ Use tags=["items"] to group endpoints under a section in the docs UI
📌 Set prefix on the router to avoid repeating "/items" on every route
🟢 Add dependencies at include_router() level to protect entire route groups
routerstructureorganization