Home › Python / FastAPI › FastAPI Windsurf rules

FastAPI Windsurf rules

Windsurf rules for FastAPI apps with Pydantic v2, SQLAlchemy 2.0 async, dependency injection, and pytest.

WindsurfPython / FastAPI

How to use

Save this as .windsurf/rules/fastapi.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.windsurf/rules/fastapi.mdDownload  
---
trigger: glob
globs: **/*.py
---

# FastAPI

- Routers per feature in `app/<feature>/router.py` with `APIRouter(prefix="/orders", tags=["orders"])`; schemas in `schemas.py`; logic in `service.py`.
- Request and response models are Pydantic v2 (`model_config = ConfigDict(from_attributes=True)`); set `response_model` or a return annotation on every endpoint so output is filtered.
- Use `Annotated` dependencies: `SessionDep = Annotated[AsyncSession, Depends(get_session)]`, `CurrentUser = Annotated[User, Depends(get_current_user)]`.
- Async endpoints only call async libraries (async SQLAlchemy, `httpx.AsyncClient`). Blocking work goes in sync `def` endpoints or `run_in_threadpool`.
- SQLAlchemy 2.0 style `select()` with `scalars()`; `selectinload` for relations. Migrations with Alembic (`alembic revision --autogenerate -m ...`, then review).
- Errors: raise `HTTPException` with correct codes, or domain exceptions mapped by `app.exception_handler`.
- Settings via `pydantic-settings` `BaseSettings`, loaded once.
- Startup/shutdown in a `lifespan` context manager, not `@app.on_event`.
- Auth via OAuth2/JWT dependencies; never trust ids from the body for ownership.

## Done when
`ruff check . && pyright && pytest -q` pass, with endpoint tests using `httpx.AsyncClient(transport=ASGITransport(app))` and dependency overrides.

Related rules

Web accessibility (WCAG 2.2) rules

Windsurf rules for building accessible web UIs to WCAG 2.2 AA: semantics, keyboard, focus, contrast, and testing.

WindsurfAccessibility

Django Windsurf rules

Windsurf rules for Django 5.2 LTS projects: apps, models and migrations, ORM performance, forms, and tests.

WindsurfDjango

Documentation writing rules

Windsurf rules for writing and updating READMEs, docstrings, ADRs, and changelogs alongside code changes.

WindsurfAgent workflows

Electron app Windsurf rules

Windsurf rules for secure Electron 44 apps: context isolation, preload bridges, typed IPC, and packaging.

WindsurfElectron

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.