How to Access API Documentation in FastAPI Easily
/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.
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.
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
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.
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
| Feature | Default URL | Description |
|---|---|---|
| Swagger UI docs | /docs | Interactive API explorer with testing |
| ReDoc docs | /redoc | Clean, readable API documentation |
| OpenAPI JSON schema | /openapi.json | Raw API schema in JSON format |
| Disable docs | Set docs_url=None | Turn off docs if needed |
| Custom docs URL | docs_url='/custom' | Change docs path |