REST API Best Practices
1. Use Nouns for Resources
URLs identify resources, not actions. Use plural nouns.
โ
GET /users /products/42
โ GET /getUser /fetchProduct
โ GET /getUser /fetchProduct
2. HTTP Methods Semantics
Use the right verb for each operation.
GET /users โ list all users
POST /users โ create a user
GET /users/1 โ get user #1
PUT /users/1 โ replace user #1
PATCH /users/1 โ partial update
DELETE /users/1 โ delete user #1
POST /users โ create a user
GET /users/1 โ get user #1
PUT /users/1 โ replace user #1
PATCH /users/1 โ partial update
DELETE /users/1 โ delete user #1
3. Consistent HTTP Status Codes
200 OK โ success (GET, PUT, PATCH)
201 Created โ resource created (POST)
204 No Content โ success, no body (DELETE)
400 Bad Request โ invalid input
401 Unauthorized โ missing/invalid auth
403 Forbidden โ no permission
404 Not Found โ resource doesn't exist
422 Unprocessable โ validation failed
500 Server Error โ internal error
201 Created โ resource created (POST)
204 No Content โ success, no body (DELETE)
400 Bad Request โ invalid input
401 Unauthorized โ missing/invalid auth
403 Forbidden โ no permission
404 Not Found โ resource doesn't exist
422 Unprocessable โ validation failed
500 Server Error โ internal error
4. Versioning
Version your API to avoid breaking changes.
โ
/api/v1/users
โ Accept: application/vnd.myapi.v2+json
โ Accept: application/vnd.myapi.v2+json
5. Pagination, Filtering, Sorting
GET /users?page=2&limit=20
GET /products?sort=price&order=asc
GET /orders?status=pending&userId=42
GET /products?sort=price&order=asc
GET /orders?status=pending&userId=42
6. Use JSON for Request/Response Bodies
Always set Content-Type: application/json. Return consistent error objects with code, message, and details fields.