Learn how to design clean, predictable, and hierarchical URIs for your API resources.
In the context of a REST API, a URI (Uniform Resource Identifier), or more specifically an endpoint, is the address used to identify and access a resource. Well-designed URIs are crucial for a good developer experience; they should be intuitive, predictable, and easy to understand. Several best practices guide URI design. First, URIs should represent resources, not actions. This means using nouns instead of verbs. For example, use `/users/123` to identify a specific user, not `/getUserById?id=123`. The action to be performed on that resource is determined by the HTTP method (e.g., GET to retrieve, DELETE to remove). Second, use plural nouns for collections to maintain consistency. For example, `/users` represents the collection of all users, and `/users/123` represents a specific user within that collection. Third, maintain a clear and consistent hierarchy to represent relationships. For instance, to get all articles written by user 123, a logical URI would be `/users/123/articles`. This structure is easy to read and follows a natural path. Finally, use hyphens (`-`) to improve the readability of long path segments (e.g., `/api/user-profiles`) and stick to lowercase letters to avoid case-sensitivity issues, which can cause confusion and errors. The goal is to create a URI structure that is stable and doesn't change over time, even if the underlying implementation of the API does. A well-designed URI is a fundamental part of creating a clean, professional, and easy-to-use API.