1
---
2
name: fastapi-development
3
description: >
4
Build high-performance FastAPI applications with async routes, validation,
5
dependency injection, security, and automatic API documentation. Use when
6
developing modern Python APIs with async support, automatic OpenAPI
7
documentation, and high performance requirements.
8
---
9
10
# FastAPI Development
11
12
## Table of Contents
13
14
- [Overview](#overview)
15
- [When to Use](#when-to-use)
16
- [Quick Start](#quick-start)
17
- [Reference Guides](#reference-guides)
18
- [Best Practices](#best-practices)
19
20
## Overview
21
22
Create fast, modern Python APIs using FastAPI with async/await support, automatic API documentation, type validation using Pydantic, dependency injection, JWT authentication, and SQLAlchemy ORM integration.
23
24
## When to Use
25
26
- Building high-performance Python REST APIs
27
- Creating async API endpoints
28
- Implementing automatic OpenAPI/Swagger documentation
29
- Leveraging Python type hints for validation
30
- Building microservices with async support
31
- Integrating Pydantic for data validation
32
33
## Quick Start
34
35
Minimal working example:
36
37
```python
38
# main.py
39
from fastapi import FastAPI, HTTPException, status
40
from fastapi.middleware.cors import CORSMiddleware
41
from contextlib import asynccontextmanager
42
import logging
43
44
# Setup logging
45
logging.basicConfig(level=logging.INFO)
46
logger = logging.getLogger(__name__)
47
48
# Create FastAPI instance
49
app = FastAPI(
50
title="API Service",
51
description="A modern FastAPI application",
52
version="1.0.0",
53
docs_url="/api/docs",
54
openapi_url="/api/openapi.json"
55
)
56
57
# Add CORS middleware
58
app.add_middleware(
59
CORSMiddleware,
60
allow_origins=["http://localhost:3000"],
61
allow_credentials=True,
62
allow_methods=["*"],
63
// ... (see reference guides for full implementation)
64
```
65
66
## Reference Guides
67
68
Detailed implementations in the `references/` directory:
69
70
| Guide | Contents |
71
|---|---|
72
| [FastAPI Application Setup](references/fastapi-application-setup.md) | FastAPI Application Setup |
73
| [Pydantic Models for Validation](references/pydantic-models-for-validation.md) | Pydantic Models for Validation |
74
| [Async Database Models and Queries](references/async-database-models-and-queries.md) | Async Database Models and Queries |
75
| [Security and JWT Authentication](references/security-and-jwt-authentication.md) | Security and JWT Authentication |
76
| [Service Layer for Business Logic](references/service-layer-for-business-logic.md) | Service Layer for Business Logic |
77
| [API Routes with Async Endpoints](references/api-routes-with-async-endpoints.md) | API Routes with Async Endpoints |
78
79
## Best Practices
80
81
### ✅ DO
82
83
- Use async/await for I/O operations
84
- Leverage Pydantic for validation
85
- Use dependency injection for services
86
- Implement proper error handling with HTTPException
87
- Use type hints for automatic OpenAPI documentation
88
- Create service layers for business logic
89
- Implement authentication on protected routes
90
- Use environment variables for configuration
91
- Return appropriate HTTP status codes
92
- Document endpoints with docstrings and tags
93
94
### ❌ DON'T
95
96
- Use synchronous database operations
97
- Trust user input without validation
98
- Store secrets in code
99
- Ignore type hints
100
- Return database models in responses
101
- Implement authentication in route handlers
102
- Use mutable default arguments
103
- Forget to validate query parameters
104
- Expose stack traces in production
105