fastapi — Cursor rule: what it does, how to install
A Cursor rule for FastAPI work in Python. It guides project structure, API design, models, security, database code, testing, and deployment.
.cursor/rules/fastapi.mdcWhat fastapi does
This rule gives Cursor guidance while it works on FastAPI code. It covers module layout, domain-based routes, dependency injection, middleware, configuration, HTTP methods, status codes, request and response models, validation, error handling, and OpenAPI documentation.
The guidance also addresses Pydantic models, SQLAlchemy, migrations, transactions, JWT, OAuth2, CORS, rate limiting, async work, background tasks, and tests. It includes sections for Docker, CI/CD, logging, monitoring, docstrings, and API versioning.
The rule is not applied across the whole project because alwaysApply: false. Cursor matches it against **/*.py, app/**/*.py, api/**/*.py, so files outside those patterns do not receive these rule instructions.
What it helps with
- organize FastAPI modules by domain
- define validated Pydantic request models
- handle API errors and status codes
- review JWT, OAuth2, and CORS setup
- prepare tests, Docker, and CI/CD
Example prompts for the agent
- Review this FastAPI project and suggest a domain-based route structure.
- Create Pydantic request and response models for this API with input validation.
- Inspect this handler for incorrect status codes, error handling, or async usage.
How to install fastapi
Manually: put the files in place
.cursor/rules/fastapi.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/fastapi.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/fastapi.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/fastapi.mdManually: put the files in place
.clinerules/fastapi.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about fastapi
How do I install fastapi in Cursor?
Create or add the rule file at .cursor/rules/fastapi.mdc.
What does fastapi do?
It guides Cursor through FastAPI project structure, API models, database work, authentication, security, testing, deployment, and documentation.
Which files does the rule cover?
Its patterns are **/*.py, app/**/*.py, and api/**/*.py. With alwaysApply: false, Cursor uses those globs rather than applying the rule to every project file.
Source text
fastapi.mdc · CC0-1.0
--- description: "FastAPI best practices and patterns for building modern Python web APIs" globs: **/*.py, app/**/*.py, api/**/*.py alwaysApply: false --- # FastAPI Best Practices ## Project Structure - Use proper directory structure - Implement proper module organization - Use proper dependency injection - Keep routes organized by domain - Implement proper middleware - Use proper configuration management ## API Design - Use proper HTTP methods - Implement proper status codes - Use proper request/response models - Implement proper validation - Use proper error handling - Document APIs with OpenAPI ## Models - Use Pydantic models - Implement proper validation - Use proper type hints - Keep models organized - Use proper inheritance - Implement proper serialization ## Database - Use proper ORM (SQLAlchemy) - Implement proper migrations - Use proper connection pooling - Implement proper transactions - Use proper query optimization - Handle database errors properly ## Authentication - Implement proper JWT authentication - Use proper password hashing - Implement proper role-based access - Use proper session management - Implement proper OAuth2 - Handle authentication errors properly ## Security - Implement proper CORS - Use proper rate limiting - Implement proper input validation - Use proper security headers - Handle security errors properly - Implement proper logging ## Performance - Use proper caching - Implement proper async operations - Use proper background tasks - Implement proper connection pooling - Use proper query optimization - Monitor performance metrics ## Testing - Write proper unit tests - Implement proper integration tests - Use proper test fixtures - Implement proper mocking - Test error scenarios - Use proper test coverage ## Deployment - Use proper Docker configuration - Implement proper CI/CD - Use proper environment variables - Implement proper logging - Use proper monitoring - Handle deployment errors properly ## Documentation - Use proper docstrings - Implement proper API documentation - Use proper type hints - Keep documentation updated - Document error scenarios - Use proper versioning
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules