
Ashen Samarakoon
27 March 2024
|15 min read
Representational State Transfer (REST) APIs are a way for different computer systems to talk to each other over the internet. They follow a set of rules called ‘REST’ to make communication easier and more organized. These APIs allow applications to request and exchange data with each other, making it possible for various services and websites to work together smoothly. It's like a standardized language that helps computers understand each other and share information effectively.
In the context of RESTful APIs, a resource is any piece of information or entity that can be accessed or manipulated through the API. Resources can represent real-world objects, such as users, posts, products, or orders, as well as abstract concepts like sessions, permissions, or configurations. In a RESTful architecture, each resource is uniquely identifiable by a Uniform Resource Identifier (URI).
In RESTful APIs, the term ‘hierarchical resource structures’ refers to the structuring of resources in a nested or tree-like structure where the resources are connected to one another. A resource is associated with each level of the hierarchy. The connections between resources are shown using URLs and HTTP methods, such as GET, POST, PUT, and DELETE. This design methodology helps develop rational, predictable, and discoverable API designs by adhering to the Representational State Transfer (REST) principles.
To ensure effective API performance and maintenance, it is essential to establish a balance and stay away from overly complex hierarchies.
Consider an application that allows users to create posts and interact with each other through comments. Your task is to design the RESTful API to handle posts and comments efficiently. In this application, users can create posts, read posts, update posts, delete posts, and comment on posts. Each post can have multiple comments associated with it. The goal is to design a hierarchical resource structure that organizes posts and comments effectively, adhering to RESTful principles.
Now, let's explore best practices related to this hierarchical resource structure.
REST APIs should be designed for resources that can be entities, services, etc. Therefore, they must always be nouns. Suppose the client is asking to add resource to the collection, we can use the following request:
Generally, it is recommended to use plurals. But there is no hard rule that one cannot use singular for a REST API resource name. The ideology behind using plurals is that we are operating on one resource from a collection of resources. To depict a collection, we use plurals. Suppose the client is asking to retrieve a resource with the resource ID 123 from a collection of resources, the HTTP request should look like something below: GET /comments/123 To add one resource to the current collection of resources, we may use the following HTTP request: POST /comments This makes your API more consistent, intuitive, and easy to understand.
Hierarchical URLs in RESTful APIs provide a consistent and structured way to organize resources. Just like folders in a computer's file system, hierarchical URLs arrange resources in a logical order, making them easy to find and understand.
Consider the following example. This clearly shows the parent-child relationship between posts and comments.
Root Resource:
Posts Resource:
Single Post Resource:
Comments Resource:
Single Comment Resource:
HTTP verbs are the actions that can be performed on resources in a RESTful API. They define the type of operation being requested by the client. Here's a brief explanation of the commonly used HTTP verbs:
| URL | HTTP Verb | Purpose |
|---|---|---|
| /blog/posts | GET | Retrieves a list of all blog posts. |
| /blog/posts | POST | Creates a new blog post. |
| /blog/posts/123 | GET | Retrieves the details of the blog post with ID 123. |
| /blog/posts/123 | PUT/PATCH | Updates the content of the blog post with ID 123. |
| /blog/posts/123 | DELETE | Deletes the blog post with ID 123. |
| /blog/posts/123/comments | GET | Retrieves a list of all comments for the blog post with ID 123. |
| /blog/posts/123/comments | POST | Adds a new comment to the blog post with ID 123. |
| /blog/posts/123/comments/456 | GET | Retrieves the details of the comment with ID 456 for the blog post. |
| /blog/posts/123/comments/456 | PUT/PATCH | Updates the content of the comment with ID 456 for the blog post. |
| /blog/posts/123/comments/456 | DELETE | Deletes the comment with ID 456 for the blog post. |
Pagination is a technique used in web development to manage large collections of data by splitting them into smaller, more manageable chunks or pages. In the context of RESTful APIs, pagination is particularly important when dealing with resources that may return a large number of results in a single request.
When the client wants to fetch comments from a post, the client can implement
pagination by using query parameters like page and limit.
/posts/1234/comments?page=2&limit=10
This will fetch the second page with 10 comments per page.
Filtering with query parameters in RESTful APIs allows clients to retrieve specific subsets of resources based on certain criteria. This provides flexibility and customization options for clients, allowing them to tailor API responses to their specific needs without requiring additional endpoints or complex query languages.
To filter posts based on a specific category, use a query parameter like
/posts?category=technology to retrieve all posts in the technology category.
Resource embedding involves including related resources directly within the representation of a parent resource in API responses. This approach can help reduce the number of requests required to retrieve all necessary data, as clients receive complete information about related resources in a single response.
Consider a blog API where each post can have multiple comments. When retrieving a specific post , the server embeds the comments directly within the post representation in the API response.
GET /posts/1234
Response:
{
"id": 1234,
"title": "Getting Started with RESTful APIs",
"content": "Lorem ipsum dolor sit amet...",
"comments": [
{
"id": 1,
"text": "Great article!",
"author": "John Doe"
},
{
"id": 2,
"text": "Very informative!",
"author": "Jane Smith
}
]
}
Instead of embedding all comment details within a post response, use unique identifiers to represent relationships. This makes your API more modular, maintainable, and efficient, as you can fetch, update, or delete each resource independently, and avoid sending or receiving unnecessary data.
For example, instead of using /posts/{id} with a nested array of comments, use /posts/{id}/comments with an array of comment ids.
GET /posts/1234
Response:
{
"id": 1234,
"title": "Getting Started with RESTful APIs",
"content": "Lorem ipsum dolor sit amet...",
"comments": [1, 2, 3]
}
}
Designing hierarchical resource structures and managing relationships in RESTful APIs is a critical aspect of creating a robust and user-friendly API. By following the best practices outlined in this blog post and applying the examples provided, you can ensure an efficient API that enhances the developer experience and enables seamless data flow between services and applications.
With these tips and insights in mind, you are well-equipped to design APIs that stand out in the competitive landscape and drive the success of your digital initiatives.
In today's rapidly evolving digital landscape, organizations are constantly striving to accelerate development, enhance their offerings, and maintain a competitive edge. A critical, yet often overlooked, aspect of achieving these goals lies in the ability to truly see and understand digital assets.

Prabath Ariyarathna
13 February 2026
Read More
We're thrilled to unveil our latest Xapi platform update, bringing a more intuitive API design experience, tooling enhancements, and a smarter way to govern externally designed APIs. These new capabilities make API design and governance more accessible, more powerful, and more flexible than ever before.

Shamali Sathindra
8 April 2025
Read More
Every organization wants well-designed APIs, but the key is turning business needs into carefully crafted APIs that perfectly match their main business objectives.

Prabath Ariyarathna
17 February 2025
Read More