FastAPI Fundamentals
Learning Path
Hands-on Labs
Each objective has a coding lab that opens in VS Code in your browser
Create a FastAPI application with path operations
You will build a FastAPI application from scratch with a `GenAIServiceAPI` class that registers path operations for a prompt management service. Implement GET /prompts to list prompts with query parameter filtering (limit, offset, tags), POST /prompts to create prompts with request body validation, GET /prompts/{prompt_id} with path parameter parsing, and DELETE /prompts/{prompt_id} returning 204 No Content. Configure uvicorn programmatically with reload for development.
Define Pydantic request and response models
You will build a type-safe API layer using Pydantic models. Create `PromptCreate` with field validators for name length (3-100 chars), template format validation using regex, and a custom validator ensuring template variables match declared parameters. Build `PromptResponse` with computed fields for token_count and created_at defaults. Implement `PromptList` with pagination metadata. Use Field() for descriptions, examples, and JSON schema customization.
Implement dependency injection for shared resources
You will build a dependency injection system using FastAPI's Depends(). Create `get_settings()` that loads configuration from environment variables with Pydantic Settings. Build `get_prompt_store()` as a singleton dependency providing an in-memory prompt repository. Implement `get_current_page()` as a reusable pagination dependency extracting limit/offset from query params. Chain dependencies so endpoints receive fully configured services.
Build CRUD endpoints with proper HTTP semantics
You will implement a complete REST API for managing hosted LLM prompt templates (OpenAI, Gemini) following HTTP semantics. POST returns 201 with Location header, GET returns 200 with ETag for caching, PUT returns 200 with the updated resource, DELETE returns 204 with no body. Implement 404 responses with a ProblemDetail error model following RFC 7807. Build a `PromptRepository` class with in-memory storage that all endpoints share via dependency injection.
Configure OpenAPI documentation with examples
You will enrich the auto-generated OpenAPI documentation for production use. Add response_model_exclude_unset=True for clean JSON responses. Define multiple response examples using OpenAPI's example syntax in Pydantic model Config. Add tags and descriptions to group endpoints logically. Configure the OpenAPI schema with servers, contact info, and license. Build a custom Swagger UI page with try-it-out enabled and a Redoc alternative.
Handle errors with custom exception handlers
You will build a structured error handling system. Create custom exception classes `PromptNotFoundError`, `ValidationError`, and `RateLimitExceeded` inheriting from a base `APIError`. Register exception handlers with @app.exception_handler that return consistent RFC 7807 ProblemDetail responses. Implement a catch-all handler for unhandled exceptions that logs the traceback and returns 500 with a correlation ID. Add request validation error formatting that transforms Pydantic errors into user-friendly messages.