← Back to Tutorials

4. OpenAPI & Swagger

Automatic Documentation

FastAPI automatically generates an OpenAPI schema from your code. Visit:

Customizing Metadata

app = FastAPI(
    title="My API",
    description="A sample FastAPI application",
    version="1.0.0",
    contact={"name": "Snehal", "email": "snehal@example.com"},
    license_info={"name": "MIT"},
)

@app.get("/items/{item_id}",
         summary="Get an item",
         description="Retrieve an item by its ID",
         tags=["items"],
         deprecated=False)
def read_item(item_id: int): ...

OpenAPI Schema

# View the raw schema at /openapi.json
{
  "openapi": "3.1.0",
  "info": {"title": "My API", "version": "1.0.0"},
  "paths": {
    "/items/{item_id}": {
      "get": {
        "parameters": [...],
        "responses": {...}
      }
    }
  }
}
✏️ Exercise: Run your app and open /docs. Add title, description, and version metadata. Verify the Swagger UI reflects your changes.