FastAPI Introduction
What FastAPI is, installing it with Uvicorn, your first app, and the automatic /docs page.
What is FastAPI?
FastAPI is a modern, high-performance Python web framework, released in 2018 by Sebastián Ramírez, purpose-built for creating APIs. Two ideas define it:
- Async-first — FastAPI is built on Starlette (an ASGI framework, not the older WSGI standard) and can handle asynchronous request handlers natively, which suits I/O-heavy workloads like calling other APIs or databases without blocking the server.
- Type-hint-driven — you declare the shape of your data using ordinary Python type hints, and FastAPI uses Pydantic to parse, validate, and document that data automatically. A wrong type in an incoming request is rejected with a clear error before your view code ever runs.
Because request/response shapes are just type-annotated Python, FastAPI can generate a fully interactive API documentation page for free, without you writing a separate spec by hand.
Installing FastAPI
FastAPI itself is just the framework — you also need an ASGI server to actually run it. Uvicorn is the standard choice:
python3 -m venv venv
source venv/bin/activate # on Windows: venv\Scripts\activate
pip install fastapi uvicorn
Your first FastAPI app
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, World!"}
Notice there's no jsonify equivalent needed — returning a plain Python dict (or list, or Pydantic model) is automatically serialized to a JSON response.
Running the app with Uvicorn
uvicorn main:app --reload
main:appmeans "in themainmodule, use the object namedapp" — theFastAPI()instance created above.--reloadrestarts the server automatically whenever you save a code change, exactly like Flask's debug mode. It's for development only; a production deployment drops--reloadand typically runs multiple worker processes.
Visiting http://127.0.0.1:8000 returns {"message": "Hello, World!"}.
The automatic /docs page
This is one of FastAPI's signature features: visit http://127.0.0.1:8000/docs while the app is running, and you get a full interactive API explorer (Swagger UI) — every route, its parameters, its expected request body, and its response shape, generated automatically from your type hints. You can expand any endpoint and send a real test request directly from the browser, with no separate documentation to write or keep in sync.
A second, alternative documentation UI (ReDoc) is available at /redoc, and the raw machine-readable spec itself is served as JSON at /openapi.json.
Common mistakes
- Forgetting to install (or start) an ASGI server —
fastapialone defines the app, but something like Uvicorn is what actually serves HTTP requests. - Running with
--reloadin production — like Flask'sdebug=True, it's meant purely for local development. - Writing
defwhereasync defwas intended (or the reverse) without understanding the difference — covered in more depth alongside dependency injection later in this track.
Interview questions
Q: What does FastAPI generate automatically, and how?
FastAPI generates a fully interactive OpenAPI documentation page (/docs, using Swagger UI) directly from your route definitions, function parameter type hints, and Pydantic models — no separate specification file to hand-write or keep in sync. Because the validation rules and the documentation both come from the same type hints, they can never silently drift apart the way a hand-maintained API spec can.
Q: Why does FastAPI sit on top of Starlette and ASGI instead of WSGI, like Flask does?
WSGI handles one request per worker thread at a time and has no native concept of asynchronous I/O. ASGI (Asynchronous Server Gateway Interface) is designed around async/await, letting a single worker process handle many concurrent requests that are waiting on I/O (a database call, an HTTP request to another service) without blocking on each one — which is central to FastAPI's performance story for I/O-bound APIs.