Introduction
Automatic API documentation helps you see and test your API easily without writing extra docs.
Automatic API documentation helps you see and test your API easily without writing extra docs.
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): return {"item_id": item_id}
from fastapi import FastAPI app = FastAPI() @app.get("/hello") async def say_hello(): return {"message": "Hello World"}
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") async def create_item(item: Item): return item
This API has a POST endpoint to create a user. Swagger UI will show the input form for username and email automatically.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): username: str email: str @app.post("/users/") async def create_user(user: User): return {"message": f"User {user.username} created", "email": user.email}
You can also access ReDoc docs at /redoc URL for a different style.
Swagger UI updates automatically when you change your API code and restart the server.
FastAPI provides automatic API docs with Swagger UI at /docs.
You do not need to write extra documentation code for basic API docs.
Swagger UI lets you test API endpoints interactively in the browser.
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
def say_hello():
return {"message": "Hello, world!"}from fastapi import FastAPI
app = FastAPI()
@app.get("/items")
def read_items():
return [{"item_id": 1}]
# You run the app with: uvicorn main:app --port 8000 --host 0.0.0.0
# But /docs returns 404/docs to /api-docs. Which FastAPI app creation code achieves this?Pass the test and this pattern moves unknown