FastAPI
Essential FastAPI reference covering routes, request validation, Pydantic models, dependencies, authentication, file uploads, middleware, database integration, and project structure.
Other Python Sheets
Installation & Setup
Install FastAPI and run your first application.
Quick Start
Install FastAPI with uvicorn and create a basic application.
Path & Query Parameters
Define URL path parameters and query string parameters with validation.
Path Parameters
Extract values from the URL path with type validation.
Query Parameters
Accept query string parameters with defaults and validation.
Request Body & Pydantic Models
Define and validate request bodies using Pydantic models.
Pydantic Models
Define request and response schemas with automatic validation.
Response Models & Status Codes
Control response shape and HTTP status codes.
Form Data & File Uploads
Handle HTML form submissions and file uploads.
Form Data & File Uploads
Receive form fields and uploaded files in endpoints.
Cookie & Header Parameters
Read cookies and HTTP headers from incoming requests.
Cookies & Headers
Extract cookie values and HTTP headers from requests.
Dependencies
Dependency injection for shared logic, DB sessions, and auth.
Dependency Injection
Share logic across endpoints using Depends().
Security & Authentication
Implement OAuth2 with JWT tokens and password hashing.
OAuth2 with JWT
Implement token-based authentication with password hashing.
Error Handling
Raise HTTP errors and define custom exception handlers.
HTTPException & Custom Handlers
Return proper error responses and handle exceptions globally.
Middleware & CORS
Add middleware for cross-cutting concerns and configure CORS.
CORS & Custom Middleware
Configure CORS and write custom middleware for logging, timing, etc.
Database Integration
Connect to databases with SQLAlchemy or SQLModel.
SQLAlchemy Setup
Configure SQLAlchemy with FastAPI using dependency injection.
CRUD Operations
Create, read, update, and delete records with SQLAlchemy.
Project Structure
Organize larger applications with APIRouter and multiple files.
APIRouter & Multiple Files
Split routes into separate files using APIRouter.
Advanced Features
Background tasks, lifespan events, streaming, WebSockets, and testing.
Background Tasks & Lifespan
Run tasks after returning a response and manage app startup/shutdown.
Streaming, WebSockets & Testing
Stream responses, use WebSockets, and test with TestClient.