Skip to content
Cursor Rules Generator
Backend4 sections

FastAPI Cursor Rules Template

Generate production-ready FastAPI Cursor rules for Project Rules (.mdc), AGENTS.md, or legacy .cursorrules. This template encodes stack-specific conventions for projects using FastAPI. Modern Python async web framework with OpenAPI docs and Pydantic validation.

The Complete FastAPI Cursor Rules

The full rule set in all three formats Cursor understands — copy the one your project uses. Project Rules (.mdc) load per-file via globs, AGENTS.md is portable across AI tools, and .cursorrules is the legacy single-file format.

Replacing a legacy file? Read the guide to migrate .cursorrules to Cursor Project Rules (.mdc) before copying this template into your project.

.cursor/rules/fastapi-routes.mdc
---
description: "Routes & Endpoints"
alwaysApply: false
---
- Use path operation decorators (@app.get, @app.post, etc.).
- Use      spaces for indentation.
- Use snake_case for route handler functions.
- Use Pydantic models for request bodies and query parameters.
- Use path parameters with type hints for automatic validation.
- Use APIRouter for modular route organization with prefix and tags.
.cursor/rules/fastapi-validation.mdc
---
description: "Validation & Schemas"
alwaysApply: false
---
- Use Pydantic v2 models for all request/response schemas.
- Use Field() with validation constraints (min_length, gt, pattern, etc.).
- Define separate schemas for input (BaseModel) and output (Response model).
- Use response_model parameter for automatic response serialization.
- Use Annotated types with Query(), Path(), Body(), Header().
- Use model_validator for cross-field validation logic.
.cursor/rules/fastapi-deps.mdc
---
description: "Dependency Injection"
alwaysApply: false
---
- Use Depends() for reusable dependencies (DB sessions, auth, config).
- Use generator-based dependencies with yield for resource cleanup.
- Use dependency overrides for testing.
- Use sub-dependencies — dependencies that depend on other dependencies.
- Keep dependencies focused on a single concern.
- Use class-based dependencies via __call__ for stateful dependencies.
.cursor/rules/fastapi-error-handling.mdc
---
description: "Error Handling"
alwaysApply: false
---
- Use HTTPException for standard HTTP error responses.
- Define custom exception handlers with @app.exception_handler().
- Use status_code constants from fastapi.status.
- Return structured error responses with detail and optional headers.
- Use background tasks (BackgroundTasks) for non-critical side effects.
- Log exceptions with traceback in exception handlers.

Customize These Rules

The generator below is preloaded with the FastAPI template. Adjust indentation, quotes, naming, add your own rules, then download — no need to start from scratch on the homepage.

What This Template Covers

Required guidance

  • - Routes & Endpoints
  • - Validation & Schemas
  • - Dependency Injection
  • - Error Handling

Default style

  • - Indentation: 4 spaces
  • - Quotes: double
  • - Semicolons: disabled
  • - Naming: snake_case

Frequently Asked Questions

What does the FastAPI Cursor rules template include?
It includes 4 rule sections covering routes & endpoints, validation & schemas, dependency injection and other FastAPI development conventions.
Can I generate FastAPI Project Rules and AGENTS.md from the same template?
Yes. The generator can export the same stack-specific guidance as modern Project Rules (.mdc), AGENTS.md, or a legacy .cursorrules file.
Should I combine FastAPI with other Cursor rules templates?
Yes. You can combine FastAPI with related templates such as TypeScript, Tailwind CSS, Docker, or backend frameworks when your project uses multiple technologies.