--- name: api-design description: Design REST, GraphQL, and gRPC APIs — endpoint naming, versioning strategies, error shapes, pagination patterns, authentication, service contracts. license: MIT --- ## What I do Guide design of clean, consistent, maintainable APIs. ## Core Principles - **Consistency**: Nouns for resources, verbs for actions, consistent error format - **Simplicity**: Expose resources, not tables; keep URLs hierarchical - **Discoverability**: Self-documenting URLs, standard HTTP methods ## REST Guidelines ### URL Structure ``` /resources # Collection /resources/:id # Specific resource /resources/:id/sub # Nested resource ``` ### HTTP Methods - GET: Retrieve - POST: Create - PUT: Replace - PATCH: Partial update - DELETE: Remove ### Status Codes - 200: Success - 201: Created - 204: No content - 400: Bad request - 401: Unauthorized - 403: Forbidden - 404: Not found - 409: Conflict - 422: Validation error - 429: Rate limited - 500: Server error ### Error Format ```json { "error": { "code": "VALIDATION_ERROR", "message": "Human readable", "details": [{ "field": "email", "message": "Invalid format" }] } } ``` ## Pagination - **Offset**: `GET /items?limit=20&offset=40` - **Cursor**: `GET /items?limit=20&cursor=eyJpZCI6MTAwfQ` Prefer cursor-based for large datasets. ## Versioning ### URL Versioning (Recommended) ``` /api/v1/resources /api/v2/resources ``` ### Breaking Changes (MAJOR bump) - Remove/rename fields - Change field types - Remove endpoints ### Deprecation ``` Deprecation: true Sunset: Sat, 01 Jan 2025 00:00:00 GMT Link: ; rel="successor-version" ``` ## Authentication - Bearer tokens - Refresh tokens - Rate limit per user ## Rate Limiting ``` X-RateLimit-Limit: 100 X-RateLimit-Remaining: 95 X-RateLimit-Reset: 1609459200 ``` ## Immediate Mode APIs APIs should be immediate mode—clients describe desired state, API handles how. ```rust // Bad: Client manages state machine enum UserOperation { Create { name: String }, Update { id: String, name: String }, } // Good: Client describes desired state struct User { id: Option, // None = create, Some = update name: String, } ``` Why: - Fewer client bugs - Better iteration - Simpler mental model --- Use this skill when designing APIs.