CursorFastAPIAPI Design

FastAPI Cursor Rules

Cursor rules for FastAPI: Pydantic models, dependency injection, async patterns, and production API best practices.

.cursorrules
- Use Pydantic v2 models for all request/response schemas; separate input and output schemas
- Leverage FastAPI dependency injection for auth, DB sessions, and shared logic
- Use async def for all endpoints that do I/O (database, HTTP calls)
- Return appropriate HTTP status codes; use HTTPException with detail messages
- Add response_model to all endpoints for automatic serialization and docs
- Use APIRouter to organize routes by feature; avoid everything in main.py
- Implement lifespan context manager for startup/shutdown (not deprecated events)
- Use BackgroundTasks for fire-and-forget work that shouldn't block responses

How to use with Cursor

Create a `.cursorrules` file in your project root and paste these rules. Cursor reads this automatically on every AI interaction.

#fastapi#python#api#pydantic#cursor#coding-rules

Related Rules