API and API Design

Backend Engineering

API and API Design

API Specifications →  HATEOAS

API Specifications →  HATEOAS

What is HATEOAS?

HATEOAS is a constraint of REST (Representational State Transfer) that distinguishes it from other network application architectures. According to REST, a client interacts with a network application entirely through hypermedia provided by application servers.

HATEOAS enables a client to dynamically navigate to related resources through hyperlinks embedded in the responses. This means that a client does not need to hard-code the URL paths but instead follows links that the server provides.

Key Characteristics of HATEOAS

  1. Self-descriptive Messages: Every response includes links that provide guidance on how to move to the next state, thereby giving the client enough information to know what actions are possible next.

  2. Dynamic Navigation: Clients can dynamically navigate through resources by following links in the responses.

  3. Decoupled Client and Server: Clients do not need prior knowledge of the structure of the service URLs. The server controls the interactions by providing URLs. Hence, it reduces the tight coupling between client and server, making the API more flexible.

Practical Example

Let's create an example API for managing a library system with books, authors, and genres.

Step 1: Conceptualize Resources and Endpoints

GET /books - Retrieve all books
GET /books/{id} - Retrieve a specific book by ID

Step 2: Apply HATEOAS to Responses

Response without HATEOAS:

{
  "id": 1,
  "title": "Introduction to HATEOAS",
  "author": "John Doe",
  "genre": "Technology"
}

Response with HATEOAS:

{
  "id": 1,
  "title": "Introduction to HATEOAS",
  "author": {
    "name": "John Doe",
    "link": "/authors/1"
  },
  "genre": {
    "name": "Technology",
    "link": "/genres/technology"
  },
  "_links": {
    "self": { "href": "/books/1" },
    "collection": { "href": "/books" },
    "author": { "href": "/authors/1" },
    "genre": { "href": "/genres/technology" }
  }
}

Explanation:

  • Each resource now includes _links, which provide hyperlinks to related resources:

    • "self": Refers to the current resource.

    • "collection": Enlists the endpoint to retrieve all books.

    • "author": Provides a link to the author's resource.

    • "genre": Provides a link to the genre's resource.

Step 3: Implementing the Server Side in Node.js with Express

  1. Initialize Project

    npm init -y
    npm install express body-parser
    
  2. Basic Server Setup


const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

app.use(bodyParser.json());

const books = [
  { id: 1, title: 'Introduction to HATEOAS', author: 1, genre: 'technology' }
];
const authors = [
  { id: 1, name: 'John Doe' }
];
const genres = {
  technology: { name: 'Technology' }
};

app.get('/books/:id', (req, res) => {
  const bookId = parseInt(req.params.id);
  const book = books.find(b => b.id === bookId);
  
  if (book) {
    const author = authors.find(a => a.id === book.author);
    const genre = genres[book.genre];
    
    res.json({
      id: book.id,
      title: book.title,
      author: {
        name: author.name,
        link: `/authors/${author.id}`
      },
      genre: {
        name: genre.name,
        link: `/genres/${book.genre}`
      },
      _links: {
        self: { href: `/books/${book.id}` },
        collection: { href: '/books' },
        author: { href: `/authors/${author.id}` },
        genre: { href: `/genres/${book.genre}` }
      }
    });
  } else {
    res.status(404).send('Book not found');
  }
});

app.listen(port, () => {
  console.log(`Server running at <http://localhost>:${port}`);
});

Conclusion

HATEOAS adds a significant layer of flexibility and self-documentation to RESTful APIs by embedding hyperlinks within responses. This approach helps create a more dynamic and discoverable system:

  1. Decoupled Architecture: Clients do not need to predefine URL structures, allowing backend modifications without affecting the client application.

  2. Self-Documenting: Hypermedia is built-in documentation that guides the client through available actions and states.

By adopting HATEOAS, you can build more robust and user-friendly APIs that simplify navigation and enhance interoperability.

HATEOAS is an advanced concept within RESTful APIs that promotes dynamic, link-driven interactions between a client and server.

By embedding hyperlinks in resource representations, it allows for simplified client design, increases flexibility, and enables better discoverability of related resources. This enhances the user experience and future-proofs the API against structural changes.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board