This style guide defines conventions for designing and documenting APIs using a documentation-driven approach (e.g., OpenAPI 3.1 / Swagger). All API behaviors—errors, pagination, timeouts, and idempotency—must be explicitly described in the OpenAPI specification, including schemas, examples, and x- extensions for custom metadata. This ensures self-documenting APIs with consistent client experiences.
Tools like Spectral or Prism should validate adherence during CI/CD. Use semantic versioning for the OpenAPI spec (e.g., /openapi/v1.0.yaml).
1. Errors
Errors MUST use standard HTTP status codes and a consistent JSON response body. Never return HTML or plain text errors. All error responses are documented in OpenAPI with oneOf schemas referencing a shared Error schema.
HTTP Status Codes
Category
Code
Usage
Success
200 OK
Standard GET response.
201 Created
Resource created (POST/PUT). Include Location header with URI.
204 No Content
Successful DELETE or idempotent update.
Client Errors
400 Bad Request
Invalid input (e.g., malformed JSON).
401 Unauthorized
Missing/invalid auth.
403 Forbidden
Authenticated but insufficient permissions.
404 Not Found
Resource does not exist.
409 Conflict
Resource state conflict (e.g., duplicate key).
422 Unprocessable Entity
Valid syntax but semantic validation failed (e.g., invalid email).
Server Errors
5xx
Use specific codes (e.g., 500 Internal Server Error, 503 Service Unavailable). Retryable.
Error Response Format
All error responses use this JSON schema (define in OpenAPI components):
Use code for programmatic handling (e.g., RATE_LIMIT_EXCEEDED).
Log all errors server-side with request_id.
Rate-limit errors: 429 with Retry-After header.
2. Pagination
Use cursor-based pagination (opaque cursors) for all list endpoints to avoid offset skips/duplicates in dynamic datasets. Document pagination params and response fields in OpenAPI paths.