# Send a request in pre-request with the API Key in the header

**URL:** <https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445>\
**Category:** Help Hub\
**Tags:** tests, automation, json, headers\
**Created:** [January 10, 2024, 11:38pm UTC](https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445 "2024-01-10T23:38:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![altimetry-physicis10](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@altimetry-physicis10](https://community.postman.com/u/altimetry-physicis10)\
**Post date:** [January 10, 2024, 11:38pm UTC](https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445/1 "2024-01-10T23:38:09Z")

</div>

Hi

I want to send a request from the pre-request Script but my endpoint uses API Key as headers to access, so I have next:

 ![Screenshot 2024-01-10 at 6.35.36 PM](https://us1.discourse-cdn.com/getpostman/original/3X/c/e/ce09f8ff365a217cd051abd392aaf7d94e026e01.png)

and the next code

```auto
function sendRequest(name){
   pm.sendRequest({
        url: (pm.collectionVariables.get("baseUrl") + "/companies"),
        method: 'POST',
        headers: {
            'sn-api-key': pm.collectionVariables.get("snApiKeyInternal"),
            'sn-api-secret': pm.collectionVariables.get("snApiSecretExternal")
  },
        body: {
            mode: 'raw',
            raw:JSON.stringify(name)
        } 
    },(err,response) => {
        console.log(response);
    });
}

```

but I’m getting a 401 error. Any idea?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 10, 2024, 11:42pm UTC](https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445/2 "2024-01-10T23:42:10Z")

</div>

Hey @altimetry-physicis10 👋

Welcome to the Postman community! :postman:

I believe it should be `header` and not `headers`, that might be one the issues here.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 11, 2024, 10:44am UTC](https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445/3 "2024-01-11T10:44:09Z")

</div>

You also seem to be mixing the internal API key and the external secret.

sendRequests are also logged in the console, so check the console as a troubleshooting step to ensure any variables being sent are being transposed correctly.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 10, 2024, 10:44am UTC](https://community.postman.com/t/send-a-request-in-pre-request-with-the-api-key-in-the-header/56445/4 "2024-02-10T10:44:13Z")

</div>

Closed on February 10, 2024, 10:44am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
