# How to use Cookies to Store Secrets in Postman

Storing your secrets (e.g., API keys and passwords) in Postman environments might not meet your security requirements. Although they are stored at rest, they are accessible to every team member in plaintext. You can leverage Postman cookies as an alternative because they are local to the machine, the computer user, and the Postman desktop client.

You might be thinking, “Cookies are bad. They are also plain text.”

True. They are, but at least they are not stored in the Postman servers and accessible by every team member.

The [Postman Sandbox](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries) supports the [crypto-js](https://www.npmjs.com/package/crypto-js) package, which you can use to add more security to the cookie’s value.

Let’s explore how to use a Postman cookie. I’m not going to do the encryption for simplicity.

## Creating the Cookie

In your request, click the “Cookies” link.

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/326/1*EMZdGGgX_fh45e3-IOEFWQ.png" width="163" height="87"/></noscript>

*The “Cookies” link is below the “Send” button in your request.*

Whitelist a domain.

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/248/1*VrBMhxrMZdNS0KbeFNtBQg.png" width="124" height="48"/></noscript>

*This link is located at the bottom-left of the Cookies dialog.*

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1332/1*bLoNVU4c1rcXxpSvWbsehw.png" width="666" height="152" srcSet="https://miro.medium.com/max/552/1*bLoNVU4c1rcXxpSvWbsehw.png 276w, https://miro.medium.com/max/1104/1*bLoNVU4c1rcXxpSvWbsehw.png 552w, https://miro.medium.com/max/1280/1*bLoNVU4c1rcXxpSvWbsehw.png 640w, https://miro.medium.com/max/1332/1*bLoNVU4c1rcXxpSvWbsehw.png 666w" sizes="666px"/></noscript>

*Use a fake domain.*

Add a domain to your cookies.

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1432/1*JKLPtFZUURX2iVQCoctsQw.png" width="716" height="100" srcSet="https://miro.medium.com/max/552/1*JKLPtFZUURX2iVQCoctsQw.png 276w, https://miro.medium.com/max/1104/1*JKLPtFZUURX2iVQCoctsQw.png 552w, https://miro.medium.com/max/1280/1*JKLPtFZUURX2iVQCoctsQw.png 640w, https://miro.medium.com/max/1400/1*JKLPtFZUURX2iVQCoctsQw.png 700w" sizes="700px"/></noscript>

*Add the fake domain to your cookies.*

Add a cookie.

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1378/1*SxMpPyZNkDJdnPWXDahvZA.png" width="689" height="334" srcSet="https://miro.medium.com/max/552/1*SxMpPyZNkDJdnPWXDahvZA.png 276w, https://miro.medium.com/max/1104/1*SxMpPyZNkDJdnPWXDahvZA.png 552w, https://miro.medium.com/max/1280/1*SxMpPyZNkDJdnPWXDahvZA.png 640w, https://miro.medium.com/max/1378/1*SxMpPyZNkDJdnPWXDahvZA.png 689w" sizes="689px"/></noscript>

*Add a cookie to the fake domain.*

You can now use the cookie’s value in your request.

## Using the cookie

Use a pre-request script to set a local variable. Local variables only apply to the request per the Postman [documentation](https://learning.postman.com/docs/sending-requests/variables/#variable-scopes).


```js
const cookieJar = pm.cookies.jar();
const cookieName = "apiKey"
cookieJar.get('my-secrets.com', cookieName, (error, cookie) => {
  if (error) {
    console.error(error);
    pm.variables.set(cookieName, "error");
  }
  if (cookie) {
    pm.variables.set(cookieName, cookie);
  } else {
    console.error("Cookie is missing")
    pm.variables.set(cookieName, "missing");
  }
});
```

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1270/1*2Vc5ghThlVE8wS3-nhV90g.png" width="635" height="295" srcSet="https://miro.medium.com/max/552/1*2Vc5ghThlVE8wS3-nhV90g.png 276w, https://miro.medium.com/max/1104/1*2Vc5ghThlVE8wS3-nhV90g.png 552w, https://miro.medium.com/max/1270/1*2Vc5ghThlVE8wS3-nhV90g.png 635w" sizes="635px"/></noscript>

*The pre-request script code.*

Use the variable in the request as usual. In this example, we will use the “apiKey” variable in the headers.

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1474/1*unvf-2EtTg-g9k48h2gS4A.png" width="737" height="410" srcSet="https://miro.medium.com/max/552/1*unvf-2EtTg-g9k48h2gS4A.png 276w, https://miro.medium.com/max/1104/1*unvf-2EtTg-g9k48h2gS4A.png 552w, https://miro.medium.com/max/1280/1*unvf-2EtTg-g9k48h2gS4A.png 640w, https://miro.medium.com/max/1400/1*unvf-2EtTg-g9k48h2gS4A.png 700w" sizes="700px"/></noscript>

*Setting the header using the variable containing the cookie’s value.*

To be safe, make sure to clear the local variables. We will do this in the tests.


```js
// clear just the variable
pm.variables.unset("apiKey")
// or clear all the local variables
pm.variables.clear()
```

<noscript><img alt="Image for post" class="t u v gu aj" src="https://miro.medium.com/max/1146/1*Fs1jkVGjN5q6dHQfssirJg.png" width="573" height="134" srcSet="https://miro.medium.com/max/552/1*Fs1jkVGjN5q6dHQfssirJg.png 276w, https://miro.medium.com/max/1104/1*Fs1jkVGjN5q6dHQfssirJg.png 552w, https://miro.medium.com/max/1146/1*Fs1jkVGjN5q6dHQfssirJg.png 573w" sizes="573px"/></noscript>

*Clearing the variable after the request.*

## Conclusion

The approach will allow you to use secrets in Postman without sharing them with the world.

## Before you go

%%[mailing-list]

### About the author

%%[bio]

<hr>

*Originally published on [Medium.com](https://medium.com/javascript-in-plain-english/how-to-use-cookies-to-store-secrets-in-postman-afa9a666590d)*

*Photo by [Christina Branco](https://unsplash.com/@starvingartistfoodphotography) on [Unsplash](https://unsplash.com)*
