# How to Set Custom Response Header for a Specific Query in Apollo Server v4

**URL:** <https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832>\
**Category:** Server\
**Created:** [March 31, 2025, 12:11pm UTC](https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832 "2025-03-31T12:11:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhil](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/nikhil/32/5942_2.png) [@nikhil](https://community.apollographql.com/u/nikhil)\
**Post date:** [March 31, 2025, 12:11pm UTC](https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832/1 "2025-03-31T12:11:33Z")

</div>

Hi everyone,  
I’m using **Apollo Server v4 (standalone mode)** in a **Node.js** project, and I’m wondering if it’s possible to set a custom response header **only for a specific query** , and based on some **runtime condition**.

For example, I want to do something like:

```auto
query {
  sensitiveData {
    info
  }
}

```

And if a certain condition is met during the resolver execution, I want to include a custom response header like:

```auto
X-Custom-Warning: true

```

I know with Express we can modify res.setHeader, but since I’m using the standalone setup, I’m not sure of the best way to achieve this in Apollo Server v4.

Any suggestions or workarounds would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![michaeldouglasdev](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/michaeldouglasdev/32/4911_2.png) [@michaeldouglasdev](https://community.apollographql.com/u/michaeldouglasdev)\
**Post date:** [March 31, 2025, 1:12pm UTC](https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832/2 "2025-03-31T13:12:28Z")

</div>

Hello!

You can use an Apollo Plugin to handle this, specifically the `willSendResponse` lifecycle method.

> **[Apollo Server Plugin Event Reference](https://www.apollographql.com/docs/apollo-server/integrations/plugins-event-reference#willsendresponse)**

Another approach is to pass the response object through the context and modify it inside the resolver.

---

<div class="post-metadata">

**Author:** ![nikhil](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/nikhil/32/5942_2.png) [@nikhil](https://community.apollographql.com/u/nikhil)\
**Post date:** [April 1, 2025, 8:33am UTC](https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832/3 "2025-04-01T08:33:23Z")

</div>

Thanks for the reply! Yes, I saw the general plugin approach using `willSendResponse`, but my challenge is a bit more specific.

What I’m trying to do is:

- I have a **specific query** (e.g., `sensitiveData`)
- **Inside that resolver** , I check for a condition (e.g., a threshold or some flag)
- **Only if that condition is met** , I want to set a **custom header** in the response (e.g., `X-Custom-Warning: true`)
- This header should **not be set globally or for other queries**

So basically, I want **resolver-level control** over whether to add a response header or not — **without affecting other resolvers**.

Is there a clean way to do this in Apollo Server v4 standalone mode? I’m open to using `context`, plugins, or any workaround that enables this level of control.

Appreciate any pointers!

---

<div class="post-metadata">

**Author:** ![michaeldouglasdev](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/michaeldouglasdev/32/4911_2.png) [@michaeldouglasdev](https://community.apollographql.com/u/michaeldouglasdev)\
**Post date:** [April 1, 2025, 1:13pm UTC](https://community.apollographql.com/t/how-to-set-custom-response-header-for-a-specific-query-in-apollo-server-v4/8832/4 "2025-04-01T13:13:40Z")

</div>

> [@nikhil](#):
>
> Is there a clean way to do this in Apollo Server v4 standalone mode? I’m open to using `context`, plugins, or any workaround that enables this level of control.

In my previous answer, I mentioned an approach where you pass the response object through the context to the resolver, could you give it a try?

```auto
export const resolvers = {
  Query: {
    foo: (parent, args, context) => {
      const headers = new Headers({ "x-warning-123": "true" });
      context.res.setHeaders(headers);
      // ...
    }
  }
}

```
