Skip to content

requestId()

app.requestId([options])
NameTypeDescription
optionsObjectOptional configuration object.
options.headerNamestringCustom header name (default: 'X-Request-Id').
options.generatorFunctionCustom ID generator function (default: crypto.randomUUID()).

This middleware automatically injects a unique ID into every incoming request and outgoing response. If an upstream service (like a proxy or API gateway) already provides the header, Kaelum preserves the existing ID to maintain distributed tracing continuity.

The ID is exposed as req.id on the Request object for easy access in your route handlers or logging utilities.

// Default usage
app.requestId();
// Access the ID inside a handler
app.get('/api/users', (req, res) => {
logger.info(`Fetching users. Request ID: ${req.id}`);
res.json({ users: [] });
});
// Custom header name and custom ID format
app.requestId({
headerName: "X-Trace-Id",
generator: () => `trace_${Date.now()}_${Math.random()}`
});