📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials API Design OpenAPI and Swagger

OpenAPI and Swagger

5 min read
FastAPI auto-generates OpenAPI from type hints — interactive Swagger UI at /docs with zero extra code.

OpenAPI and Swagger

# FastAPI auto-generates OpenAPI from Pydantic
from pydantic import BaseModel
from datetime import datetime

class PostCreate(BaseModel):
    title: str
    body:  str
    tags:  list[str] = []

class PostResponse(BaseModel):
    id: int; title: str; created_at: datetime

@app.post("/api/posts",
    response_model=PostResponse,
    status_code=201,
    summary="Create a blog post",
    tags=["posts"])
def create_post(post: PostCreate): ...

# Auto-generated:
# GET /openapi.json -- spec
# GET /docs         -- Swagger UI
# GET /redoc        -- ReDoc
# openapi-generator-cli generate -i openapi.json -g python