FastAPI is a modern Python web framework for building APIs. It uses Python type hints to do request validation, serialization, and even auto-generate interactive docs. In simple language: we write a normal Python function with type hints, and FastAPI figures out the rest.
It sits on top of two libraries:
- Starlette — handles the async HTTP plumbing (routing, middleware, websockets).
- Pydantic — handles the data validation using type hints.
So FastAPI is really a thin, opinionated layer that glues these two together and adds OpenAPI/Swagger on top.
(async HTTP)
(validation)
Why people love it
Think of it like Flask, but with three big upgrades:
- Async-first. We can
async defany endpoint, hit databases or other APIs concurrently, and the event loop doesn’t block. - Validation comes free. Type-hint a parameter as
intand FastAPI rejects strings with a clean 422 error. No more manualrequest.json.get()checks. - Docs come free. Go to
/docsand there’s a full Swagger UI./redocgives the ReDoc version. Both auto-generated from our function signatures.
Quick example
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
in_stock: bool = True
@app.post("/items/")
async def create_item(item: Item):
return {"item": item, "tax": item.price * 0.18}
That’s a real endpoint. It validates the body, serializes the response, and shows up in /docs with a try-it-out button. We didn’t write a single line of validation code.
When to reach for it
- Building a REST or GraphQL API where types matter (data ingestion, ML model serving, internal services).
- Need async I/O — calling many DB queries, third-party APIs, or websockets.
- Want OpenAPI specs without hand-writing YAML.
When not to
- Building a server-rendered website with templates and forms. Django is better there.
- Tiny script that just needs one endpoint. Flask or even
http.serveris enough.
The interview pitch
If someone asks “what is FastAPI” in a screening round, the one-liner is: “FastAPI is a modern, async Python framework built on Starlette and Pydantic that uses type hints for automatic request validation, response serialization, and OpenAPI doc generation.” That sentence covers everything an interviewer wants to hear.