0
0
FastapiHow-ToBeginner · 3 min read

How to Access API Documentation in FastAPI Easily

FastAPI automatically generates interactive API documentation accessible at /docs for Swagger UI and /redoc for ReDoc. You just run your FastAPI app and open these URLs in your browser to explore and test your API.
📐

Syntax

FastAPI provides two default documentation interfaces accessible via specific URLs:

  • /docs: Shows Swagger UI, an interactive API explorer.
  • /redoc: Shows ReDoc, a clean and readable API doc.

You do not need to add extra code to enable these; they are included by default when you create a FastAPI() app instance.

python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello World"}

# Run the app and visit http://127.0.0.1:8000/docs or /redoc
💻

Example

This example shows a simple FastAPI app with one endpoint. After running it, you can access the docs at /docs or /redoc to see the API documentation and test the endpoint interactively.

python
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}

# Run with: uvicorn filename:app --reload
# Access docs at: http://127.0.0.1:8000/docs or /redoc
Output
Open your browser and go to http://127.0.0.1:8000/docs to see Swagger UI with the GET /items/{item_id} endpoint documented and testable.
⚠️

Common Pitfalls

1. Docs not showing: This can happen if you disable docs by setting docs_url=None or run behind a proxy without correct URL paths.

2. Custom docs URLs: If you change docs_url or redoc_url in FastAPI(), you must visit the new URLs.

3. Missing dependencies: FastAPI uses swagger-ui and redoc from CDN, so no extra install is needed, but blocking internet access can cause docs not to load.

python
from fastapi import FastAPI

# Disabling docs example
app = FastAPI(docs_url=None, redoc_url=None)

@app.get("/")
async def root():
    return {"msg": "No docs available"}
📊

Quick Reference

FeatureDefault URLDescription
Swagger UI docs/docsInteractive API explorer with testing
ReDoc docs/redocClean, readable API documentation
OpenAPI JSON schema/openapi.jsonRaw API schema in JSON format
Disable docsSet docs_url=NoneTurn off docs if needed
Custom docs URLdocs_url='/custom'Change docs path

Key Takeaways

FastAPI auto-generates docs at /docs (Swagger UI) and /redoc (ReDoc) by default.
Run your app and open these URLs in a browser to explore and test your API.
You can customize or disable docs URLs using FastAPI constructor parameters.
Docs require internet access to load UI assets from CDN unless self-hosted.
If docs don’t appear, check if docs_url or redoc_url were changed or disabled.