Python / FastAPI Basics Interview Questions
How do you customise the OpenAPI documentation in FastAPI?
FastAPI auto-generates OpenAPI 3.x specs from your code. You can enrich the docs with metadata, examples, tags, and custom descriptions without any extra tooling.
from fastapi import FastAPI from fastapi.openapi.utils import get_openapi from pydantic import BaseModel, Field # App-level metadata app = FastAPI( title="My Inventory API", description="## Manage your warehouse inventory\n\nSupports CRUD for items and categories.", version="2.1.0", contact={"name": "Dev Team", "email": "dev@example.com"}, license_info={"name": "MIT"}, terms_of_service="https://example.com/terms", docs_url="/docs", # Swagger UI path redoc_url="/redoc", # ReDoc path openapi_url="/openapi.json", ) class Item(BaseModel): name: str = Field( ..., examples=["Widget"], description="Human-readable item name", ) price: float = Field( ..., gt=0, examples=[9.99], description="Price in USD, must be positive", ) @app.post( "/items", summary="Create a new inventory item", description="Creates an item and returns it with its assigned ID.", response_description="The created item including its database ID", tags=["inventory"], status_code=201, ) def create_item(item: Item) -> Item: return item # Disable docs in production (optional) # app = FastAPI(docs_url=None, redoc_url=None)
More Related questions...