Difficulty: Beginner
What is FastAPI, and what makes it stand out compared to Flask or Django for building APIs?
FastAPI is a modern Python web framework for building APIs, built on top of Starlette (the ASGI web toolkit) and Pydantic (data validation). You describe your inputs and outputs with standard Python type hints, and FastAPI uses those hints to parse and validate requests, serialize responses and generate an OpenAPI schema with interactive docs, all from the same code.
FastAPI is a thin, opinionated layer over two well-tested libraries:
FastAPI glues these together through function signatures. When you write def get_order(order_id: int, q: str | None = None), FastAPI reads the signature once at startup and knows order_id is a required integer path parameter and q an optional query string.
"abc" where an int is expected and the client gets a 422 with a precise error location, without you writing a single if./docs and ReDoc at /redoc, generated from the OpenAPI schema at /openapi.json.Depends(), used for DB sessions, auth, pagination and settings.async def endpoints run on the event loop; plain def endpoints are run in a threadpool so blocking code does not freeze the server.| Aspect | FastAPI | Flask | Django (+ DRF) |
|---|---|---|---|
| Interface | ASGI, async first | WSGI (async views possible but limited) | WSGI and ASGI |
| Validation | Built in via Pydantic | Manual or extensions | DRF serializers |
| API docs | Automatic OpenAPI | Extensions | Extensions (drf-spectacular) |
| ORM, admin, auth | Bring your own | Bring your own | Batteries included |
| Best fit | JSON APIs, ML serving, microservices | Small apps, simple services | Full products with admin and ORM |
FastAPI is not "better" in every case. Django still wins when you need an admin panel, migrations and auth on day one.
FastAPI is a common choice for ML model serving (wrapping a PyTorch or scikit-learn model behind /predict), internal microservices and backends for mobile apps. A typical startup stack is FastAPI + SQLAlchemy 2.0 + PostgreSQL + Redis, deployed in Docker behind Nginx or a cloud load balancer.
async def everywhere makes it faster." Calling a blocking library inside async def blocks the whole event loop.from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Orders API")
class Order(BaseModel):
item: str
quantity: int
amount_paise: int # store money as integer paise, never float rupees
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
@app.post("/orders", status_code=201)
def create_order(order: Order):
# order is already parsed and validated here
return {"item": order.item, "total_paise": order.quantity * order.amount_paise}
# Run in dev: fastapi dev main.py (or: uvicorn main:app --reload)
# Docs: http://127.0.0.1:8000/docs
Type hints on the Pydantic model drive validation and the docs. Posting a string for quantity returns a 422 automatically.
pip install "fastapi[standard]" # FastAPI + Uvicorn + fastapi CLI
fastapi dev main.py # auto-reload, for development
fastapi run main.py # production mode, no reload
curl -X POST http://127.0.0.1:8000/orders \
-H "Content-Type: application/json" \
-d '{"item": "dosa", "quantity": "two", "amount_paise": 8000}'
The fastapi CLI ships with the standard extra in recent versions. The bad quantity value triggers an automatic validation error.
FastAPI, ASGI, Starlette, Pydantic, type hints, OpenAPI