Search code examples
restrestful-architectureapi-designrestful-url

how to choose between query string params and resource name url building a new endpoint?


I have 3 models/resources:

  • Model A
  • Model B
  • Model C that belongs to Model A and Model B

Then, I can build my API routes like this:

/api/a-resources/x/c-resources

/api/b-resources/x/c-resources

Or maybe I can do:

/api/c-resources?a_resource_id=x

/api/c-resources?b_resource_id=x

to get similar behaviour...

The question is:

what do I need to ask myself to choose between these options?


Solution

  • Perhaps think about how is your API going to be consumed, what's the workflow for the consumer?

    • if it's first going to navigate to resource A/B, and only then he might request C, the first option seems a better fit;
    • however, if there's also a scenario where he might directly need resource C, then you can go with the latter option.

    You can obviously go with either of those, there's no wrong one, things are rarely black and white in REST.