# Accessing Kong Request ID in Custom Plugin

**URL:** https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911
**Category:** Questions
**Tags:** plugins
**Created:** [August 8, 2024, 2:39pm UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911 "2024-08-08T14:39:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![shauliriger](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/shauliriger/32/4599_2.png) [@shauliriger](https://discuss.konghq.com/u/shauliriger)
#### Post date: [August 8, 2024, 2:39pm UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911/1 "2024-08-08T14:39:53Z")

</div>

Hi Kong Community,

Due to security requirements, I need to remove the `x-kong` headers from the response by setting `KONG_HEADERS=off`.  
However, I still need to retain the `requestId` functionality by adding it as a custom header (e.g., `request-id`) and also including it in plugin logs for debugging purposes.

My Questions:

1. How can I access the Kong `requestId` from within my custom plugin?
2. Is there a recommended way to add this `requestId` to a custom header while keeping

Thanks

---

<div class="post-metadata">

### Author: ![samugi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/samugi/32/4510_2.png) [@samugi](https://discuss.konghq.com/u/samugi)
#### Post date: [August 13, 2024, 4:24pm UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911/2 "2024-08-13T16:24:41Z")

</div>

Hello @shauliriger , thank you for your questions!

Am I right that you’re attempting to keep the Request ID header but omit the latency tokens? If that is the case, perhaps setting `KONG_HEADERS=X-Kong-Request-Id` (instead of `off`) would achieve the desired result, could you give that a try?

Thank you

---

<div class="post-metadata">

### Author: ![shauliriger](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/shauliriger/32/4599_2.png) [@shauliriger](https://discuss.konghq.com/u/shauliriger)
#### Post date: [August 13, 2024, 7:29pm UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911/3 "2024-08-13T19:29:48Z")

</div>

Hi @samugi, thank you for your response!

I appreciate the suggestion. However, my current setup has `KONG_HEADERS=off` as required by our security guidelines. What I’m specifically trying to achieve is accessing the `requestId` within my custom plugin. The goal is to log this `requestId` and add it to the response header under a different name, such as `request-id`.

Is there a way to access the `requestId` from within the plugin under these conditions?

Thanks again!

---

<div class="post-metadata">

### Author: ![samugi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/samugi/32/4510_2.png) [@samugi](https://discuss.konghq.com/u/samugi)
#### Post date: [August 14, 2024, 6:23am UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911/4 "2024-08-14T06:23:49Z")

</div>

Hello @shauliriger  
to access the request\_id from code you can do something like:

```auto
local request_id_get = require("kong.observability.tracing.request_id").get
local request_id = request_id_get() or ""

```

this can then be set as a header:

```auto
kong.service.request.set_header("Request-Id-Header", request_id)

```

This is equivalent to setting `KONG_HEADERS=X-Kong-Request-Id`, except for the name of the header, because by specifying what header is set, Kong will only inject that one (in this case, the request ID).

The custom plugin may allow specifying advanced logic to decide when to inject the header, but keep in mind that you would be accessing the request ID through an internal module since this is currently not exposed via the PDK, so this interface might change in the future without notice and potentially break your logic.

I hope that helps! Regards.

---

<div class="post-metadata">

### Author: ![shauliriger](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/shauliriger/32/4599_2.png) [@shauliriger](https://discuss.konghq.com/u/shauliriger)
#### Post date: [August 14, 2024, 6:48am UTC](https://discuss.konghq.com/t/accessing-kong-request-id-in-custom-plugin/12911/5 "2024-08-14T06:48:20Z")

</div>

Hi @samugi , thanks again. My custom plugin is implemented with Go. What’s the equivalent code in Go?
