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:

text
  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)
ProtocolSynchronous call interfaceAsync-capable interface
Typical serverGunicornUvicorn, Hypercorn
WebSocketsVia extensionsNative support
async routesLimitedFirst-class async def
text
  Uvicorn (HTTP)  →  ASGI  →  FastAPI app  →  your endpoints

Code 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:

URLPurpose
/docsSwagger UI—try endpoints in browser
/redocReDoc—readable reference
/openapi.jsonMachine-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

FastAPIFlaskDjango
Primary useJSON APIs, microservicesAPIs + server-rendered HTMLFull-stack apps, admin
ValidationPydantic built-inManual or extensionsForms / DRF
API docsAutomatic OpenAPIManual or add-onsDRF schema
TemplatesOptional (not focus)Jinja2 coreBuilt-in
Learning curveTypes + async conceptsGentle, minimalSteeper, 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

text
Python basics → FastAPI (this track) → MySQL via SQLAlchemy
       ↓              ↓                      ↓
   type hints    OpenAPI / JWT            Alembic migrations
       ↓              ↓                      ↓
     Git           Redis (optional)         Nginx deploy
TrackRole
PythonLanguage
FlaskWSGI, templates, sessions (compare)
MySQLSQL and schema
RedisCaching (optional)
GitVersion control
NginxReverse proxy
VueSPA frontend—fetch, CORS, deploy (chapter 17, 24)

Minimal Preview

You will write code like this in chapter 3:

python
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

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.