FastAPI Python API Development Skill
Build high-performance async REST APIs with FastAPI, Python's fastest-growing web framework. Automatic OpenAPI/Swagger documentation, type-safe validation with Pydantic, native async/await support, and dependency injection for clean architecture.
Open the source and read safety notes before installing.
Prerequisites
- Python 3.9+
- fastapi ^0.104.0
- uvicorn[standard] ^0.24.0
- pydantic ^2.0.0
- ASGI server (uvicorn, hypercorn, or daphne) for running FastAPI applications in production
- Database driver for async operations (asyncpg for PostgreSQL, aiomysql for MySQL, or motor for MongoDB) when using database integration
Schema details
- Install type
- package
- Reading time
- 6 min
- Difficulty score
- 100
- Troubleshooting
- Yes
- Breaking changes
- No
- Download URL
- /downloads/skills/fastapi-python-backend.zip
- Package verified
- Yes
- SHA-256
- a62b1a1e1fcee8faa7b1fb5cd68fa3c5112a74337fea3729a6bc5738c24f36d9
- Skill type
- general
- Skill level
- advanced
- Verification
- draft
- Verified at
- 2025-10-23
| Platform | Support | Install path |
|---|---|---|
| claude-code | Native | .claude/skills/<skill-name>/SKILL.md |
| codex | Native | .agents/skills/<skill-name>/SKILL.md |
| windsurf | Native | .windsurf/skills/<skill-name>/SKILL.md |
| gemini | Native | .gemini/skills/<skill-name>/SKILL.md or .agents/skills/<skill-name>/SKILL.md |
| cursor | Adapter | .cursor/rules/<skill-name>.mdc |
| cli | Manual | AGENTS.md or tool-specific context file |
Full copyable content
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from typing import List
app = FastAPI(title="Blog API", version="1.0.0")
# Pydantic models
class PostCreate(BaseModel):
title: str = Field(..., min_length=1, max_length=200)
content: str = Field(..., min_length=1)
published: bool = False
class Post(PostCreate):
id: int
class Config:
from_attributes = True
# In-memory storage (use database in production)
posts_db: List[Post] = []
post_id_counter = 1
@app.get("/posts", response_model=List[Post], tags=["posts"])
async def list_posts():
"""Get all blog posts"""
return posts_db
@app.post("/posts", response_model=Post, status_code=201, tags=["posts"])
async def create_post(post: PostCreate):
"""Create a new blog post"""
global post_id_counter
new_post = Post(id=post_id_counter, **post.dict())
posts_db.append(new_post)
post_id_counter += 1
return new_post
@app.get("/posts/{post_id}", response_model=Post, tags=["posts"])
async def get_post(post_id: int):
"""Get a single post by ID"""
for post in posts_db:
if post.id == post_id:
return post
raise HTTPException(status_code=404, detail="Post not found")
@app.delete("/posts/{post_id}", status_code=204, tags=["posts"])
async def delete_post(post_id: int):
"""Delete a post by ID"""
for i, post in enumerate(posts_db):
if post.id == post_id:
posts_db.pop(i)
return
raise HTTPException(status_code=404, detail="Post not found")
# Run with: uvicorn main:app --reload
# Docs at: http://localhost:8000/docsAbout this resource
What This Skill Enables
Claude can build production-ready REST APIs using FastAPI, Python's fastest web framework with automatic OpenAPI documentation and async support. FastAPI combines Python 3.9+ type hints with Pydantic validation for type-safe APIs that rival Node.js performance. With automatic interactive docs, dependency injection, and async/await, FastAPI eliminates boilerplate while maintaining enterprise-grade reliability.
Compatibility
Native
- Claude Code / Claude: native skill usage via
SKILL.md. - Codex/OpenAI workflows: compatible with Agent Skills-style
SKILL.mdcontent as reusable workflow instructions.
Manual Adaptation
- Gemini CLI: native skill usage via
.gemini/skills/<skill-name>/SKILL.mdor.agents/skills/<skill-name>/SKILL.mdwhere supported. - Cursor: use the generated
.cursor/rules/*.mdcadapter for project rules. - OpenClaw and similar agents: use the same skill content as a reusable prompt/workflow file when native skill import is unavailable.
Prerequisites
Required:
- Claude Pro subscription or Claude Code CLI
- Python 3.9+ (3.11+ recommended for best performance)
- pip or poetry package manager
- Basic Python knowledge
What Claude handles automatically:
- Writing FastAPI route handlers with type hints
- Creating Pydantic models for request/response validation
- Adding automatic OpenAPI/Swagger documentation
- Implementing async endpoints for I/O-bound operations
- Setting up dependency injection for auth/database
- Handling errors with custom exception handlers
- Adding middleware for CORS, logging, rate limiting
- Structuring large applications with routers
How to Use This Skill
Basic CRUD API
Prompt: "Create a FastAPI CRUD API for blog posts with: GET /posts (list), POST /posts (create), GET /posts/{id} (get one), PUT /posts/{id} (update), DELETE /posts/{id} (delete). Use Pydantic for validation."
Claude will:
- Create FastAPI app with routers
- Define Pydantic models for Post
- Implement all 5 CRUD endpoints
- Add automatic validation
- Include OpenAPI docs at /docs
- Add error handling for 404s
- Return proper HTTP status codes
Database Integration with SQLAlchemy
Prompt: "Build FastAPI app with PostgreSQL using SQLAlchemy ORM. Include: user authentication, database models for users/posts, async queries, connection pooling, and migrations with Alembic."
Claude will:
- Set up async SQLAlchemy engine
- Create database models with relationships
- Implement dependency injection for sessions
- Add async CRUD operations
- Include connection pooling config
- Set up Alembic for migrations
- Add authentication dependencies
Authentication & Authorization
Prompt: "Create FastAPI auth system with JWT tokens. Include: user registration, login, password hashing with bcrypt, protected routes with dependencies, refresh tokens, and role-based access control."
Claude will:
- Implement password hashing with passlib
- Create JWT token generation/validation
- Add OAuth2 password bearer scheme
- Build auth dependencies for routes
- Implement refresh token rotation
- Add role-based access decorators
- Include user registration/login endpoints
Async Background Tasks
Prompt: "Build FastAPI endpoint that processes uploaded CSV file in background. Include: file upload validation, background task with progress tracking, webhook notification on completion, and error handling."
Claude will:
- Create file upload endpoint
- Validate CSV format with Pydantic
- Launch background task
- Add progress tracking with Redis
- Implement webhook callback
- Handle errors gracefully
- Return task ID for status checks
Tips for Best Results
Use Type Hints Everywhere: FastAPI relies on Python type hints for automatic validation and docs. Always specify return types and use
Optional[T]for nullable fields.Async for I/O-Bound: Use
async deffor endpoints that do database queries, API calls, or file operations. FastAPI handles concurrency automatically.Pydantic for Validation: Create Pydantic models for request bodies. Never parse JSON manually - let Pydantic handle validation and serialization.
Dependency Injection: Use FastAPI's
Depends()for auth, database sessions, pagination, etc. This keeps endpoints clean and testable.Status Codes Matter: Use proper HTTP status codes: 201 for created, 204 for deleted, 404 for not found, 422 for validation errors.
OpenAPI Customization: Add descriptions, examples, and tags to endpoints for better automatic documentation.
Common Workflows
E-Commerce API
"Build FastAPI e-commerce backend:
1. Products: CRUD with search, filtering, pagination
2. Cart: session-based with Redis, add/remove items, calculate totals
3. Orders: create order, payment processing, order history
4. Auth: JWT-based customer accounts with OAuth2
5. Admin: protected endpoints for inventory management
6. Webhooks: Stripe payment notifications
7. Database: PostgreSQL with async SQLAlchemy
8. Background tasks: order confirmation emails"
Real-Time Analytics API
"Create FastAPI analytics dashboard backend:
1. Ingest endpoint: accept events via POST with Pydantic validation
2. Time-series storage: TimescaleDB for event data
3. Aggregation endpoints: metrics by day/week/month
4. WebSocket: real-time updates for live dashboard
5. Caching: Redis for frequently-accessed metrics
6. Rate limiting: per-API-key limits with middleware
7. Export: CSV/JSON download of filtered data
8. Admin API: user management, API key generation"
Multi-Tenant SaaS API
"Build multi-tenant SaaS API with FastAPI:
1. Tenant isolation: subdomain-based routing
2. Database: separate schemas per tenant in PostgreSQL
3. Auth: tenant-scoped JWT tokens
4. Billing: usage tracking, quota enforcement
5. API versioning: /v1/, /v2/ with deprecation notices
6. Rate limiting: per-tenant quotas
7. Webhooks: tenant-configurable event notifications
8. Admin: tenant provisioning, feature flags"
GraphQL + REST Hybrid
"Create FastAPI app with both REST and GraphQL:
1. REST endpoints: CRUD operations with OpenAPI docs
2. GraphQL: Strawberry integration for complex queries
3. DataLoader: batching and caching for N+1 queries
4. Auth: shared JWT validation across REST/GraphQL
5. Subscriptions: WebSocket support for real-time data
6. File uploads: multipart form data handling
7. Error handling: unified error format
8. Testing: pytest fixtures for both APIs"
Troubleshooting
Issue: "Pydantic validation errors not showing custom messages"
Solution: Add Field() with description: email: str = Field(..., description='Valid email required'). Use @validator decorator for complex rules. Check Pydantic V2 migration if using FastAPI 0.100+.
Issue: "Async endpoints slower than sync" Solution: Only use async for I/O operations (database, HTTP calls). CPU-bound tasks should use sync functions. Ensure database driver supports async (asyncpg, aiomysql). Check connection pool settings.
Issue: "CORS errors in browser"
Solution: Add CORSMiddleware with correct origins: app.add_middleware(CORSMiddleware, allow_origins=['http://localhost:3000'], allow_credentials=True). Don't use wildcard in production.
Issue: "Dependency injection not working"
Solution: Ensure dependencies return values, not None. Use Depends() in function signature, not inside function body. Check dependency order - dependencies can depend on other dependencies.
Issue: "File uploads failing with 413 error"
Solution: Increase max upload size in uvicorn: uvicorn.run(app, limit_concurrency=100, limit_max_requests=1000, timeout_keep_alive=30). For large files, use streaming with UploadFile.
Learn More
- FastAPI Official Documentation
- FastAPI GitHub Repository
- Pydantic V2 Documentation
- SQLAlchemy Async Tutorial
- FastAPI Full Stack Template
- FastAPI Best Practices
Features
- Automatic OpenAPI/Swagger documentation generation
- Native async/await support for high concurrency
- Pydantic integration for request/response validation
- Dependency injection system for clean architecture
- Type-safe APIs with automatic request/response validation
- WebSocket support for real-time bidirectional communication
- Built-in testing utilities with TestClient for API testing
- Background task processing with BackgroundTasks for async operations, task queue integration, and long-running job management without blocking request responses
Use Cases
- REST API backends with automatic documentation
- Microservices with async database operations
- Real-time APIs with WebSocket support
- High-performance microservices with async database operations
- API-first applications with automatic OpenAPI documentation
- Real-time applications with WebSocket and Server-Sent Events
- What This Skill Enables
- Compatibility
- Native
- Manual Adaptation
- Prerequisites
- How to Use This Skill
- Basic CRUD API
- Database Integration with SQLAlchemy
- Authentication & Authorization
- Async Background Tasks
- Tips for Best Results
- Common Workflows
- E-Commerce API
- Real-Time Analytics API
- Multi-Tenant SaaS API
- GraphQL + REST Hybrid
Source citations
Signals
Loading live community signals…
A short, calm digest of reviewed Claude resources. Unsubscribe any time.