# Sending an API Key as a Header (Calling REST)

**URL:** <https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943>\
**Category:** General\
**Created:** [2 August 2019 15:36 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943 "2019-08-02T15:36:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leftShark2019](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@leftShark2019](https://discourse.nodered.org/u/leftShark2019)\
**Post date:** [2 August 2019 15:36 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/1 "2019-08-02T15:36:28Z")

</div>

Hello,

I've read this [Calling REST api](https://discourse.nodered.org/t/calling-rest-api/1089)  
and know that to query an API requires an inject, change, http request, and debug node.

I'm trying to send a GET request to this API, but to receive a 2xx response I need to include an API key, so I'm trying to do that with a msg.header in the change node, but am uncertain if I should do that with string format. I'm trying to use JSON.

Guidance on how to structure the msg.header object so the API accepts the key would be appreciated.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 August 2019 17:16 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/2 "2019-08-02T17:16:46Z")

</div>

Headers need to be an array of objects where the object key is the header name and the object value is the header value.

Something like (untested):

```auto
msg.headers = [
    "Ocp-Apim-Subscription-Key": "069525d4f10146dea45c8d6bb3d81905",
]

```

---

<div class="post-metadata">

**Author:** ![leftShark2019](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@leftShark2019](https://discourse.nodered.org/u/leftShark2019)\
**Post date:** [2 August 2019 17:19 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/3 "2019-08-02T17:19:02Z")

</div>

That is actually pretty close to what I tried with a function node, I just used brackets {} to contain the JSON. No such luck. The change node I worked with was somewhat less clear.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 August 2019 17:22 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/4 "2019-08-02T17:22:32Z")

</div>

My bad, of course I forgot the inner {}

```auto
[{"id":"55e06a3a.782524","type":"change","z":"63281c77.40a064","name":"","rules":[{"t":"set","p":"headers","pt":"msg","to":"[{\"Ocp-Apim-Subscription-Key\":\"069525d4f10146dea45c8d6bb3d81905\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":1660,"wires":[[]]}]

```

```auto
msg.headers = [
    {
        "Ocp-Apim-Subscription-Key": "069525d4f10146dea45c8d6bb3d81905"
    }
]

```

---

<div class="post-metadata">

**Author:** ![leftShark2019](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@leftShark2019](https://discourse.nodered.org/u/leftShark2019)\
**Post date:** [2 August 2019 18:17 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/5 "2019-08-02T18:17:12Z")

</div>

Hm. Surprisingly, there seems to be an error symbol no matter which brackets I use.

---

<div class="post-metadata">

**Author:** ![leftShark2019](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@leftShark2019](https://discourse.nodered.org/u/leftShark2019)\
**Post date:** [2 August 2019 19:51 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/6 "2019-08-02T19:51:56Z")

</div>

I figured out it didn't need JSON, and successfully queried the API.  
{"events":,"hasErrors":false,"message":"Success"}

Used:

"msg.headers = {};  
msg.headers['']="";

return msg;"

In my function node.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 August 2019 22:53 UTC](https://discourse.nodered.org/t/sending-an-api-key-as-a-header-calling-rest/13943/7 "2019-08-02T22:53:32Z")

</div>

> [@TotallyInformation](#):
>
> My bad, of course I forgot the inner {}

Should just be { } ... no needed.

```auto
msg.headers = {
        "Ocp-Apim-Subscription-Key": "069525d4f10146dea45c8d6bb3d81905"
    }

```
