Discovering API documentation, contracts and status over HTTP with RFC 8631
Apply four HTTP link relations defined in 2019 to a warehouse service. A local Node.js example verifies an empty HEAD response, a JSON contract and a degraded tracking component.
Archive retrospective · Topic year 2019
In this article
Start a warehouse integration with one known address
A device that picks orders in a warehouse starts with the orders URL. Its operator may want a guide explaining what counts as a picked order. An integration client instead needs a description it can process. A dispatch supervisor needs to know whether the tracking component is working. Hard-coding three further addresses makes every deployment responsible for keeping those addresses up to date. RFC 8631, published in July 2019 as an Informational RFC, defines four registered link relations that can describe these connections. The warehouse service can expose them in an HTTP response from the orders resource. A consumer starts at that resource and selects a destination using the advertised relation. Moving the guide then involves updating the published link. The initial address still comes from configuration, a service catalogue, or another trusted starting point. The cover is an original illustrative diagram of the four links exposed by the local orders resource.
Distinguish guidance, machine descriptions and metadata
The service-doc relation points to guidance intended mainly for people. It can take a warehouse operator to an HTML page explaining the order lifecycle. A machine-oriented description is identified by service-desc. Its format depends on the service. OpenAPI is one possible choice. The local experiment uses a smaller JSON object to make the discovery sequence visible without additional dependencies. The service-meta relation can identify a resource describing the service's context, such as its owner or classification. A contract or metadata document can contain a version field. Its meaning depends on whether it describes the orders interface or the accompanying information. Publishing the two relations separately allows a consumer to choose the kind of information it needs without looking for it on a human-oriented landing page.
Read the relation and then inspect the target
The example server returns four typed links in the Link field of an HTTP HEAD response. The request does not transfer a list of orders. Each link has a target, a relation, and a media-type hint. The client chooses a link by relation and resolves its root-relative target against the orders URL. If the service is moved to a new host and still uses relative links, the new host becomes the base for those references. The advertised type can help the client decide whether to follow a link. Once it does, it still checks the returned Content-Type and validates the payload. The demo uses a narrow parser for the syntax its own server emits. RFC 8288 defines a broader grammar that includes quoted parameters and relation lists, so a general integration needs a parser suitable for that grammar.
Run the test against a loopback server
The Node.js script starts an HTTP server on 127.0.0.1 and lets the operating system choose an unused port. It verifies a successful HEAD response with no body and checks all four relation names. It resolves the service-desc destination, checks that the target is on the expected origin, and asserts that the returned JSON describes version 2 of the orders resource. It separately requests the status resource and checks that it reports a degraded tracking component. The expected line is four relations, empty HEAD, contract v2, tracking degraded. The local run produced that line using Node.js v24.19.0. The test uses a modern runtime to demonstrate a relation vocabulary published in 2019. The guide and metadata links are advertised for demonstration and are deliberately left out of the local client's requests.
import assert from 'node:assert/strict';
import {createServer} from 'node:http';
import {once} from 'node:events';
const links = [
'</depot/guide>; rel="service-doc"; type="text/html"',
'</depot/contract>; rel="service-desc"; type="application/json"',
'</depot/about>; rel="service-meta"; type="application/json"',
'</status/depot>; rel="status"; type="application/json"'
];
const server = createServer((request, response) => {
if (request.url === '/depot/orders') {
response.setHeader('Link', links.join(', '));
response.setHeader('Content-Type', 'application/json');
response.end(request.method === 'HEAD' ? undefined : '[]');
return;
}
if (request.url === '/depot/contract') {
response.writeHead(200, {'Content-Type':'application/json'});
response.end(JSON.stringify({version:'2', resource:'orders'}));
return;
}
if (request.url === '/status/depot') {
response.writeHead(200, {'Content-Type':'application/json'});
response.end(JSON.stringify({state:'degraded', component:'tracking'}));
return;
}
response.writeHead(404).end();
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
try {
const origin = `http://127.0.0.1:${server.address().port}`;
const resource = new URL('/depot/orders', origin);
const head = await fetch(resource, {method:'HEAD'});
assert.equal(head.status, 200);
assert.equal(await head.text(), '');
// Parse only the exact link syntax emitted by this example server.
const pattern = /<([^>]+)>; rel="([^"]+)"; type="([^"]+)"/gu;
const offer = new Map([...head.headers.get('link').matchAll(pattern)]
.map(([,path,rel,type]) => [rel, {url:new URL(path, resource), type}]));
assert.deepEqual([...offer.keys()], ['service-doc','service-desc','service-meta','status']);
const contract = offer.get('service-desc');
assert.equal(contract.type, 'application/json');
assert.equal(contract.url.origin, resource.origin);
const description = await fetch(contract.url);
assert.equal(description.status, 200);
assert.equal(description.headers.get('content-type'), 'application/json');
assert.deepEqual(await description.json(), {version:'2', resource:'orders'});
const statusUrl = offer.get('status').url;
assert.equal(statusUrl.origin, resource.origin);
const status = await fetch(statusUrl);
assert.equal(status.status, 200);
assert.deepEqual(await status.json(), {state:'degraded', component:'tracking'});
console.log('four relations, empty HEAD, contract v2, tracking degraded');
} finally {
server.close();
await once(server, 'close');
}
Allow the status resource to describe a degraded component
In the example the status response returns HTTP 200 while its body reports degraded tracking. The response code establishes that the status resource was read successfully. The payload describes the condition of the system it monitors. Those are different decisions in the warehouse client. A client can keep accepting orders while postponing optional tracking requests if the business process allows it. The team also has to choose how current the information needs to be. An old operator guide can still be helpful after a short interval, while a stale incident report can keep a client in the wrong mode. RFC 8631 identifies the relationship to the status resource and leaves the actual status model and its update policy to the service.
Test a contract move before depending on discovery
Move the description document to a new path while keeping the orders resource and its relation name stable. A discovery client should still find the current document. Remove service-desc and observe the result separately. An integration can disable an optional capability, consult a configured fallback, or return a clear discovery error depending on the agreement it has with the service. Another useful case is a description URL that returns HTML when the consumer expected JSON. A client that checks the response at the destination can reject that response without treating a landing page as a contract. The loopback demo follows only links to its own server. A deployed integration needs an explicit trust policy for external hosts and redirects. Each of these tests tracks a decision a client has to make as the service changes.
Reader discussion
Which alternative architecture would you try for the same problem?
No comments yet. Be the first to contribute.
Write a comment