Skip to content

Restful Hateoas Intro

DodaTech 2 min read

title: "RESTful HATEOAS — Hypermedia as the Engine of Application State" description: "RESTful HATEOAS embeds hypermedia links in API responses, enabling clients to discover available actions and navigate resources without prior endpoint knowledge." date: 2026-06-28 lastmod: 2026-06-28 weight: 15 tags: [apis, restful] }

RESTful HATEOAS (Hypermedia as the Engine of Application State) embeds discoverable links in API responses, making the API self-documenting and navigable.

What You'll Learn

  • HATEOAS concept and benefits
  • Link structures in responses
  • Dynamic API navigation

Why It Matters

HATEOAS is what truly makes an API RESTful (per Fielding). Clients navigate by following links rather than hard-coding URLs.

# HATEOAS response with links
@app.route('/users/<int:id>')
def get_user(id):
    user = db.get_user(id)

    response = jsonify({
        "id": user.id,
        "name": user.name,
        "email": user.email,
        "_links": {
            "self": {"href": f"/users/{user.id}", "method": "GET"},
            "orders": {"href": f"/users/{user.id}/orders", "method": "GET"},
            "update": {"href": f"/users/{user.id}", "method": "PUT"},
            "delete": {"href": f"/users/{user.id}", "method": "DELETE"}
        }
    })

    response.headers['Content-Type'] = 'application/hal+json'
    return response

# Collection with links
@app.route('/users')
def list_users():
    users = db.get_users()
    page = request.args.get('page', 1, type=int)

    response = jsonify({
        "_links": {
            "self": {"href": f"/users?page={page}"},
            "next": {"href": f"/users?page={page + 1}"},
            "prev": {"href": f"/users?page={page - 1}" if page > 1 else None}
        },
        "_embedded": {
            "users": [{
                "id": u.id,
                "name": u.name,
                "_links": {"self": {"href": f"/users/{u.id}"}}
            } for u in users]
        }
    })

    response.headers['Content-Type'] = 'application/hal+json'
    return response
// Express HATEOAS implementation
app.get('/api/orders/:id', (req, res) => {
  const order = db.findOrder(req.params.id);

  res.json({
    id: order.id,
    total: order.total,
    status: order.status,
    _links: {
      self: { href: `/api/orders/${order.id}` },
      customer: { href: `/api/users/${order.userId}` },
      items: { href: `/api/orders/${order.id}/items` },
      cancel: {
        href: `/api/orders/${order.id}/cancel`,
        method: 'POST',
        condition: order.status === 'pending'
      },
      pay: {
        href: `/api/orders/${order.id}/pay`,
        method: 'POST',
        condition: order.status === 'pending'
      }
    }
  });
});

Common Mistakes

Without links, clients must hard-code endpoint URLs.

Use a standard format like HAL, JSON-LD, or Siren.

Tell clients which HTTP method to use for each link.

Links may become invalid. Set appropriate cache TTLs.

Links for actions (like cancel) should only appear when actionable.

Practice Questions

  1. What does HATEOAS stand for?
  2. How does HATEOAS help API clients?
  3. What is HAL?
  4. Why include HTTP methods in links?
  5. When should links be conditionally included?

Answers:

  1. Hypermedia as the Engine of Application State.
  2. Clients discover available actions by following links instead of hard-coding URLs.
  3. Hypertext Application Language — a standard format for hypermedia responses.
  4. So clients know how to interact with the linked resource.
  5. When the action is only available in certain states (e.g., cancel only for pending orders).

Challenge: Add HATEOAS links to your API responses. Include self, collection, and action links with HTTP methods.

FAQ

Is HATEOAS required for REST APIs?

: Fielding argues yes. In practice, most REST APIs omit it for simplicity.

What hypermedia format is most popular?

: HAL (Hypertext Application Language) is the most widely used.

Does HATEOAS work with mobile apps?

: Yes. Mobile clients can dynamically render UI based on available links.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro