Introduction
A well-designed API is a joy to use. It's intuitive, consistent, and follows established conventions. Let's explore the best practices for designing REST APIs that developers love.
Resource Naming
Use nouns, not verbs. Resources are things, not actions.
bash
Copy
1# ✅ Good - nouns 2GET /users 3GET /users/123 4POST /users 5PUT /users/123 6DELETE /users/123 7 8# ❌ Bad - verbs 9GET /getUsers10POST /createUser11PUT /updateUser/12312DELETE /deleteUser/123
#
Plural vs Singular
Use plural nouns consistently:
bash
Copy
1GET /users # Collection 2GET /users/123 # Single resource
#
Nested Resources
For related resources:
bash
Copy
1GET /users/123/posts # All posts by user 123 2GET /users/123/posts/456 # Specific post by user 3POST /users/123/posts # Create post for user
HTTP Methods
Use the right method for each action:
| Method | Purpose | Idempotent |
|--------|---------|------------|
| GET | Read resource | Yes |
| POST | Create resource | No |
| PUT | Replace resource | Yes |
| PATCH | Partial update | Yes |
| DELETE | Delete resource | Yes |
HTTP Status Codes
Return appropriate status codes:
javascript
Copy
1// Success 2200 OK // General success 3201 Created // Resource created 4204 No Content // Successful delete 5 6// Client Errors 7400 Bad Request // Invalid input 8401 Unauthorized // No/invalid auth 9403 Forbidden // Not allowed10404 Not Found // Resource not found11422 Unprocessable // Validation failed1213// Server Errors14500 Internal Error // Generic server error15503 Service Unavailable
Error Handling
Return consistent error responses:
json
Copy
1{ 2 "error": { 3 "code": "VALIDATION_ERROR", 4 "message": "The request body contains invalid data", 5 "details": [ 6 { 7 "field": "email", 8 "message": "Must be a valid email address" 9 },10 {11 "field": "password",12 "message": "Must be at least 8 characters"13 }14 ]15 }16}
Pagination
For list endpoints, always paginate:
bash
Copy
1GET /users?page=2&limit=20
Response:
json
Copy
1{ 2 "data": [...], 3 "pagination": { 4 "page": 2, 5 "limit": 20, 6 "total": 156, 7 "totalPages": 8 8 } 9}
Filtering and Sorting
Support query parameters:
bash
Copy
1# Filtering 2GET /users?status=active&role=admin 3 4# Sorting 5GET /users?sort=createdAt&order=desc 6 7# Searching 8GET /users?search=john
API Versioning
Version your API to avoid breaking changes:
bash
Copy
1# URL versioning (recommended) 2GET /v1/users 3GET /v2/users 4 5# Header versioning 6GET /users 7Accept: application/vnd.myapi.v1+json
Authentication
Use appropriate auth methods:
bash
Copy
1# Bearer token (JWT) 2Authorization: Bearer eyJhbGciOiJIUzI1NiIs... 3 4# API Key (for service-to-service) 5X-API-Key: your-api-key
Rate Limiting
Protect your API with rate limits:
bash
Copy
1HTTP/1.1 429 Too Many Requests 2X-RateLimit-Limit: 100 3X-RateLimit-Remaining: 0 4X-RateLimit-Reset: 1609459200
Documentation
Document your API with OpenAPI/Swagger:
yaml
Copy
1openapi: 3.0.0 2info: 3 title: User API 4 version: 1.0.0 5paths: 6 /users: 7 get: 8 summary: List all users 9 parameters:10 - name: page11 in: query12 type: integer13 responses:14 200:15 description: List of users
Conclusion
Well-designed APIs are predictable, consistent, and self-documenting. Follow these conventions and your API consumers will thank you.