Back to Home

Nested API Endpoint and it's issue.

In a recent sharing session with our CTO Brian de Heus, about REST API. One of the thing that i remember is about nested API Endpoint. He pointing that Nested endpoint is bad. However due to the time, we unfortunately getting less context about it so i'd love to give more explanation and context why it's bad.

What is Nested API Endpoint

A nested API endpoint refers to an API structure where one resource is contained within another resource. This allows for more specific queries that reflect the hierarchical relationships between different resources in a system.

# Simple endpoint
/api/v1/users/123
# Nested endpoints
/api/v1/users/123/tasks/456
/api/v1/teams/789/users/123/tasks/456

Perceived Benefits

At first glance, nested endpoints appear to offer several advantages:

1. Clear Resource Hierarchy

2. Intuitive Navigation

3. Apparent Simplicity

However, these benefits often turn out to be superficial when compared to the problems they introduce.

The Major Challenges

1. Path Ambiguity and Redundancy

One of the most significant issues is the creation of multiple valid paths to the same resource. Consider these endpoints:

/api/v1/tasks/456
/api/v1/users/123/tasks/456
/api/v1/teams/789/users/123/tasks/456

This redundancy raises several critical questions:

2. Inconsistent Depth Problems

Let say we have this nested structure :

/api/v1/companies/1/departments/2/employees/3/tasks/4/comments/5

This structure raises several architectural questions:

/api/v1/comments/5
/api/v1/tasks/4/comments/5
/api/v1/employees/3/comments/5

3. Permission and Authorization Complexity

Nested endpoints often create confusion around access control:

/api/v1/users/123/posts/456        # Requires user ownership?
/api/v1/posts/456                  # Just needs authentication?
/api/v1/teams/789/posts/456        # Demands team membership?

This leads to several security considerations:

4. Resource Relationship Confusion

Resources often belong to multiple parent entities, making the "correct" path unclear:

/api/v1/users/123/posts/456        # Post by user perspective
/api/v1/categories/789/posts/456   # Post by category perspective
/api/v1/teams/567/posts/456        # Post by team perspective

This creates several design challenges:

Better Approach

1. Flat Resources with Query Parameters

/api/v1/tasks?user_id=123&team_id=789
/api/v1/tasks/456

2. Resource Expansion Parameters

/api/v1/tasks/456?expand=user,team,comments

3. Separate Relationship Endpoints

/api/v1/users/123/task-assignments

Benefits of Flat Design

Conclusion

While nested API endpoints might seem intuitive initially, they often create more problems than they solve. The apparent benefits of hierarchical representation are outweighed by issues with ambiguity, permissions, and maintenance. When designing your API:

Remember, the goal of API design is to create interfaces that are not just easy to understand, but also easy to maintain, secure, and evolve over time.