Before you can build an API, you must understand the rules of the road. REST is the architectural framework that keeps the web organized.
1Understanding REST
REST (Representational State Transfer) is the architectural style that powers the majority of APIs on the web. It is not a rigid protocol or a piece of software; it is a set of design constraints. When an API adheres to these constraints, we call it a 'RESTful' API. The core philosophy of REST is treating everything on the server as a 'Resource' (like a User, a Post, or a Comment) that can be accessed via standard URLs. By standardizing this access, developers don't have to guess how to fetch data.
const resource = "A User";
const url = "https://api.example.com/users";
2Statelessness
The most critical constraint of REST is 'Statelessness'. This means the Server does NOT remember anything about the Client between requests. Every single request sent to the Server must contain all the information necessary to understand and process it (like an API Key or Authentication Token). Because the server doesn't have to manage 'sessions' or remember who you are, it becomes incredibly easy to scale the architecture to millions of users. If a server dies, another can instantly process the next request because it carries all context within itself.
// Stateless (RESTful):
// "Delete account for User 42. Here is my secure Token."
3URLs vs URIs
In REST, every resource is identified by a URI (Uniform Resource Identifier). The most common type of URI is a URL (Uniform Resource Locator). Think of a URI as a noun. Instead of creating a messy endpoint like /api/deleteUser?id=5, REST enforces clean, hierarchical nouns. The correct RESTful URL would be /api/users/5. The action (delete) is handled by the HTTP method, not the URL. Do not pollute your URLs with verbs.
/getUsers
/createNewPost
// ✅ RESTful URLs (Nouns only):
/users
/posts
4Hierarchical Relationships
REST URLs shine when representing relationships between resources. If a specific User (ID: 5) writes a specific Post (ID: 10), and you want to view the Comments on that post, the URL structure logically nests these nouns. The path /api/users/5/posts/10/comments perfectly maps the database hierarchy into an intuitive URL that any developer can instantly understand. It scales naturally with the complexity of your data model.
GET /users/5/posts/10/comments
