agentleFS
girijashankarjgirijashankarj/cursor-handbook/.cursor/rules/backend/api-design.mdc

cursor-handbook / backend

Cursor rule30 starsChanged 3 months agoMITView on GitHub

REST API design conventions and standards. Apply when designing or editing API endpoints.

The file

We split this api-design.mdc into the rules it gives an agent

10 rules. About 0 are common, 6 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use URL path versioning: /api/v1/, /api/v2/

    line 44
  2. Distinctive:

    Document breaking changes in changelog

    line 47
  3. Distinctive:

    Don't use 200 for errors — use 4xx/5xx with error body

    line 67
  4. Very specific:

    Don't nest resources > 2 levels — /api/v1/orders/123/items OK.

    line 68
  5. Distinctive:

    Don't return array without pagination — always paginate list endpoints

    line 69
  6. Distinctive:

    Don't use query params for sensitive data — use headers or body

    line 70
  7. Distinctive:

    Don't version in headers only — use URL path for cacheability

    line 71
  8. Distinctive:

    Bulk operations: Use POST with array; return 207 for partial success

    line 83
  9. Very specific:

    Use @handler-patterns for handler flow

    line 88
  10. Very specific:

    Use @create-handler skill for new endpoints

    line 89

≈ “Common” is an estimate: at least 1 in 5 of the 7,324 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
api-design.mdc
---
description: "REST API design conventions and standards. Apply when designing or editing API endpoints."
alwaysApply: false
globs: "**/api/**/*.ts,**/*.route*.ts,**/*.routes.ts,**/routes/**/*.ts"
---

# API Design Rules

## URL Structure
```
/api/v{version}/{resource}                    # Collection
/api/v{version}/{resource}/{id}               # Single resource
/api/v{version}/{resource}/{id}/{sub-resource} # Sub-resource
```

### HTTP Methods
| Method | Action | Response Code | Idempotent |
|--------|--------|--------------|------------|
| GET | Read | 200 | Yes |
| POST | Create | 201 | No |
| PUT | Full update | 200 | Yes |
| PATCH | Partial update | 200 | Yes |
| DELETE | Soft delete | 200 | Yes |

### Response Envelope
```json
{
  "data": {},
  "meta": {
    "correlationId": "abc-123",
    "timestamp": "2026-01-15T10:30:00Z",
    "pagination": {
      "page": 1,
      "pageSize": 20,
      "total": 150,
      "hasNextPage": true,
      "cursor": "eyJpZCI6MTIzfQ=="
    }
  }
}
```

## Versioning
- Use URL path versioning: `/api/v1/`, `/api/v2/`
- Support at most 2 active versions simultaneously
- Deprecation notice 6 months before version removal
- Document breaking changes in changelog

## Query Parameters
| Parameter | Purpose | Example |
|-----------|---------|---------|
| `page`, `pageSize` | Pagination | `?page=2&pageSize=20` |
| `cursor` | Cursor pagination | `?cursor=abc123` |
| `sort` | Sorting | `?sort=-created_at,name` |
| `filter` | Filtering | `?filter[status]=active` |
| `fields` | Field selection | `?fields=id,name,email` |
| `include` | Related resources | `?include=items,customer` |

## Rate Limiting
- Include rate limit headers in all responses:
  - `X-RateLimit-Limit`: Max requests per window
  - `X-RateLimit-Remaining`: Remaining requests
  - `X-RateLimit-Reset`: Window reset timestamp
- Return 429 when limit exceeded with `Retry-After` header

## Anti-patterns
- Don't use 200 for errors — use 4xx/5xx with error body
- Don't nest resources > 2 levels — `/api/v1/orders/123/items` OK; deeper = split
- Don't return array without pagination — always paginate list endpoints
- Don't use query params for sensitive data — use headers or body
- Don't version in headers only — use URL path for cacheability

## Examples
```json
// BAD: No envelope, no meta
["item1", "item2"]

// GOOD: Envelope with pagination
{ "data": [...], "meta": { "pagination": { "page": 1, "total": 100 } } }
```

## Edge Cases
- **Bulk operations**: Use POST with array; return 207 for partial success
- **Filtering empty**: `?filter[status]=` — treat as "no filter" not "empty string"
- **Sort invalid column**: Return 400 with allowed sort fields in error

## Related
- Use `@handler-patterns` for handler flow
- Use `@create-handler` skill for new endpoints

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.