Step 12 of 21
RESTful conventions, versioning, pagination, error responses, when REST isn't enough
RESTful conventions, versioning, pagination, error responses — ออกแบบ API ที่คนอื่นใช้งานได้
REST (Representational State Transfer) is an architectural style for designing networked APIs. You interact with resources using standard HTTP methods.
A consistent REST API is predictable. Developers can guess the endpoints without reading docs. This reduces onboarding time and bugs.
GET /users → List users
GET /users/42 → Get user 42
POST /users → Create a user
PUT /users/42 → Replace user 42
PATCH /users/42 → Update part of user 42
DELETE /users/42 → Delete user 42
GET /users/42/orders → List orders for user 42
Rules:
/users not /user/users/42/orders/getUser or /deleteOrder/users?role=admin&sort=-created| Method | Idempotent | Safe | Use For |
|---|---|---|---|
| GET | Yes | Yes | Read |
| POST | No | No | Create |
| PUT | Yes | No | Full replacement |
| PATCH | No | No | Partial update |
| DELETE | Yes | No | Remove |
Idempotent means calling it once or ten times produces the same result.
200 OK — Success (GET, PUT, PATCH, DELETE)
201 Created — Resource created (POST)
204 No Content — Success, no body (DELETE)
400 Bad Request — Client sent invalid data
401 Unauthorized — Not authenticated
403 Forbidden — Authenticated but not allowed
404 Not Found — Resource doesn't exist
409 Conflict — Duplicate or state conflict
422 Unprocessable — Valid format, business rule violation
429 Too Many Requests — Rate limited
500 Internal Server Error — Something broke on your end
502 Bad Gateway — Upstream service failed
503 Service Unavailable — You're down or overloaded
REST works well for CRUD-heavy APIs. It struggles when: