Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/fastapi.mdc
Open on GitHub

What 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Backend and APIs →

From the same set210

gamemaker-gml Guides Cursor with GameMaker Language conventions for objects, scripts, and rooms. Keeps GML projects performant and prevents common memory leaks. ★ 41,024repo stars fortran A Cursor rule for modern Fortran practices covering modules, types, arrays, builds, and tests. Use it when working on scientific Fortran code. ★ 41,024repo stars flutter-development-guidelines-cursorrules-prompt-file Locks down folder layout, naming and Dart style for Flutter apps built on MVVM with Riverpod. Useful when the agent scatters files across the project. ★ 41,024repo stars flutter-app-expert-cursorrules-prompt-file Configures Cursor for Flutter 3.x development with Clean Architecture, BLoC, and Material 3 design. Guides code structure and testing practices. ★ 41,024repo stars es-module-nodejs-guidelines-cursorrules-prompt-fil Cursor rule for Node.js development with ES module syntax. It enforces short responses, prioritizes task steps, and controls verbosity via Vx flags. ★ 41,024repo stars engineering-ticket-template-cursorrules-prompt-file This Cursor rule turns development requests into structured engineering tickets with implementation details, acceptance criteria, and testing notes. ★ 41,024repo stars embedded-stm32-hal A Cursor rule that keeps the agent inside embedded C/C++ conventions: STM32 HAL, interrupts, DMA and tight memory budgets. ★ 41,024repo stars elixir-phoenix-docker-setup-cursorrules-prompt-fil Configures Cursor for Elixir and Phoenix development inside Docker. Enforces senior engineering practices, Conventional Commits, and quick replies via the VV prefix. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor