Introduction to FastAPI and Modern Web Services
Build typed, validated, self-documenting web APIs in Python with FastAPI and Pydantic, from your first endpoint to a tested CRUD service with dependencies and interactive docs.
Intermediate · Software Development · about 15 hours · 5 modules · certificate on completion
Taught by Chaiyapòn Srisomboòn, Mouhamadouù Bâ, AI Learning Companions.
About this course
FastAPI has become a popular way to build web APIs in Python because it turns ordinary type hints into request parsing, validation and documentation. This course takes you from the ideas underneath, HTTP, ASGI and the event loop behind async and await, to a complete, tested CRUD service. You'll define routes with path and query parameters, return the right status codes and organise endpoints with routers; describe your data with Pydantic models, constraints, custom validators and nested schemas; share logic through FastAPI's dependency injection, from database sessions opened and closed for each request to API-key checks that tell 401 from 403; and make the generated OpenAPI documentation genuinely useful in Swagger UI and ReDoc.
Every module has a lab on one task API that grows from lab to lab, with starter files, TODOs to complete, and the exact output of each curl command and test run, all checked against current FastAPI. You'll even measure what blocking the event loop does. The capstone is a projects-and-tasks API you can use entirely from Swagger UI, with its own test suite.
What you'll learn
- Explain how ASGI servers such as Uvicorn run async Python web apps, and when to write async def or def endpoints
- Create FastAPI applications with routers, path and query parameters, and the right HTTP methods and status codes
- Validate requests and shape responses with Pydantic models, field constraints, validators and nested schemas
- Share logic with dependencies for sessions, paging, lookups and API-key checks, and override them in tests
- Document an API through its OpenAPI schema, and explore and test it in Swagger UI and ReDoc
Skills you'll gain
FastAPI, REST API design, Pydantic data validation, HTTP methods and status codes, Dependency injection, OpenAPI documentation, Async Python (ASGI), API testing with TestClient
Tools
FastAPI, Pydantic, Uvicorn, Swagger UI, ReDoc, pytest, curl
Before you start
Comfortable Python: functions, classes, dictionaries, modules, virtual environments and pip, as in Python Programming with PyCharm; type hints help but are explained here. A basic idea of HTTP requests, responses and status codes helps; Networking Fundamentals: How Networks and the Internet Work covers it. You need Python 3.10 or later, a terminal with curl, and a web browser.
Modules
The Modern Web & ASGI
How Python web services work today: HTTP requests and responses, the move from WSGI to ASGI, the event loop behind async and await, and the type hints FastAPI is built on, ending with your first running API.
- HTTP APIs, WSGI and ASGI (11 min)
- async and await in a web server (12 min)
- Type hints and your first FastAPI app (12 min)
- Hands-on lab: Your first API, and async in action
FastAPI Fundamentals & Routing
Set up a FastAPI project and define its routes: path operations for each HTTP method, path and query parameters with validation, status codes and errors, and APIRouter to keep a growing API organised.
- Path operations and HTTP methods (12 min)
- Path and query parameters (12 min)
- Responses, status codes and routers (12 min)
- Hands-on lab: Route a task list
Data Validation with Pydantic
Define the shape of the data your API accepts and returns with Pydantic: request bodies validated automatically, constraints and custom validators, clear 422 errors, nested models, and response models that never leak internal fields.
- Pydantic models and request bodies (12 min)
- Field constraints and validation errors (12 min)
- Nested models and response models (12 min)
- Hands-on lab: Validate tasks with Pydantic
Dependency Injection System
FastAPI's dependency injection: reusable functions and classes the framework calls for you, chains of dependencies, set-up and clean-up with yield for database sessions, and dependencies that enforce security and can be swapped in tests.
- Dependencies with Depends() (12 min)
- Sub-dependencies, classes and yield (13 min)
- Dependencies for security and shared resources (12 min)
- Hands-on lab: Refactor the task API with dependencies
Interactive API Documentation
The documentation FastAPI generates from your code: the OpenAPI schema, Swagger UI for trying requests and ReDoc for reading, and the metadata, tags, error responses and examples that make them useful to the people who call your API.
- OpenAPI: the schema behind the docs (11 min)
- Swagger UI and ReDoc (11 min)
- Making the docs useful (12 min)
- Hands-on lab: Document and explore the task API
Project: A task management API
Build a complete RESTful API for a small team's task manager with FastAPI, building on the labs. It has projects and tasks: every task belongs to a project, so tasks live under /projects/{project_id}/tasks. Provide CRUD operations for both, with the right methods and status codes (201 with the new resource, 204 for deletions, 404 for anything that doesn't exist, 409 for a project name that's taken). Validate everything with Pydantic: separate create, update and response models, constraints, a custom validator, a nested checklist, unknown fields rejected, and an internal field that's never returned. Use dependency injection for a simulated database session opened and closed for each request, for paging and filters, for finding a project or task or answering 404, and for API keys with read and write access. Document the API so that someone else could use it entirely from Swagger UI: metadata, tags, summaries, examples and documented errors. Test it with TestClient, including dependency overrides, and export openapi.json.
Part of
The products this course names are trademarks of their owners, who haven't made or endorsed it.