Python FastAPI
Overview
FastAPI is a modern Python web framework for building APIs with automatic documentation. This skill should be invoked when building high-performance APIs that benefit from Python's type hints and FastAPI's automatic validation.
Core Principles
- Type Hints: Leverage Python type hints for validation
- Automatic Docs: OpenAPI/Swagger generated automatically
- Async Support: Native async/await support
- Dependency Injection: Use Depends for clean code
Preparation Checklist
- Install FastAPI:
pip install fastapi - Choose ASGI server (Uvicorn, Hypercorn)
- Plan API structure
- Define Pydantic models
Step-by-Step Process
- Install: Add FastAPI and uvicorn
- Define Models: Create Pydantic models
- Routes: Add route handlers
- Validate: Use type hints and Pydantic
- Document: Access automatic docs at /docs
- Run: Start with uvicorn
Do's and Don'ts
- ✅ Do use Pydantic for validation
- ✅ Do leverage async for I/O operations
- ✅ Do use dependency injection
- ❌ Don't ignore type hints
- ❌ Don't skip error handling
- ❌ Don't use sync code for DB calls
Anti-Patterns
- No Validation: Skipping Pydantic models
- Sync DB: Using synchronous database calls
- Missing Errors: Not handling HTTP exceptions
- No Docs: Not reviewing automatic docs