REST at Layer 5: Designing the API Layer

Every codebase has one: a REST route that grew to eighty lines. It checks a loan limit, computes a late fee, sends a notification and then, finally, returns JSON. When the mobile team asks for a GraphQL version, somebody copies the whole thing. LVD's Layer 5 exists to stop that story at the first route, by giving the API layer a single job.

A layer that only translates

Layer 5 is the main doorway for human-facing clients: web and mobile applications, partner integrations and other internal services. It carries a volatility coefficient of 0.85, reflecting how often endpoint shapes, versioning schemes and protocol choices change compared with the business logic beneath.

It may depend on the Tools layer and below. In return it is held to a hard constraint: no business logic, no validation rules and no domain calculations. Its sole responsibility is translation. Take in a protocol-specific request, delegate to the right tool handler, and shape the tool's result into a protocol-specific response. If a decision about the business is happening in this layer, it is in the wrong place.

What a thin route looks like

REST models the domain as resources with addresses. In LVD those resources line up with schemas in Layer 1 and operations in Layer 4, so each tool maps naturally onto a verb and a path. A typical route is almost nothing but plumbing:

router.post(
  "/api/v1/bookings",
  authenticate,
  validateBody(CreateBookingParams),   // schema comes from L1
  wrap(async (req) => {
    const result = await createBookingHandler({ ...req.body, guestId: req.user.id });
    return { status: 201, body: result.data };
  }),
);

A few details are worth noticing. The body check applies a Layer 1 schema, so the route enforces a rule it does not own. The identity of the caller comes from the authentication step and is passed in as ordinary data. And the handler returns a status and a body instead of touching the response object, because a small shared wrapper deals with that part and with translating errors.

One table for errors

Tools signal problems with domain errors. The API layer owns the conversion to HTTP, and the book wants that conversion in one central place rather than scattered through route code. The mapping is the following:

  • validation error: 400 Bad Request
  • authentication error: 401 Unauthorized
  • authorization error: 403 Forbidden
  • not found: 404
  • conflict: 409
  • rate limit exceeded: 429, with a retry hint
  • anything unhandled: 500, with a generic message

The last line deserves a second look. For unexpected failures the client receives a deliberately bland message, so internals do not leak out through an error string. A central error handler looks up each error's class in a map and falls back to 500 when it finds nothing. That keeps individual routes free of try and catch noise, and it means a change to the error format is a change in one file.

Choosing a versioning style

REST APIs eventually need versions, and the book compares three common approaches.

  1. In the path (/api/v1/bookings): explicit and easy to discover, though routes multiply as versions pile up.
  2. In a header: tidier URIs, but harder to discover.
  3. In a query parameter: simple to build, but it mixes versioning with filtering.

The book favours the path version for LVD systems. Its argument is that LVD values explicit dependency declaration, and a version that appears in every route definition and every import path is as explicit as it gets. Whether you agree for your own system, the reasoning is a useful example of applying the model's values to an ordinary design choice.

The takeaway

A well-formed REST layer is thin enough to read in a minute. Translate in, delegate, translate out, and map errors in a single place. Everything interesting happens a layer down, which is why the same tool can later be exposed through a second protocol without being rewritten. That is the subject of GraphQL, gRPC and living API documentation. Who is allowed to call these routes is the concern of the authentication sublayer, and the layer's place in the stack is described on the Layer 5 page.

What the book covers

  • The algorithm that maps a tool onto a REST endpoint
  • A full Express router example and the shared handler wrapper
  • The complete mapping of domain errors to HTTP status codes and response bodies
  • Screen-specific endpoints and how they stay free of business logic