What Is FastAPI
Introduction
FastAPI is a modern Python framework for building APIs with standard type hints. It validates request data, serializes responses, and generates OpenAPI documentation automatically—so you write less boilerplate and catch bugs before runtime. This chapter explains what FastAPI is, how ASGI differs from Flask’s WSGI, and where this track fits on Hello Code.
Prerequisites
- Basic Python knowledge: functions, modules, and type hints (helpful but not required yet)
- A terminal on Windows, macOS, or Linux
- No FastAPI installation needed for this overview
What FastAPI Does
When a client sends an HTTP request, FastAPI:
Client (browser, app) FastAPI Your code
│ │ │
│ GET /api/items │ │
├─────────────────────►│ match route │
│ ├────────────────────►│ return data
│◄─────────────────────┤ JSON response │
│ │ + OpenAPI metadata │Code explanation:
- Routes are Python functions decorated with
@app.get,@app.post, etc. - Return a dict or Pydantic model—FastAPI converts it to JSON
- Invalid input returns 422 with structured error details before your function runs
FastAPI sits on Starlette (web toolkit) and Pydantic (data validation).
ASGI vs WSGI
| WSGI (Flask) | ASGI (FastAPI) | |
|---|---|---|
| Protocol | Synchronous call interface | Async-capable interface |
| Typical server | Gunicorn | Uvicorn, Hypercorn |
| WebSockets | Via extensions | Native support |
| async routes | Limited | First-class async def |
Uvicorn (HTTP) → ASGI → FastAPI app → your endpointsCode explanation:
- Development uses
uvicorn ... --reload - Production uses Uvicorn workers or Gunicorn with UvicornWorker—see deployment chapter
Compare Flask’s WSGI model in What Is Flask.
OpenAPI Out of the Box
FastAPI generates interactive docs:
| URL | Purpose |
|---|---|
/docs | Swagger UI—try endpoints in browser |
/redoc | ReDoc—readable reference |
/openapi.json | Machine-readable schema |
No separate codegen step—your type hints and Pydantic models become the spec.
Tip
Docs Are for Development First
Disable or protect /docs in production if you do not want public API exploration.
FastAPI vs Flask vs Django
| FastAPI | Flask | Django | |
|---|---|---|---|
| Primary use | JSON APIs, microservices | APIs + server-rendered HTML | Full-stack apps, admin |
| Validation | Pydantic built-in | Manual or extensions | Forms / DRF |
| API docs | Automatic OpenAPI | Manual or add-ons | DRF schema |
| Templates | Optional (not focus) | Jinja2 core | Built-in |
| Learning curve | Types + async concepts | Gentle, minimal | Steeper, all-in-one |
Choose FastAPI when the product is API-first or you want typed contracts with minimal setup.
Typical Use Cases
- REST APIs for React, Vue, or mobile apps
- Microservices behind API gateways
- ML inference endpoints wrapping Python models
- Internal tools exposing JSON to dashboards
- WebSockets for live updates (later chapter)
Version and Stack
This track targets:
- Python 3.10+
- FastAPI 0.110+
- Pydantic v2
Main tutorial stack: FastAPI + Uvicorn + SQLAlchemy 2.0 + Alembic + JWT.
Hello Code Learning Path
Python basics → FastAPI (this track) → MySQL via SQLAlchemy
↓ ↓ ↓
type hints OpenAPI / JWT Alembic migrations
↓ ↓ ↓
Git Redis (optional) Nginx deploy| Track | Role |
|---|---|
| Python | Language |
| Flask | WSGI, templates, sessions (compare) |
| MySQL | SQL and schema |
| Redis | Caching (optional) |
| Git | Version control |
| Nginx | Reverse proxy |
| Vue | SPA frontend—fetch, CORS, deploy (chapter 17, 24) |
Minimal Preview
You will write code like this in chapter 3:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}Run with uvicorn main:app --reload and open http://127.0.0.1:8000/docs.
FAQ
FastAPI still popular?
Yes—widely adopted for new Python APIs; active development by the community and original author.
Need Flask first?
Not required. Learn Flask if you want server-rendered HTML; learn FastAPI if you want typed JSON APIs. Many teams use both.
Must I use async everywhere?
No—sync def routes work fine. Async helps for high-concurrency I/O when used correctly.
Replace Django?
Different goals—Django for monolithic web apps with admin; FastAPI for lean APIs.
Commercial use?
FastAPI is MIT-licensed.
Official docs?
FastAPI documentation—companion to this track.