Advanced·18 min·web · fastapi · rest · api
REST APIs with FastAPI
FastAPI: typed, async, auto-documented
FastAPI is Flask's modern successor — same simplicity, but type hints do triple duty:
- Editor autocomplete — IDEs know the shape of your data.
- Runtime validation — Pydantic rejects bad payloads with a clear 422 error.
- Auto-generated OpenAPI docs — visit
/docsfor a live Swagger UI,/redocfor ReDoc.
Core concepts
Pydantic models = your API contract
class TodoIn(BaseModel):
title: str = Field(min_length=1, max_length=200)
done: bool = False
If a client POSTs {"title": ""}, FastAPI auto-rejects with 422. You don't write validation code.
Response models
response_model=Todo filters output — you can't leak fields you didn't declare.
HTTPException
Raise HTTPException(404, "Not found") for typed error responses.
Async when you need it
Any handler can be async def — great for calling other APIs or an async DB driver like asyncpg.
Run it
uvicorn main:app --reload
Then open http://localhost:8000/docs.
Try it (mentally, before running locally)
- Add a
DELETE /todos/{todo_id}endpoint. - Add a query param
?done=truetoGET /todosthat filters.