Uvicorn

Uvicorn is a lightning-fast ASGI (Asynchronous Server Gateway Interface) server for Python. FastAPI is a web framework; Uvicorn is the server that actually runs the app and handles incoming HTTP requests.
When you run fastapi dev main.py, the FastAPI CLI starts Uvicorn under the hood. You can also run Uvicorn directly:


$ uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000

# main   = Python file (main.py)
# app    = FastAPI instance inside that file
# --reload = restart server on code changes (development only)
      

ASGI (Asynchronous Server Gateway Interface)

ASGI is a standard interface between a Python web server and a Python web application. It is the async successor to WSGI (Web Server Gateway Interface), which only supports synchronous request/response handling.
What it does: Defines how a server (e.g. Uvicorn) passes HTTP requests, WebSocket connections, and other events to an app (e.g. FastAPI), and how the app sends responses back.
When it is used:
1. Building async APIs with async def route handlers.
2. Handling WebSockets (chat, live updates, streaming).
3. Long-running or concurrent I/O (database calls, external API requests) without blocking other requests.
4. Frameworks like FastAPI, Starlette, and Django (async views) that run on ASGI servers such as Uvicorn, Hypercorn, or Daphne.

Routes

Routes are Python functions decorated with HTTP method decorators such as @app.get, @app.post, etc. FastAPI maps the URL path and HTTP verb to the function.


from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}
      

Pydantic Models

Pydantic models define the shape of request and response data. FastAPI validates incoming JSON against the model and serializes responses automatically.


from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float
    in_stock: bool = True

@app.post("/items/")
def create_item(item: Item):
    return item
      

Auto Docs

FastAPI generates OpenAPI schema from your type hints and route definitions. Visit these URLs while the server is running:


http://127.0.0.1:8000/docs — Swagger UI
http://127.0.0.1:8000/redoc — ReDoc