
fastapi
by alijilani-dev
Claude code developments.
SKILL.md
name: fastapi description: Comprehensive FastAPI development skill for building APIs from hello world to production-ready applications. Use when building REST APIs, microservices, ML/AI APIs, or full-stack applications with FastAPI. Covers project scaffolding, database integration (PostgreSQL, MySQL, MongoDB, SQLite), testing, Docker deployment, background tasks, file uploads, and production best practices.
FastAPI Development
Build production-ready APIs with FastAPI, from simple hello world to complex microservices.
Overview
This skill provides everything needed to build FastAPI applications:
- Project scaffolding - Generate project structures automatically
- Database integration - PostgreSQL, MySQL, MongoDB, SQLite patterns
- Testing - Pytest patterns and fixtures
- Deployment - Docker, docker-compose, production optimization
- Advanced features - Background tasks, file uploads, async operations
- Templates - Ready-to-use boilerplate code
Quick Start Decision Tree
Choose your starting point based on what you're building:
First time with FastAPI?
→ Use assets/hello-world/main.py template (5 minutes)
→ Run fastapi dev main.py and explore http://localhost:8000/docs
Building a simple CRUD API?
→ Use assets/crud-api-template/ (15 minutes)
→ Modify schemas in schemas.py for your data model
Starting a new project from scratch?
→ Use scripts/scaffold_project.py to generate full project structure
→ Choose project type: simple-api, microservice, fullstack, or ml-api
Adding specific features to existing project?
→ Database: See references/databases.md
→ Testing: See references/testing.md
→ Docker: See references/docker.md
→ Background tasks: See references/background_tasks.md
→ File uploads: See references/file_uploads.md
Building Your First API
Option 1: Hello World (Fastest)
Copy assets/hello-world/main.py to your project:
cp assets/hello-world/main.py .
pip install -r assets/hello-world/requirements.txt
fastapi dev main.py
Option 2: CRUD API Template
Copy the CRUD template for a complete REST API:
cp -r assets/crud-api-template/* .
pip install -r requirements.txt
fastapi dev main.py
This gives you:
- Complete CRUD operations
- Pydantic validation
- API documentation
- Error handling
Option 3: Scaffold a New Project
Generate a complete project structure:
python scripts/scaffold_project.py my-api --type simple-api --database postgresql
Project types:
simple-api- Basic REST API with CRUDmicroservice- Microservice with health checksfullstack- Full-stack with frontend integrationml-api- ML/AI API with model serving
Options:
--database- postgresql, mysql, sqlite, mongodb--docker- Include Docker configuration--testing- Include pytest setup
Adding Database Integration
Quick Database Setup
For PostgreSQL, MySQL, or SQLite (SQL databases):
- Read
references/databases.mdfor your database type - Copy the database configuration code
- Define models with SQLAlchemy
- Set up migrations with Alembic
For MongoDB (NoSQL):
- Read
references/databases.mdMongoDB section - Install Beanie:
pip install motor beanie - Define document models
- Initialize database on startup
Common Pattern
# 1. Configure database (references/databases.md)
# 2. Define models
# 3. Create CRUD service functions
# 4. Use in endpoints with Depends(get_db)
The scaffolding script (scripts/scaffold_project.py) automatically generates this structure when you specify --database.
Adding Features
Testing
To add comprehensive testing to your project:
- Read
references/testing.md - Install:
pip install pytest pytest-asyncio httpx - Create
tests/conftest.pywith test client fixture - Write test files in
tests/directory - Run:
pytest --cov=app tests/
Key patterns from testing.md:
- Test client setup
- Database fixtures
- Async testing
- Mocking external services
Docker Deployment
To containerize your application:
- Read
references/docker.md - Create Dockerfile (see multi-stage build example)
- Create docker-compose.yml
- Build:
docker-compose up --build
For production:
- Use multi-stage builds
- Run as non-root user
- Add health checks
- Use gunicorn with uvicorn workers
Background Tasks
For async job processing:
Simple tasks (same process):
- Use FastAPI's built-in
BackgroundTasks - See
references/background_tasks.md→ FastAPI Background Tasks
Production tasks (distributed):
- Use Celery + Redis
- See
references/background_tasks.md→ Celery section - Includes task monitoring with Flower
Modern async tasks:
- Use ARQ (async task queue)
- See
references/background_tasks.md→ ARQ section
File Uploads
To handle file uploads:
- Read
references/file_uploads.md - Choose your pattern:
- Single file: Basic pattern
- Multiple files: Batch upload
- Large files: Streaming with progress
- Images: Processing with Pillow
- CSV/Excel: pandas integration
Key patterns:
- File validation (type, size)
- Unique filenames
- Organized storage
- Cloud storage (S3)
Production Deployment
Checklist
Before deploying to production:
-
Database
- Use production database (not SQLite)
- Set up connection pooling
- Configure backups
- Run migrations
-
Security
- Use environment variables for secrets
- Enable CORS properly
- Add authentication (JWT, OAuth2)
- Validate all inputs
-
Performance
- Use gunicorn with uvicorn workers
- Configure worker count based on CPU cores
- Add caching (Redis)
- Optimize database queries
-
Monitoring
- Add health check endpoint
- Configure logging
- Set up error tracking
- Monitor background tasks
-
Docker
- Use production Dockerfile (see
references/docker.md) - Set resource limits
- Configure restart policies
- Use docker-compose for multi-service setup
- Use production Dockerfile (see
Production Dockerfile Pattern
See references/docker.md → Production Optimization for:
- Multi-stage builds
- Non-root user
- Gunicorn configuration
- Health checks
Environment Configuration
# app/core/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
DATABASE_URL: str
REDIS_URL: str
SECRET_KEY: str
class Config:
env_file = ".env"
settings = Settings()
Resources
Scripts
scaffold_project.py - Generate complete project structures
- Supports 4 project types
- Configurable database backend
- Optional Docker and testing setup
- Run:
python scripts/scaffold_project.py --help
References
databases.md - Complete database integration guide
- PostgreSQL (sync & async)
- MySQL
- SQLite
- MongoDB with Beanie
- SQLAlchemy patterns
- Migrations with Alembic
testing.md - Comprehensive testing patterns
- Test client setup
- Database fixtures
- Async testing
- Mocking patterns
- Authentication testing
- File upload testing
- Coverage reporting
docker.md - Docker deployment guide
- Basic Dockerfile
- Multi-stage builds
- docker-compose configurations
- Production optimization
- Health checks
- Logging
background_tasks.md - Async job processing
- FastAPI BackgroundTasks
- Celery + Redis setup
- ARQ (async task queue)
- Scheduled tasks
- Task monitoring
file_uploads.md - File handling patterns
- Single & multiple uploads
- File validation
- Large file streaming
- Image processing
- CSV/Excel handling
- Cloud storage (S3)
Assets
hello-world/ - Minimal FastAPI application
- Single file setup
- Perfect for learning
- 3 example endpoints
crud-api-template/ - Complete CRUD API
- Full REST API implementation
- Pydantic schemas
- Error handling
- Pagination
- Easy to customize
Common Workflows
Building a Simple API
- Copy CRUD template:
cp -r assets/crud-api-template/* . - Modify
schemas.pywith your data model - Update
main.pywith your business logic - Run:
fastapi dev main.py - Test at http://localhost:8000/docs
Building with Database
- Scaffold project:
python scripts/scaffold_project.py myapi --database postgresql - Configure
.envwith database credentials - Define models in
app/models/ - Create schemas in
app/schemas/ - Implement CRUD in
app/services/ - Add endpoints in
app/api/endpoints/ - Run migrations:
alembic upgrade head
Adding to Existing Project
Add Database:
- Read
references/databases.mdfor your DB type - Install dependencies
- Create
app/core/database.pywith configuration - Define models
- Add
get_dbdependency to endpoints
Add Testing:
- Read
references/testing.md - Install pytest dependencies
- Create
tests/conftest.py - Write tests in
tests/ - Run with
pytest
Add Docker:
- Read
references/docker.md - Create Dockerfile
- Create docker-compose.yml
- Build and run:
docker-compose up
Deploying to Production
- Review production checklist above
- Create production Dockerfile (see
references/docker.md) - Set up docker-compose with all services
- Configure environment variables
- Run database migrations
- Start with:
docker-compose -f docker-compose.prod.yml up -d - Monitor with health checks and logging
Best Practices
- Always use type hints - FastAPI uses them for validation
- Separate concerns - Keep models, schemas, and services separate
- Use dependency injection - For database sessions, auth, etc.
- Validate inputs - Let Pydantic handle it with schemas
- Handle errors properly - Use HTTPException with clear messages
- Test thoroughly - Aim for high coverage on critical paths
- Use async when needed - For I/O-bound operations
- Keep it simple - Don't over-engineer early
- Read the docs - Use references for detailed patterns
- Use the scaffolding - Don't start from scratch every time
スコア
総合スコア
リポジトリの品質指標に基づく評価
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
レビュー
レビュー機能は近日公開予定です