# Where to set secret for API\_KEY

**URL:** <https://community.postman.com/t/where-to-set-secret-for-api-key/19157>\
**Category:** Help Hub\
**Tags:** authentication\
**Created:** [January 14, 2021, 10:49am UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157 "2021-01-14T10:49:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit0001](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@amit0001](https://community.postman.com/u/amit0001)\
**Post date:** [January 14, 2021, 10:49am UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/1 "2021-01-14T10:49:04Z")

</div>

Hi,

I’m using an API which needs an API\_KEY and API\_SECRET to be sent in the headers for each request.

I want to set this once for the entire collection of requests.

In the collection auth, it’s possible to enter an API\_KEY but not a secret.

Any suggestions on how to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 14, 2021, 11:38am UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/2 "2021-01-14T11:38:10Z")

</div>

you can add custom headers 🙂

```
pm.request.addHeader("API_KEY","value")
pm.request.addHeader("API_SECRET","value")

```

Add this to pre-requisite script in the collection level

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 14, 2021, 12:47pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/3 "2021-01-14T12:47:04Z")

</div>

What kind of Auth is your API using?

If the API is using basic authentication, you must combine the api key and secret in this format:

```auto
api_key:api_secret

```

and then base64 encode it. You would then pass in the encoded string as your authentication. [This site](https://www.blitter.se/utils/basic-authentication-header-generator/) does a pretty good job at making it simple for you to get it in the right format.

If your API auth uses OAuth2.0, the steps are a bit more involved, we can go into that if you think it’s necessary.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 14, 2021, 1:33pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/4 "2021-01-14T13:33:05Z")

</div>

That’s a basic authentication , postman natively supports it

You will get in the authentication drop down

---

<div class="post-metadata">

**Author:** ![amit0001](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@amit0001](https://community.postman.com/u/amit0001)\
**Post date:** [January 14, 2021, 3:23pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/5 "2021-01-14T15:23:07Z")

</div>

Thanks guys. This site doesn’t use basic auth, they just need their specific key and secret values in the headers.

It would be nice if the product supported common headers in a tab, like variables, but I guess some pr-request javascript will do. 🙂

---

<div class="post-metadata">

**Author:** ![amit0001](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@amit0001](https://community.postman.com/u/amit0001)\
**Post date:** [January 14, 2021, 3:30pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/6 "2021-01-14T15:30:19Z")

</div>

I just came across a problem with this solution. The only works when running the whole collection in the collection runner. When running a single request the collection pre-request scripts don’t run.

With auth, this does work since there is inheritance.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 14, 2021, 3:39pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/7 "2021-01-14T15:39:18Z")

</div>

Thats not correct , it should work for single request also . What postman version you are using

---

<div class="post-metadata">

**Author:** ![amit0001](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@amit0001](https://community.postman.com/u/amit0001)\
**Post date:** [January 14, 2021, 4:30pm UTC](https://community.postman.com/t/where-to-set-secret-for-api-key/19157/8 "2021-01-14T16:30:11Z")

</div>

Sorry, my mistake. It does work by setting the headers in the collection - even when running a single request BUT for me it only works using the following format:

pm.request.headers.add( { key: “API\_KEY”, value: pm.environment.get(“API\_KEY”) } );  
pm.request.headers.add( { key: “API\_SECRET”, value: pm.environment.get(“API\_SECRET”) } );

I’m using postman in the browser: v7.26.10
