Sitemap
Utkarsh Tiwari

Utkarsh Tiwari

Member-only story

Python’s — Swagger — FastAPI

3 min readSep 25, 2026

--

How /docs (Swagger UI) Gets Generated

Before getting into it, how Swagger UI gets generated, please have a look of project structure which I am using.

Press enter or click to view image in full size
Application Structure

Nobody wrote a Swagger page for this project. It’s assembled automatically by FastAPI from the exact same route objects you just walked through — routing metadata, response_model, tags, function signatures, docstrings. Here's the full mechanism.

Step 1 — Route registration happens first (at import time)

Every @router.get(...), @router.post(...) decorator in the codebase doesn't just register a URL — it builds a rich object called APIRoute and stores far more than "path → function."

Press enter or click to view image in full size
router used as an example

When this decorator runs, FastAPI captures:

  • The path: /stats (later becomes /api/v1/dashboard/stats once prefixes are applied)
  • The HTTP method: GET
  • The tag: "Dashboard" (from APIRouter(prefix="/dashboard", tags=["Dashboard"]))
  • The response schema: DashboardStats — and by extension, the schema of StatsBlock nested inside it

--

--