Python · FastAPI · Pydantic

FastAPI rules for Cursor

Routing, validation and dependency-injection rules for FastAPI services.

The rule

Save as .cursor/rules/fastapi.mdc. Cursor picks it up on the next request.

.cursor/rules/fastapi.mdc

---
description: FastAPI service conventions
globs: **/*.py
alwaysApply: false
---

- Define request and response bodies as Pydantic models; set response_model on every route.
- Use Depends() for database sessions, auth and settings instead of globals.
- Use async def for I/O-bound routes and keep blocking calls out of them.
- Raise HTTPException with a clear status code and detail; do not return error dicts with 200.
- Group routes with APIRouter per domain and include them in the app factory.
- Read configuration from a settings model, never hard-code secrets.

How Cursor loads this rule

Read from the frontmatter above, per Cursor's rules documentation.

  • alwaysApply: false with globs: **/*.py: attached automatically when a matching file is in context.
  • Keep the .mdc extension. A plain .md file in .cursor/rules is ignored by the rules system.
  • Prefer to generate your own? Type /create-rule in Agent and describe what you want; Cursor saves the file to .cursor/rules.
  • Team Rules come first, then Project Rules like this one, then User Rules. Earlier sources win when guidance conflicts.

Cursor rules setup guideCursor Team RulesLet Cursor run commands without asking

Using another agent too?

The same instructions work as plain Markdown in AGENTS.md, which Codex, Gemini CLI, GitHub Copilot and most other coding agents read.

AGENTS.md (section)

## FastAPI

- Define request and response bodies as Pydantic models; set response_model on every route.
- Use Depends() for database sessions, auth and settings instead of globals.
- Use async def for I/O-bound routes and keep blocking calls out of them.
- Raise HTTPException with a clear status code and detail; do not return error dicts with 200.
- Group routes with APIRouter per domain and include them in the app factory.
- Read configuration from a settings model, never hard-code secrets.

More templates

Get Cursor rules and settings changes by email

When Cursor changes how rules, run modes or settings work, we update these templates from the docs and send a short note. One email a week at most.

Reviewed Oct 17, 2026. Written by AgentAtlas. Adapt it to your codebase before relying on it.