Learn proven REST API design patterns for building scalable, maintainable JSON APIs. From status codes to error handling, master API design.
A well-designed REST API is intuitive, consistent, and developer-friendly. Whether you are building internal microservices or public APIs, following established patterns ensures your API scales with your business needs.
Status codes communicate result without parsing the body:
Follow resource-oriented naming:
Good:
Bad:
Always version from day one:
URL Path (Recommended):
Header (Alternative):
Use a consistent envelope structure:
Success Response: { "data": { ... }, "meta": { "timestamp": "2026-05-01T12:00:00Z", "requestId": "uuid" } }
Error Response: { "error": { "code": "VALIDATION_ERROR", "message": "Email is required", "details": [{ "field": "email", "issue": "missing" }] } }
Never return unbounded lists:
Offset Pagination: GET /users?limit=20&offset=40 { "data": [...], "pagination": { "total": 1000, "limit": 20, "offset": 40, "hasMore": true } }
Cursor Pagination (Better for large datasets): GET /users?limit=20&cursor=eyJpZCI6MTIzfQ
Provide actionable error messages:
Bad: { "error": "Something went wrong" }
Good: { "error": { "type": "ValidationError", "code": "INVALID_EMAIL", "message": "The email address 'invalid' is not valid.", "field": "email", "suggestion": "Please provide a valid email like [email protected]" } }
Allow flexible data retrieval:
GET /users?status=active&sort=-createdAt&fields=id,name,email
Protect your API from abuse:
Headers: X-RateLimit-Limit: 1000 X-RateLimit-Remaining: 999 X-RateLimit-Reset: 1640995200
Response when limited (429): { "error": { "code": "RATE_LIMIT_EXCEEDED", "message": "Too many requests. Please try again in 60 seconds." } }
Enable cross-origin requests securely:
Headers: Access-Control-Allow-Origin: https://yourdomain.com Access-Control-Allow-Methods: GET, POST, PUT, DELETE Access-Control-Allow-Headers: Content-Type, Authorization Access-Control-Max-Age: 86400
Use OpenAPI (Swagger) for interactive documentation:
Great APIs are designed with the developer experience in mind. Follow these REST API best practices to build APIs that are intuitive, scalable, and maintainable. Test your API responses with our JSON formatter to ensure consistent, valid JSON output.