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.
-
This is probably a good place to start: restapitutorial.com/lessons/restfulresourcenaming.htmlRobert Harvey– Robert Harvey2014年09月17日 16:19:17 +00:00Commented Sep 17, 2014 at 16:19
-
for JSON there is the following spec: jsonapi.org there is also an API design guide from Google: cloud.google.com/apis/design and from Microsoft: github.com/Microsoft/api-guidelines/blob/master/Guidelines.mdcloud_traveler– cloud_traveler2018年03月15日 22:17:08 +00:00Commented Mar 15, 2018 at 22:17
1 Answer 1
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