koa-requestid is a lightweight Koa middleware designed to automatically generate and attach a unique request ID to incoming HTTP requests. By default, it uses UUIDv4 for generation and exposes the ID via the `Request-Id` response header, also storing it in `ctx.state.id` for easy access within downstream middleware or route handlers. The current stable version is 2.3.0, with minor updates released periodically based on maintenance needs and dependency updates, as seen in the recent changelogs. Key differentiators include its simplicity, minimal configuration overhead, and the ability to accept custom request IDs from specified request headers or query parameters, which is particularly useful for debugging or tracing across services. Since version 2.0.2, it ships with comprehensive TypeScript definitions, enhancing developer experience for TypeScript-based Koa applications. It is specifically built for Koa applications, leveraging Koa's middleware pattern and context object.
npm install koa-requestidVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `koa-requestid` into a TypeScript Koa application, configure custom headers/query parameters, and access the generated or provided request ID from `ctx.state.id`.
Ensure your Koa application is using Koa 2.x or later. Migrate any generator-based middleware to `async/await` functions. Alternatively, if upgrading Koa is not feasible, use `koa-requestid` versions prior to 2.0.0 (e.g., `^1.0.0`).
Upgrade your Node.js environment to version 20 or newer. If forced to use an older Node.js version, consider using an older version of `koa-requestid` if compatible or evaluate alternative request ID middleware for Koa.
If `koa-requestid` is configured to read custom IDs from client input (e.g., `header` or `query` options are set), add custom middleware *before* `koa-requestid` to validate or sanitize these inputs according to your application's security requirements.
Upgrade to `koa-requestid@2.0.2` or newer to leverage official TypeScript definitions. If using older versions, you may need to install `@types/koa-requestid` if available, or declare the module manually.
Ensure your application is built with Koa 2.x or newer. If you are using Express or another framework, `koa-requestid` is not compatible; you'll need a request ID middleware specific to that framework.
To resolve this, you need to augment Koa's `DefaultState` interface in your TypeScript project. Add a declaration like `declare module 'koa' { interface DefaultState { id: string; } }` in a global `.d.ts` file or at the top of a relevant `.ts` file.First, ensure the package is installed: `npm install koa-requestid` or `yarn add koa-requestid`. Verify the import path `from 'koa-requestid'`. If using an older version of `koa-requestid` (<2.0.2) in TypeScript, consider upgrading or installing `@types/koa-requestid` if available.