DEV Community

Cover image for WTF is HATEOAS?
Russell Carey
Russell Carey

Posted on • Updated on

WTF is HATEOAS?

Recently I have been diving into the world of Laravel. I took it upon myself to speed run this great (a little outdated) Udemy course on creating REST APIs Link and come across something new and interesting. HATEOAS.

Wikipedia defines it as (get ready); Hypermedia as the Engine of Application State is a constraint of the REST application architecture that distinguishes it from other network application architectures. With HATEOAS, a client interacts with a network application whose application servers provide information dynamically through hypermedia.

WHAT?!

So my friends let me break it down for you.

Basically, it means that hypertext should be used to find your way through the API.

Let me give you an example. Below is a Laravel transformer I created (one way to change the attribute names of our data from whats in the DB to the client) with the links for that resource included.

Code for HATOAS

At the top we have the transformer transforming the attribute names and below this we have the links for our routes.

Rel is the name of the link and the href is the route of the resource. My code is just getting the route and inputting the ID into the route URL of this resource.

Now, what does this look like in the response?

API Response

As you can see, each of our items has a new section called links and this contains links to the other resources that are linked to this one.

This enables easier navigation through our API's, kind of like a website!

I hope this little tip improves you API's!

Footer:
I am currently living in Japan and looking for work (local or remote) so if you have anything, send it my way!

Check out my portfolio here link.
Check out my twitter here link.
Check out my GitHub here link.

Top comments (1)

Collapse
 
evert profile image
Evert Pot

I can strongly recommend using a standard format like HAL instead of your own Link format, so you can use existing clients like mine!

github.com/badgateway/ketting

But also not just mine. Standard formats means less work for consumers of your API.