angro
angro

Reputation: 160

REST API URI for resource with multiple primary keys

I am developing a generic REST API for my projects and I'm wondering what to do when I have a table/resource with 2 or more primary keys.

For example, lets suppose I have a table named "question" with two primary keys (date and type) and I need to create the resource REST URI. What is the best way to do it following the standard schema api/{resource}/{id}?

Maybe something like: api/question/{:date},{:type}? What is the best way to do it?

Thank you.

Upvotes: 14

Views: 12431

Answers (4)

Nkosi
Nkosi

Reputation: 247611

This is a good example of when to use a slug. This answer to What is a slug provides a good idea of how you can use your composite primary key in your api design.

With that, you have a few options at your disposal. Which is the best would be a matter of opinion and what suits your needs.

  • api/question/{:date}/{:type} or api/question/{:key1}/{:key2}/.../{:keyn}

The same pattern could also be applied to the following.

  • api/question/{:date}_{:type}

  • api/question/{:date}-{:type}

Upvotes: 2

Dherik
Dherik

Reputation: 19120

I think that what you call multiple primary keys is a composite key. Right?

You have some options.

Use api/questions/dates/:date/types/:type

Maybe, the best alternative for you is:

api/questions/dates/{:date}/types/{:type}

This is more natural to read as a http resource for your case, even if don't make sense have a api/question/dates/{:date} in your application.

Use api/questions/:date/:type/

Another alternative is:

api/questions/:date/:type/

Use query parameter

If it's no a problem for you, instead of return a single object question you can return an array of questions as response using a filter query, like:

api/questions?date=2022-10-27&type=XYZ

Both are not mandatory, but if the user send both, the return will be always an array with a single element. Also this bring some flexibility to your API, because the user can inform just one of them and have some results. You need to check if this behavior it's valid for your case.

Upvotes: 4

Lokesh Gupta
Lokesh Gupta

Reputation: 472

I do not find it a good idea of having two primary keys for a resource. REST heavily depends on resources and it's representations.

If you are struck into situation where you are ending up with two identifiers for a resource - then redesign your application (may be by creating another key in backend after mapping it to other identifiers) and add these multiple keys as attributes in resource.

Idea is - "keep it simple" if you want to create truly world class REST APIs.

Bonus: You don't need to teach few extra things to clients/developers about something fancy you did with your APIs.

Upvotes: 1

Alex Marculescu
Alex Marculescu

Reputation: 5770

You're on the right path, I think you definitely should include both the date and the type in the resource url if that's the only way you can uniquely identify it

api/question/{date}_{type}

Upvotes: 3

Related Questions