How to use cookies to store secrets in Postman
Tags: security, api, postman, inthirtyseconds
Published: 2020-10-29 00:10:39

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.

![](https://miro.medium.com/max/163/1*EMZdGGgX_fh45e3-IOEFWQ.png)
<figcaption>The “Cookies” link is below the “Send” button in your request.</figcaption>

Whitelist a domain.

![](https://miro.medium.com/max/124/1*VrBMhxrMZdNS0KbeFNtBQg.png) <figcaption>This link is located at the bottom-left of the Cookies dialog.</figcaption>

![](https://miro.medium.com/max/666/1*bLoNVU4c1rcXxpSvWbsehw.png)
<figcaption>Use a fake domain.</figcaption>

Add a domain to your cookies.

![](https://miro.medium.com/max/700/1*JKLPtFZUURX2iVQCoctsQw.png)
<figcaption>Add the fake domain to your cookies.</figcaption>

Add a cookie.

![](https://miro.medium.com/max/689/1*SxMpPyZNkDJdnPWXDahvZA.png)
<figcaption>Add a cookie to the fake domain.</figcaption>

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");
  }
});
```

![](https://miro.medium.com/max/635/1*2Vc5ghThlVE8wS3-nhV90g.png)
<figcaption>The pre-request script code.</figcaption>

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

![](https://miro.medium.com/max/700/1*unvf-2EtTg-g9k48h2gS4A.png)
<figcaption>Setting the header using the variable containing the cookie’s value.</figcaption>

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()
```

![](https://miro.medium.com/max/573/1*Fs1jkVGjN5q6dHQfssirJg.png)
Clearing the variable after the request.

# Conclusion

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

### A Note from the Author

Join my mailing list to receive updates about my writing.

Visit  [**miguelacallesmba.com/subscribe**](https://miguelacallesmba.com/subscribe) and sign up.

Stay secure,<br>Miguel

#### About the Author

Miguel is a Principal Security Engineer and is the author of the " [Serverless Security](https://ServerlessSecurityBook.com) " book. He has worked on multiple serverless projects as a developer and security engineer, contributed to open-source serverless projects, and worked on large military systems in various engineering roles.

<hr>

*This post was originally published on Medium.*

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

{%medium https://medium.com/javascript-in-plain-english/how-to-use-cookies-to-store-secrets-in-postman-afa9a666590d %}