This is a dilemma I find myself facing very often when dealing with nested resources.

So suppose the target user has n photos. Does it make sense to define a GET /users/{id}/photos route or should I just send n GET /photos/{id} requests by first requesting the User object and then looping through that User's photo_ids attribute?

1

There are 1 best solutions below

0
On

From a best practices perspective, I would not send several request for such similar resources. In that scenario, you end up creating more work for yourself by having to rate limit on the backend, in order to keep your users with a lot of pictures from blowing up your server. That would be impactful to your UX as well.

I recommend you format your route as such:

GET /users/photos?id={{id}}

And return all associated photos with that user ID all at once. You can always limit that to X number of photos per call too, and paginate:

GET /users/photos?id=658&page={{1,2,3, etc.}}

My personal preference is always to try to keep variable data in the URL parameters. Having spent the afternoon with several unrelated APIs, I can tell you a whole slew of developers agree.