2

Basing on the naming conventions found here : https://restfulapi.net/resource-naming/ , I have a particular question to which I can not find an answer.

Taking the example of customers and accounts where sub-collection resource “accounts” of a particular “customer” can be identified using the URN “/customers/{customerId}/accounts” , how do I find accounts for multiple customer IDs? What are the naming conventions for such a case?

Is the only option to use filters? eg: customers/accounts?customerId=12,22

1 Answers1

0

I tend to avoid filters and keep everything as a urn and keep the implementation of the backend system hidden. e.g. this

customers/accounts?customerId=12,22

means the client needs to know that customers are represented in the system by a variable called customerId. Clients shouldn't need to know that. They just need to know that customers have numbers, IMHO anyway.

This answer shows a solution for your situation, which would look like:

customers/accounts/12,22

although to keep it in line with the domain, where customers have ids and associated accounts, it would look like:

customers/12,22/accounts

and your backend framework would give you the list of customer 'numbers' from the url and at that point they become customerIds.

Not all frameworks may support arrays in paths but pick the right tool for the job and you can design your API to be elegant and a good match for your domain.

codebrane
  • 4,290
  • 2
  • 18
  • 27
  • I like the 1st example (query string) best. Especially the 2nd is bad since it's not clear that those are customer IDs. Could be account IDs, which will bite you, if they are ever introduced. – DanMan Aug 09 '19 at 14:07
  • yes, that's why I prefer the third one as it's clear they're related to customers – codebrane Aug 09 '19 at 14:35
  • thank you for your inputs! I think we'll stick to the query string, not used to the arrays in the middle of a uri.. – Shalkie Yadav Aug 13 '19 at 09:31