RESTful API Design: HTTP Verbs, Resource Naming, Status Codes, and Idempotency
Representational State Transfer (REST) is an architectural style for distributed hypermedia systems. RESTful APIs utilize standard HTTP protocol features to expose resources, manipulate entity state, and enable seamless interoperability across web and mobile clients.
The Six Architectural Constraints of REST
Coined by Roy Fielding in his 2000 doctoral dissertation, REST mandates six architectural constraints: Client-Server separation, Statelessness, Cacheability, Uniform Interface, Layered System, and Code-on-Demand (optional).
Statelessness ensures that every HTTP request contains all necessary credentials and context needed for the server to process it without relying on stored session affinity.
Resource-Oriented URI Design
URIs should represent nouns (resources) rather than verbs (actions). Let HTTP verbs specify the intended action instead of cluttering URIs with verbs like `/getArticle` or `/deleteUser`.
| Method | URI Endpoint | Intended Operation |
|---|---|---|
| GET | /articles | Retrieve a collection of articles |
| POST | /articles | Create a new article resource |
| GET | /articles/101 | Retrieve single article by ID or slug |
| PUT | /articles/101 | Completely replace article 101 |
| PATCH | /articles/101 | Partially update specific fields of article 101 |
| DELETE | /articles/101 | Remove article 101 |
HTTP Verbs and Idempotency Rules
An HTTP method is idempotent if making multiple identical requests has the exact same side effect on the server as making a single request.
`GET`, `HEAD`, `PUT`, and `DELETE` are idempotent. `POST` is non-idempotent because submitting it multiple times creates duplicate records.
{
"status": "success",
"data": {
"id": "art-09",
"title": "REST API Architecture",
"slug": "rest-api",
"viewCount": 1420
},
"links": {
"self": "https://api.devpath.local/v1/articles/rest-api",
"topicHub": "https://api.devpath.local/v1/topics/backend"
}
}Mastering HTTP Status Code Families
Leverage standard status codes so clients and caching proxies instantly understand response states:
• 200 OK: Standard success. • 201 Created: Entity successfully created via POST. • 400 Bad Request: Malformed payload or validation error. • 401 Unauthorized vs 403 Forbidden: Identity missing vs identity recognized but permission denied. • 404 Not Found: Resource does not exist. • 500 Internal Error: Unhandled server exception.
Handling Pagination, Sorting, and Filtering
Large dataset queries should always be paginated to safeguard server memory and speed up response time. Query parameters like `?page=2&limit=20&sort=-createdAt` are the recognized standard.
Architecture Continuity
Combine REST API principles with Express.js middleware to build scalable, documented endpoints.