Identify item by either an ID or a slug in a RESTful API

Of the three I prefer the third option, it’s not uncommon to see that syntax; e.g. parts of Twitter’s API allow that syntax:
https://dev.twitter.com/rest/reference/get/statuses/show/id

A fourth option is a hybrid approach, where you pick one (say, ID) as the typical access method for single items, but also allow queries based on the slug. E.g.:

GET /items/<id>
GET /items?slug=<slug>
GET /items?id=<id>

Your routing will obvious map /items/id to /items?id=

Extensible to multiple ids/slugs, but still meets the REST paradigm of matching URIs to the underlying data model.

Leave a Comment