Skip to main content
In this lesson we’ll cover how to create clear, machine-readable API documentation using FastAPI and OpenAPI. You’ll see a compact, production-ready example that demonstrates best practices for Pydantic models, endpoint metadata, and SQLite-backed data retrieval—so your generated Swagger UI and ReDoc docs are useful to consumers.
A presentation slide titled "Creating API Documentation" with a large dark blue curved shape on the right containing the word "Demo." The slide also shows a small "© Copyright KodeKloud" mark at the bottom left.

Why FastAPI?

  • FastAPI automatically generates OpenAPI-compliant docs (Swagger UI at /docs, ReDoc at /redoc) from your typed function signatures and Pydantic models.
  • Adding precise Pydantic models, descriptive docstrings, and endpoint metadata (summary, description, tags, response models) improves the generated documentation, making it searchable and actionable for API consumers.
FastAPI exposes interactive documentation at /docs (Swagger UI) and /redoc (ReDoc). Supplying typed models, examples, and response schemas greatly enhances the usefulness of both the UI and the underlying OpenAPI spec.

Example FastAPI app (complete)

Below is a concise, corrected FastAPI application that exposes a POST endpoint to return fake data from a local SQLite database. The code includes:
  • App creation and imports
  • Pydantic request and response models
  • A context manager for DB connections with documentation
  • A POST endpoint with validation, response_model, and useful metadata for OpenAPI

Running the application

Start the app with Uvicorn (development with auto-reload):
Typical server output (condensed):
After startup:
  • Visit http://localhost:8000/docs for Swagger UI
  • Visit http://localhost:8000/redoc for ReDoc
  • http://localhost:8000/openapi.json returns the raw OpenAPI spec

Improving the generated documentation

Key recommendations to improve your OpenAPI documentation quality:
  • Use Pydantic models for both request and response so the OpenAPI output is precise.
  • Set response_model on endpoints for explicit response schemas.
  • Add summary, description (via docstring), and tags to improve discoverability in the docs UI.
  • Provide example payloads using Pydantic Field(..., example=...) or via the responses parameter in the route decorator.
Table — Quick mapping of doc endpoints and best-practice metadata:

Example request / response

Request (JSON):
Successful response (200):
Validation error (400):

Tips when using Copilot or other AI assistants for docs

  • Treat the assistant as a reviewer: ask it to suggest missing fields, example payloads, and error responses.
  • Prefer explicit Pydantic models and decorator metadata rather than embedding YAML inside docstrings—this yields more consistent OpenAPI output.
  • Iterate: accept suggestions, tweak models or examples, restart the server, and inspect changes at /docs and /redoc.

References

Summary
  • FastAPI auto-generates documentation, but the quality depends on models, signatures, and metadata.
  • Use Pydantic models for precise schemas, response_model for outputs, and clear docstrings or decorator metadata for readable docs.
  • AI assistants can accelerate doc creation—review and refine generated content for accuracy.
Thank you for reading.

Watch Video