resthateoashypermedia

REST - Should an API client "advance" to the "next" resource like a browser?


In my years specifying and designing REST APIs, I'm increasingly finding that its very similar to designing a website where the user's journey and the actions and links are story-boarded and critical to the UX.

With my API designs currently, I return links in items and at the bottom of resources. They perform actions, mutate state or bring back other resources.

But its as if each link opens in a new tab; the client explores down a new route and their next options may narrow as they go.

If this were a website, it wouldn't necessarily be a good design. The user would have to either open links in new tabs or back-up the stack all the time to get things done.

Good sites are forward only, or indeed have a way to indicate a branch off the main flow, i.e. links automatically opening in new windows (via anchor tag target).

So should a good REST API be designed as if the client discards the current resource and advances to the next and is always advancing forward?

Or do we assume the client is building a map as it goes, like um a Roomba exploring our living room?

The thing with the map concept is that the knowledge that one should return to a previous resource, of the many it might know about, is in a sentient human, a guess. Computers are incapable of guessing and so its need programming, and this implies out-of-band static documentation and breaks REST.


Solution

  • In my years specifying and designing REST APIs, I'm increasingly finding that its very similar to designing a website

    Yes - a good REST API looks a lot like a machine readable web site.

    So should a good REST API be designed as if the client discards the current resource and advances to the next and is always advancing forward?

    Sort of - the client is permitted to cache representations; so if you present a link, the client may "follow" the link to the cached representation rather than using the server.

    That also means that the client may, at its discretion, "hit the back button" to go off and do something else (for example, if the link that it was hoping to find isn't present, it might try to achieve its goal another way). This is part of the motivation for the "stateless" constraint; the server doesn't have to pretend to know the client's currently displayed page to interpret a message.

    Computers are incapable of guessing and so its need programming, and this implies out-of-band static documentation and breaks REST.

    Fielding, writing in 2008

    Of course the client has prior knowledge. Every protocol, every media type definition, every URI scheme, and every link relationship type constitutes prior knowledge that the client must know (or learn) in order to make use of that knowledge. REST doesn’t eliminate the need for a clue. What REST does is concentrate that need for prior knowledge into readily standardizable forms. That is the essential distinction between data-oriented and control-oriented integration.