# Access Postman Vault from Pre-Request Script

**URL:** <https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869>\
**Category:** Help Hub\
**Tags:** variables, scripts, vault\
**Created:** [May 9, 2024, 5:36pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869 "2024-05-09T17:36:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 9, 2024, 5:36pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/1 "2024-05-09T17:36:07Z")

</div>

How can you access values in the Postman Vault from Pre-Request Script?

---

<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:** [May 9, 2024, 6:21pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/2 "2024-05-09T18:21:59Z")

</div>

Hey @rcmcmullen2 👋

Welcome to the Postman community :postman:

You should be able to use:

```auto
pm.variables.get('vault:<var_name>' );

```

Is this working for you?

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 9, 2024, 6:40pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/3 "2024-05-09T18:40:21Z")

</div>

No. I’ve tried that as well as pm.environment.get(‘vault:\<var\_name\>’) and pm.collectionVariables.get(‘vault:\<var\_name\>’) and none of them are working. I created the associated key/value pairs in both Environment and Collection Variables.

---

<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:** [May 9, 2024, 6:48pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/4 "2024-05-09T18:48:55Z")

</div>

Taking a step back here, you’re using V11 right?

And you have created variables in your Postman Vault?

Could you share some images of what you have please? It will be easier to see what’s happening.

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 9, 2024, 7:07pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/5 "2024-05-09T19:07:44Z")

</div>

Yes, I’m using v11, and Yes I’ve created the variable in my Postman Vault

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/a/aa0b1878088115a64430b273cf2a44777c0c0256.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/6/66f7deff3e860fc7cef851184c74e51d02d12921.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/e/6e318fa9e24294d41d08c4303b26649d82b43c99.png)

---

<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:** [May 9, 2024, 7:27pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/6 "2024-05-09T19:27:18Z")

</div>

Interesting. 🤔

Are you able to use a vault value in the URL, Params or Headers?

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 9, 2024, 7:28pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/7 "2024-05-09T19:28:31Z")

</div>

I tried it in the Headers successfully.

---

<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:** [May 10, 2024, 8:14am UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/8 "2024-05-10T08:14:42Z")

</div>

My bad, I was automatically assuming that this could be achieved in the sandbox but after speaking with the team - I realised this was not the case. 😢

You can only access the vault variables in the URL, Params, Headers, Body and the Auth helper fields.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/9/69dd77fc1af60572c0130952f87011a7db94d6f7.png)

---

<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:** [May 10, 2024, 10:29am UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/9 "2024-05-10T10:29:02Z")

</div>

It would appear that you can set an environment variable to reference a key vault secret, but if you console log the environment variable, it just shows the secret name as a string.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/f/3f68e06e7ead153f026d294aac301730419e5131.png)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/0/f0052fd8ba6eef5019e9bb0ae09be41ae036d041.png)

You can see that the secret value “456” was used in the request (as its echoed back).

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/2/329cdad2c86094c9a04e71128619d9840da5d573.png)

However the console log of the environment variable just shows the string.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/a/3a76103fcbdea428e62aca7a476c97f0957ed4f7.png)

Which is probably correct, as its trying to prevent the leak of those secrets.

---

<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:** [May 10, 2024, 11:08am UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/10 "2024-05-10T11:08:18Z")

</div>

I should have been clearer - there isn’t a direct way to use them but there are _workarounds_ 😅

Similar to how you could you `pm.variables.set()` to temp change a vault variable during the request execution.

There isn’t a method like the other scoped variables to directly reference a vault variable.

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 10, 2024, 1:18pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/11 "2024-05-10T13:18:42Z")

</div>

So if we can’t access the Vault directly from the Sandbox then what is the suggested workaround? If I use the pm.variables.set() method I would need to either hard code the value (which needs to be secured), or try to reference the vault (which is not accessible).

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 10, 2024, 1:22pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/12 "2024-05-10T13:22:47Z")

</div>

I am currently using an environment variable, but the value is not secured like it is in the vault. The intention is to secure the value in a location that can being used by the Request.

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 10, 2024, 1:24pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/13 "2024-05-10T13:24:35Z")

</div>

That is a problem because we need this variable as part of a Pre-Request Script to generate a JWT Token.

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 10, 2024, 2:40pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/14 "2024-05-10T14:40:37Z")

</div>

@danny-dainton - I presume you cannot create a Variable (Local, Collection, Global) to reference the Vault, either?

---

<div class="post-metadata">

**Author:** ![pbalsara-wk](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pbalsara-wk](https://community.postman.com/u/pbalsara-wk)\
**Post date:** [May 13, 2024, 10:21pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/15 "2024-05-13T22:21:22Z")

</div>

It seems like you can create a variable at the local, collection, global to reference the Vault as the Postman documentation suggests, but you cannot access the Vault variable in pre/post-scripts. I’m running into the same issue as well.

I was trying to determine which Vault variable to use based on the environment variable selected. It doesn’t seem like there’s a way to do that.

---

<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:** [May 14, 2024, 9:17am UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/16 "2024-05-14T09:17:34Z")

</div>

I suspect at this point, its working as designed and this would be a feature request that needs to be raised on the Postman Github.

I sort of understand why as it prevents the encrypted info from being discovered through the scripts but as I also use pre-request scripts a lot for authentication, it would mean that I can’t use the vault for this either.

I can use the vault to integrate with Azure Key Vault to retrieve the OAuth information required to authenticate which is a good feature, but I have no way of accessing those details in the pre-request script where I use sendRequest to hit the token end point.

I would have to use the authentication helper or a straight up request in the GUI instead to get the token.

This feature is still quite new, and has had quite a few changes since the first iteration already, so requesting the feature on Github may provide a favourable response as this feature is being actively worked on.

---

<div class="post-metadata">

**Author:** ![rcmcmullen2](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rcmcmullen2](https://community.postman.com/u/rcmcmullen2)\
**Post date:** [May 14, 2024, 1:05pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/17 "2024-05-14T13:05:43Z")

</div>

Well I’m sorry to hear that as our Product license is coming up for renewal and our Project is looking for a product that can address this security concern. So it looks like we’ll have to look for other options.

---

<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:** [May 14, 2024, 1:14pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/18 "2024-05-14T13:14:17Z")

</div>

The first production iteration of the feature was only introduced 14 days ago and was never previously part of the platform, based on user feedback and the various different use cases that people might have, like creating tokens via a pre-request script, we can understand how the feature can be made better.

Postman like every software project, isn’t a finished product. It’s ever changing and improving to make our users lives easier.

Creating a public feature request is the best way to bring these new usage ideas to the team, to triage and assess them with a view to potentially making them part of the platform.

---

<div class="post-metadata">

**Author:** ![pbalsara-wk](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pbalsara-wk](https://community.postman.com/u/pbalsara-wk)\
**Post date:** [May 14, 2024, 10:18pm UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/19 "2024-05-14T22:18:18Z")

</div>

First time doing this so I hope I did it right. For anyone following, here’s the request: [Ability to use Postman Vault variables in pre/post-scripts · Issue #12864 · postmanlabs/postman-app-support · GitHub](https://github.com/postmanlabs/postman-app-support/issues/12864)

---

<div class="post-metadata">

**Author:** ![science-administrat5](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@science-administrat5](https://community.postman.com/u/science-administrat5)\
**Post date:** [May 17, 2024, 9:49am UTC](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869/20 "2024-05-17T09:49:46Z")

</div>

The documentation says that:

> You can also reference vault secrets as the values of your global, environment, and collection variables

Does that work for anyone? It works in params etc but when I added to environment variables it doesn’t seem to resolve.

This problem should be workaround-able if this worked, as the value of the secret is not stored in the cloud but the reference is (again, according to the documentation)

[Next page](https://community.postman.com/t/access-postman-vault-from-pre-request-script/60869.md?page=2)
