Routers and project structure
Routers and project structure
A single main.py is right for the first afternoon and wrong by the end of the week. Relay already has tickets, customers, a chat endpoint, and a health check, each with its own models and dependencies, and they do not belong in one file. FastAPI's unit of organisation is the APIRouter: a mini-application that holds a set of routes and is mounted into the real app with a prefix. The app file becomes a short assembly point, and each feature lives in its own module.
The second half of this lesson is the layout around the routers. A router should be thin: it parses the request, calls a service function, and shapes the response. The rules about tickets live in a service module that knows nothing about HTTP, which is what makes them testable and reusable from the agent later.
Continue reading
Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.