Rest – What are the standards for having nested resources in REST API

rest

I am designing a REST API and figured I'll just look at how others are naming their resources and choosing the routes.

I look at Twitter's API and see that they have nested resources. For example:

https://dev.twitter.com/rest/reference/get/statuses/retweets_of_me

The resource is called retweets_of_me but it's also nested under statuses.

Does this mean that there is a logical association between the two resources? I can pick whatever routes I want to use but arbitrarily nesting routes probably isn't good practice.

Best Answer

Using random routes...bad design.

In regards to nesting, lets take a use case where you have a set of users and a set of books, and a certain user can have multiple books associated

It would then make sense to have:

  • /users - get users, you can use query params for pagination, sorting and filtering
  • /books - get books
  • /users/{user_id}/books - get the books associated to a certain user, thus having a natural nesting, reflected by your data model as well

This was just a simple example, hoped in helped, for a better understanding I would suggest googleing for something like rest api desing best practices, go over multiple sources and try to adapt something that feels ok to you and also makes sense from the app requirements point of view

Related Topic