API Design Protocol
When designing APIs:
REST API Design
Resource Naming
- Use nouns, not verbs:
/users, not/getUsers - Use plural for collections:
/articles,/comments - Nest resources logically:
/users/{id}/posts
- Use nouns, not verbs:
HTTP Methods
GET: Retrieve resources (idempotent, cacheable)POST: Create new resourcesPUT: Replace entire resourcePATCH: Partial updateDELETE: Remove resource
Status Codes
200 OK: Successful GET, PUT, PATCH201 Created: Successful POST204 No Content: Successful DELETE400 Bad Request: Invalid input401 Unauthorized: Missing/invalid auth403 Forbidden: Authenticated but not authorized404 Not Found: Resource doesn't exist500 Internal Server Error: Server failure
Response Format
- Consistent JSON structure
- Include metadata (pagination, timestamps)
- Use camelCase or snake_case consistently
- Provide meaningful error messages
Versioning
- URL versioning:
/v1/users,/v2/users - Header versioning:
Accept: application/vnd.api.v2+json - Maintain backward compatibility when possible
- URL versioning:
GraphQL Design
Schema Design
- Define clear types and relationships
- Use interfaces for polymorphic types
- Implement pagination (cursor-based preferred)
Query Optimization
- Implement DataLoader to prevent N+1 queries
- Use query complexity analysis
- Set depth and rate limits
Error Handling
- Return partial data when possible
- Use extensions for error metadata
- Distinguish user errors from system errors
Best Practices
- Pagination: Use cursor-based for large datasets
- Filtering & Sorting: Support query parameters
- Authentication: Use OAuth2 or JWT
- Rate Limiting: Protect against abuse
- Documentation: Use OpenAPI (Swagger) or GraphQL introspection
- Validation: Validate input at API boundary
- CORS: Configure properly for web clients