What is FastAPI

beginner fastapi async pydantic starlette

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.

FastAPI Stack
Your Code (type-hinted functions)
FastAPI (routing + OpenAPI glue)
Starlette
(async HTTP)
Pydantic
(validation)
Uvicorn (ASGI server)

Why people love it

Think of it like Flask, but with three big upgrades:

  1. Async-first. We can async def any endpoint, hit databases or other APIs concurrently, and the event loop doesn’t block.
  2. Validation comes free. Type-hint a parameter as int and FastAPI rejects strings with a clean 422 error. No more manual request.json.get() checks.
  3. Docs come free. Go to /docs and there’s a full Swagger UI. /redoc gives 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.server is 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.