FastAPI in 60 Minutes
TL;DR: Learn how to build a REST API with FastAPI and uvicorn in 60 minutes with hands-on examples covering request validation, dependency injection, and a complete Book Catalog API served over real HTTP
Tutorial in 30 Seconds#
FastAPI is a modern Python framework for building APIs, using standard type hints to drive request validation, serialization, and interactive documentation, and uvicorn is the ASGI server that runs it
Key capabilities:
- Automatic validation:
pydanticmodels validate request and response bodies from type hints alone, with no separate schema to maintain - Interactive docs: Swagger UI and ReDoc are generated automatically from the same type hints, always in sync with the code
- Async native: first-class support for
async defendpoints, so a single worker serves many concurrent requests - Dependency injection:
Depends()shares parameter parsing and validation logic across endpoints - High performance: built on
Starletteandpydantic, close to Node.js and Go for I/O-bound workloads
This tutorial's goal is to show you in 60 minutes:
- The core API of FastAPI: path and query parameters, request bodies, dependency injection, and error handling
- How to run a FastAPI app with uvicorn, both in-process with
TestClientand behind a real server - A complete example: a Book Catalog REST API exercised with real HTTP requests over the network
Official References#
Tutorial Content#
This tutorial includes all the code, notebooks, and Docker containers in tutorials/fastapi
README.md: Instructions and setup for the tutorial environment- A Docker system to build and run the environment using our standardized approach
fastapi.API.ipynb: Tutorial notebook focusing on core FastAPI building blocks- Path and query parameters, request body validation with
pydanticmodels - Dependency injection with
Depends() - Error handling with
HTTPException - The automatic
/docs,/redoc, and/openapi.json fastapi.example.ipynb: End-to-end Book Catalog API example- Runs a real
uvicornserver on a background thread - Exercises the API with real HTTP requests via
httpx - Covers create, read, update, delete, filtering, and error responses
fastapi_utils.py: Utility functions and models shared by both notebooks