AgentSkillsCN

rest-patterns

RESTful API 设计模式、HTTP 语义、缓存策略与限流机制的快速参考。适用场景包括:REST API、HTTP 方法、状态码、API 设计、端点设计、API 版本控制、限流策略、缓存头设置等。

SKILL.md
--- frontmatter
name: rest-patterns
description: "Quick reference for RESTful API design patterns, HTTP semantics, caching, and rate limiting. Triggers on: rest api, http methods, status codes, api design, endpoint design, api versioning, rate limiting, caching headers."
allowed-tools: "Read Write"

REST Patterns

Quick reference for RESTful API design patterns and HTTP semantics.

HTTP Methods

MethodPurposeIdempotentCacheable
GETRetrieve resource(s)YesYes
POSTCreate new resourceNoNo
PUTReplace entire resourceYesNo
PATCHPartial updateMaybeNo
DELETERemove resourceYesNo

Essential Status Codes

CodeNameUse
200OKSuccess with body
201CreatedPOST success (add Location header)
204No ContentSuccess, no body
400Bad RequestInvalid syntax
401UnauthorizedNot authenticated
403ForbiddenNot authorized
404Not FoundResource doesn't exist
422UnprocessableValidation error
429Too Many RequestsRate limited
500Server ErrorInternal failure

Resource Design

http
GET    /users              # List
POST   /users              # Create
GET    /users/{id}         # Get one
PUT    /users/{id}         # Replace
PATCH  /users/{id}         # Update
DELETE /users/{id}         # Delete

# Query parameters
GET /users?page=2&limit=20          # Pagination
GET /users?sort=created_at:desc     # Sorting
GET /users?role=admin               # Filtering

Security Checklist

  • HTTPS/TLS only
  • OAuth 2.0 or JWT for auth
  • Validate all inputs
  • Rate limit per client
  • CORS headers configured
  • No sensitive data in URLs
  • Use no-store for sensitive responses

Common Mistakes

MistakeFix
Verbs in URLs/getUsers/users
Deep nestingFlatten or use query params
200 for errorsUse proper 4xx/5xx
No paginationAlways paginate collections
Missing rate limitsProtect against abuse

Quick Reference

TaskPattern
Paginate?page=2&limit=20
Sort?sort=field:asc
Filter?status=active
Sparse fields?fields=id,name
Include related?include=orders

When to Use

  • Designing new API endpoints
  • Choosing HTTP methods and status codes
  • Implementing caching headers
  • Setting up rate limiting
  • Structuring error responses

Additional Resources

For detailed patterns, load:

  • ./references/status-codes.md - Complete status code reference with examples
  • ./references/caching-patterns.md - Cache-Control, ETag, CDN patterns
  • ./references/rate-limiting.md - Rate limiting strategies and headers
  • ./references/response-formats.md - Errors, versioning, bulk ops, HATEOAS