This file provides guidance to WARP (warp.dev) when working with code in this repository.
# Activate virtual environment (required for all development)
.\.venv\Scripts\Activate.ps1
# Install/update dependencies
.\.venv\Scripts\pip.exe install -e .
# Install dev dependencies
.\.venv\Scripts\pip.exe install -e ".[dev]"# Standard way to run any FastAPI app (replace PROJECT_NAME)
.\.venv\Scripts\uvicorn.exe "PROJECT_NAME.main:app" --reload --host 0.0.0.0 --port 8000
# Examples for specific projects:
.\.venv\Scripts\uvicorn.exe "CRUD Operation With SQLAlchemy.main:app" --reload
.\.venv\Scripts\uvicorn.exe "JWT Authentication In FastAPI.main:app" --reload
.\.venv\Scripts\uvicorn.exe "FastAPI With MongoDB.main:app" --reload
# For projects with custom entry points
.\.venv\Scripts\python.exe ".\JWT Authentication In FastAPI\main.py"# Run all tests
.\.venv\Scripts\pytest.exe
# Run tests for specific project
.\.venv\Scripts\pytest.exe ".\Pytest Testing With FastAPI\"
# Run tests with coverage
.\.venv\Scripts\pytest.exe --cov=. --cov-report=html
# Run single test file
.\.venv\Scripts\pytest.exe ".\Pytest Testing With FastAPI\test_api.py" -v# Code formatting
.\.venv\Scripts\black.exe .
# Linting
.\.venv\Scripts\ruff.exe check .
.\.venv\Scripts\ruff.exe check --fix .
# Type checking
.\.venv\Scripts\mypy.exe .This repository contains 35+ independent FastAPI demonstration projects, each showcasing specific patterns and use cases:
- Authentication & Authorization: JWT Authentication, Role Base Authentication, Resource Access Management
- Database Integration: CRUD Operation With SQLAlchemy, FastAPI With MongoDB, Fastapi Postgresql Docker
- Performance & Caching: Caching in FastAPI Applications, Redis in FastAPI, Pagination with FastAPI
- Background Processing: Celery in FastAPI, Fastapi Background Tasks, Schedule Your Job with Apscheduler
- API Enhancement: Fastapi Documentation, Fastapi Metadata Configuration, SSE in FastAPI Application
- Infrastructure: Fastapi with Middleware, Exceptions In FastAPI, FastAPI with Python Logging
- Real-time Features: Websocket in Fastapi, Slow-Polling FastAPI
- Testing & Quality: Pytest Testing With FastAPI, Profiling In FastAPI
pyproject.toml- Main dependency management using modern Python packaginguv.lock- Lock file for reproducible dependencies.venv/- Virtual environment (use this for all Python commands)requirements.txt- Legacy requirements file (mostly empty, use pyproject.toml)
Most projects follow this pattern (using "CRUD Operation With SQLAlchemy" as reference):
Project Name/
├── main.py # FastAPI app instance and endpoint definitions
├── models.py # SQLAlchemy database models (if using SQLAlchemy)
├── schemas.py # Pydantic models for request/response validation
├── databases.py # Database connection and session management
├── __init__.py # Makes directory a Python package
├── README.md # Project-specific documentation
└── test_*.py # Test files (if present)
1. Simple Single-File Apps
# Direct approach - everything in main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello World"}2. Layered Architecture (Recommended)
# main.py - FastAPI app and routes only
from fastapi import FastAPI, Depends
from .schemas import ItemCreate, Item
from .models import get_db
app = FastAPI()
@app.post("/items")
def create_item(item: ItemCreate, db: Session = Depends(get_db)) -> Item:
# Business logic here3. Class-Based Handlers (JWT Authentication pattern)
# Separation of concerns with handler classes
class AuthHandler:
def __init__(self):
self.pwd_context = CryptContext(schemes=["bcrypt"])
async def authenticate_user(self, username: str, password: str):
# Authentication logic4. Middleware Integration
# Custom middleware for cross-cutting concerns
app.add_middleware(ResourceBaseMiddleware)- Most SQLAlchemy projects use in-memory SQLite for demos
- Database tables auto-create on startup via
@app.on_event("startup") - Check
databases.pyfor connection strings and session management
# For Docker-enabled projects (e.g., "Fastapi Postgresql Docker")
docker-compose up --build
# Access at http://localhost:8000
# PostgreSQL at localhost:5432- Import Errors: Ensure you're in
.venvand project root directory - Database Errors: Check if database containers are running (for Docker projects)
- Port Conflicts: Default port 8000, change with
--port 8001 - Cookie Expiry: JWT projects handle token expiration - check auth handlers for refresh logic
# Run with debug logging
.\.venv\Scripts\uvicorn.exe "main:app" --reload --log-level debug
# Interactive API docs always at /docs
# Alternative docs at /redoc- Pytest Testing With FastAPI - Complete testing example
- Exceptions In FastAPI - Error handling tests
# Standard test setup pattern
from fastapi.testclient import TestClient
from .main import app
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == "server is running"# Override database dependency for testing
from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool
DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(DATABASE_URL, poolclass=StaticPool)
def override_get_db():
database = TestingSessionLocal()
try:
yield database
finally:
database.close()
app.dependency_overrides[get_db] = override_get_db# Test all database-related projects
.\.venv\Scripts\pytest.exe -k "CRUD|MongoDB|Postgresql" -v
# Test authentication projects
.\.venv\Scripts\pytest.exe -k "JWT|Auth" -v
# Test with specific markers (if defined)
.\.venv\Scripts\pytest.exe -m "integration" -v# Standard SQLAlchemy setup (from CRUD example)
from sqlalchemy.orm import Session
def get_db():
database = SessionLocal()
try:
yield database
finally:
database.close()# From JWT Authentication project
async def auth_wrapper(self, auth: HTTPAuthorizationCredentials = Security(security)):
token = auth.credentials
if not token or await self.istokenblock(token):
raise HTTPException(status_code=401, detail="Not authenticated")
# Decode and validate token...# From Exceptions project
from fastapi import HTTPException
@app.exception_handler(CustomException)
async def custom_exception_handler(request: Request, exc: CustomException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.message}
)# From Background Tasks project
from fastapi import BackgroundTasks
@app.post("/send-email/")
async def send_email(background_tasks: BackgroundTasks):
background_tasks.add_task(send_email_task, email_data)
return {"message": "Email will be sent"}# Common pattern across projects
from fastapi import Depends
async def get_current_user(token: str = Depends(oauth2_scheme)):
# User validation logic
return user
@app.get("/protected")
async def protected_route(current_user: User = Depends(get_current_user)):
return {"user": current_user}- "CRUD Operation With SQLAlchemy" - Best example of clean layered architecture
- "Pytest Testing With FastAPI" - Complete testing patterns and setup
- "JWT Authentication In FastAPI" - Production-ready auth implementation
- "Fastapi Postgresql Docker" - Docker containerization best practices
- "Fastapi with Middleware" - Custom middleware implementation
Many projects require additional services:
- Redis projects: Need Redis server running
- Celery projects: Require message broker (RabbitMQ/Redis)
- MongoDB projects: Need MongoDB instance
- PostgreSQL projects: Database server required
- Use
--reloadfor development to see changes immediately - FastAPI automatically opens to windowed output showing request logs
- Interactive documentation always available at
/docsendpoint for testing API calls - Use
pytest -vfor verbose test output in window mode