RESTful API Design Best Practices Overview
1. Noun-Based Endpoint Architecture
Endpoints should represent resource entities (nouns) rather than actions (verbs).
- Recommended: GET /users/123
- Discouraged: GET /getUserWithId?id=123
2. Consistent HTTP Method Conventions
- GET: Retrieve resource data without side effects.
- POST: Create a new resource in the target collection.
- PUT: Replace an existing resource entirely or create it if missing.
- PATCH: Apply partial modifications to a resource.
- DELETE: Remove a resource.
3. Meaningful HTTP Status Codes
Always return standard status codes to reflect execution results:
- 200 OK: Successful operation.
- 201 Created: Resource successfully created.
- 400 Bad Request: Invalid client payload or parameters.
- 401 Unauthorized: Authentication required or invalid.
- 404 Not Found: Resource path does not exist.
- 500 Internal Server Error: Unhandled server exception.
4. Pagination and Filtering
For large data collections, use query parameters for pagination:
GET /items?page=2&limit=20&sort=desc