Request ID Middleware
Request ID Middleware generates a unique ID for each request, which you can use in your handlers.
INFO
Node.js: This middleware uses crypto.randomUUID() to generate IDs. The global crypto was introduced in Node.js version 20 or later. Therefore, errors may occur in versions earlier than that. In that case, please specify generator. However, if you are using the Node.js adapter, it automatically sets crypto globally, so this is not necessary.
Import
import { Hono } from 'hono'
import { requestId } from 'hono/request-id'Usage
You can access the Request ID through the requestId variable in the handlers and middleware to which the Request ID Middleware is applied.
const app = new Hono()
app.use('*', requestId())
app.get('/', (c) => {
return c.text(`Your request id is ${c.get('requestId')}`)
})If you want to explicitly specify the type, import RequestIdVariables and pass it in the generics of new Hono().
import type { RequestIdVariables } from 'hono/request-id'
const app = new Hono<{
Variables: RequestIdVariables
}>()Set Request ID
You set a custom request ID in the header (default: X-Request-Id), the middleware will use that value instead of generating a new one:
const app = new Hono()
app.use('*', requestId())
app.get('/', (c) => {
return c.text(`${c.get('requestId')}`)
})
const res = await app.request('/', {
headers: {
'X-Request-Id': 'your-custom-id',
},
})
console.log(await res.text()) // your-custom-idIf you want to disable this feature, set headerName option to an empty string.
Options
optional limitLength: number
The maximum length of the request ID. The default is 255.
optional headerName: string
The header name used for the request ID. The default is X-Request-Id.
optional generator: (c: Context) => string
The request ID generation function. By default, it uses crypto.randomUUID().
Platform specific Request IDs
Some platform (such as AWS Lambda) already generate their own Request IDs per request. Without any additional configuration, this middleware is unaware of these specific Request IDs and generates a new Request ID. This can lead to confusion when looking at your application logs.
To unify these IDs, use the generator function to capture the platform specific Request ID and to use it in this middleware.
Platform specific links
- AWS Lambda
- Cloudflare
- Deno
- Fastly