# koa-requestid

> A middleware that adds a request id to a koa application

Latest version **2.3.0** (published 2026-01-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-requestid
pnpm add koa-requestid
yarn add koa-requestid
bun add koa-requestid
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2026-01-05 |
| First published | 2015-07-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 13 |
| Author | Rui Marinho |
| Maintainers | botatuphold, ruimarinho, fixe |
| Keywords | koa, request, request-id, requestId |

## Links

- npm: https://www.npmjs.com/package/koa-requestid
- Repository: https://github.com/uphold/koa-requestid
- Homepage: https://github.com/uphold/koa-requestid/
- Issues: https://github.com/uphold/koa-requestid/issues
- npm.io page: https://npm.io/package/koa-requestid

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.3.0 (latest) — 2026-01-05
- 2.2.1 — 2023-11-21
- 2.1.0 — 2019-10-11
- 2.0.2 — 2019-10-11
- 2.0.1 — 2017-03-21
- 2.0.0 — 2017-03-17
- 1.0.0 — 2015-07-24

## README

# koa-requestid

## Status

[![npm version][npm-image]][npm-url]
[![CI][ci-image]][ci-url]

## Installation

Install the package via `yarn`:

```sh
❯ yarn add koa-requestid
```

or via `npm`:

```sh
❯ npm install koa-requestid --save
```

## Usage

Use `koa-requestid` as a middleware for a [koa](https://github.com/koajs/koa) app. By default, it generates a unique uuid (v4) and exposes it on the response via the `Request-Id` header. The id is also saved as part of the request *state*.

In the following example, the generated uuid is manually exposed on the body for debugging purposes:

```js
const Koa = require('koa');
const requestId = require('koa-requestid');
const app = new Koa();

app.use(requestId());
app.use(async ctx => {
  ctx.body = ctx.state.id;
});

app.listen(3000);
```

Execute a request to the running app:

```bash
❯ curl -v http://localhost:3000

< HTTP/1.1 200 OK
< Request-Id: cc0f12c7-f3b6-4c86-94c2-8c4ce7751651

cc0f12c7-f3b6-4c86-94c2-8c4ce7751651
```

Sometimes it is also useful to pass a custom id via a request header or query string, specifically in debugging sessions. Please note that the input id is not sanitized, so the usual precautions apply.

Using the above snippet to send a custom via the default `Request-Id` header:

```bash
❯ curl -v -H 'Request-Id: foobar' http://localhost:3000

< HTTP/1.1 200 OK
< Request-Id: foobar

foobar
```

or using a query string parameter (default is `requestId`):

```bash
❯ curl -v http://localhost:3000?requestId=foobar

< HTTP/1.1 200 OK
< Request-Id: foobar

foobar
```

## Configuration

#### Arguments
1. `options` *(object)*: A dictionary of options.

#### Options
1. `expose` *(string|false)*: The name of the header to expose the id on the response, or `false` to disable.
2. `header` *(string|false)*: The name of the header to read the id on the request, or `false` to disable.
3. `query` *(string|false)*: The name of the header to read the id on the query string, or `false` to disable.

Example:

```js
const Koa = require('koa');
const requestId = require('koa-requestid');
const app = new Koa();

app.use(requestId({
  expose: 'X-Request-Id',
  header: 'X-Req-Id',
  query: 'request-id'
}));
```

This configuration would expose every generated request id via the `X-Request-Id` response header and accept a custom id via the `X-Req-Id` header or `request-id` query string parameter.

## Tests

```
❯ yarn test
```

## Release

```sh
npm version [<new version> | major | minor | patch] -m "Release %s"
```

## License

MIT

[npm-image]: https://img.shields.io/npm/v/koa-requestid.svg
[npm-url]: https://www.npmjs.com/package/koa-requestid
[ci-image]: https://github.com/uphold/koa-requestid/actions/workflows/ci.yaml/badge.svg
[ci-url]: https://github.com/uphold/koa-requestid/actions/workflows/ci.yaml

---
_Source: https://npm.io/package/koa-requestid · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
