I was evaluating FastAPI for a new service. Here’s the quick start.
Install:
pip install fastapi uvicorn
Basic app:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
Run:
uvicorn main:app --reload
Request body with Pydantic Link to heading
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
def create_item(item: Item):
return item
Auto docs Link to heading
FastAPI generates docs automatically:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Async support Link to heading
@app.get("/async")
async def async_route():
result = await some_async_function()
return result
The key thing is type hints - FastAPI uses them for validation and documentation.