FastAPI Essentials

Course Content

FastAPI Essentials

1 sections · 32 lessons

What are OpenAPI specifications and why are they important?


What you need to know

An OpenAPI document has a few main parts:

SectionWhat it describes
infoTitle, version, description
pathsEach URL and method, with parameters, request body and responses
components.schemasReusable models, such as ChatRequest
components.securitySchemesHow to authenticate (Bearer token, API key)

Who uses it

  • People: Swagger UI at /docs and ReDoc at /redoc render it for humans.
  • Client generators: openapi-typescript or openapi-generator create typed SDKs, so the front-end never hand-writes fetch calls.
  • Infrastructure: API gateways, mock servers and contract tests read it.
  • AI agents: a parameter schema is already JSON Schema, which is what LLM tool definitions use.

From OpenAPI to an LLM tool

Python
from typing import Annotatedfrom fastapi import FastAPI, Queryfrom pydantic import BaseModelapp = FastAPI(title="Orders API")class OrderStatus(BaseModel):    order_id: str    status: str    eta_minutes: int | None = None@app.get("/orders/{order_id}", operation_id="get_order_status",         summary="Get the delivery status of one order")def get_order_status(order_id: str,                     include_eta: Annotated[bool, Query(description="Also return ETA")] = True) -> OrderStatus:    ...def openapi_to_tools(spec: dict) -> list[dict]:    tools = []    for path, ops in spec["paths"].items():        for method, op in ops.items():            params = op.get("parameters", [])            tools.append({                "name": op["operationId"], "description": op["summary"],                "input_schema": {"type": "object",                                 "properties": {p["name"]: p["schema"] for p in params},                                 "required": [p["name"] for p in params if p.get("required")]}})    return tools

openapi_to_tools(app.openapi()) really produces a tool named get_order_status with the summary as its description, an input schema with order_id (string, required) and include_eta (boolean, default true, "Also return ETA"). The agent can now call your endpoint, and when you change the route, the tool changes with it. Libraries such as FastMCP can build a whole MCP server from an OpenAPI spec in the same spirit.

Set operation_id and summary deliberately: they become the tool's name and the text the model reads to decide when to call it.

A real-life example

A quick-commerce company has an orders service in FastAPI, a React web app, an Android app and a support agent built on an LLM. Before, each team wrote its own client and its own idea of the order status values. When the backend added a new status, rider_reassigned, the Android app crashed on the unknown value.

Now the status is a declared Enum in the schema. CI regenerates the TypeScript and Kotlin clients from /openapi.json on every backend release, and a contract test fails if a change would break an existing client. The support agent's tools are generated from the same spec, so it learned about rider_reassigned with no prompt edits.

Follow-up questions to expect

  • "What is the difference between OpenAPI and Swagger?" — Swagger was the original name of the spec; since version 3 the spec is OpenAPI, and "Swagger" now means the tools such as Swagger UI.
  • "How do you make the spec more useful?" — Return types on every route, responses= for error codes, field descriptions and examples, and clear operation_id values.
  • "Should the spec be public?" — For internal services, often not: disable the docs routes in production and publish the spec to an internal portal or registry instead.