# Using prerequest script to setting environmental variable from RestAPI result

**URL:** <https://community.postman.com/t/using-prerequest-script-to-setting-environmental-variable-from-restapi-result/971>\
**Category:** Help Hub\
**Tags:** setenvironmentvariab\
**Created:** [February 17, 2018, 5:15am UTC](https://community.postman.com/t/using-prerequest-script-to-setting-environmental-variable-from-restapi-result/971 "2018-02-17T05:15:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cluther](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cluther/32/351_2.png) [@cluther](https://community.postman.com/u/cluther)\
**Post date:** [February 17, 2018, 5:15am UTC](https://community.postman.com/t/using-prerequest-script-to-setting-environmental-variable-from-restapi-result/971/1 "2018-02-17T05:15:44Z")

</div>

HI,

I’m trying to share both a collections and environments among a team while protecting access to sensitive Access Keys. Company policy prevents saving these keys in a shared repository, such as Postman environment file.

I’ve used ENVKEY to set up a restful endpoint which responds back with the result of the Access Key. I’m trying to figure out how to create a prerequest script to:

- make the endpoint call,
- set an environment variable with the result

The api call is [http://localhost:3000/envkey/AUTHSTRING\_EMEA](http://localhost:3000/envkey/AUTHSTRING_EMEA)  
and it returns back a JSON key value array. The script should parse the result and set a variable from the result. My current nonworking script is:

```
var endpoint = 'http://localhost:3000/envkey/AUTHSTRING_EMEA';
var xhr = new XMLHttpRequest();
xhr.open("GET", endpoint, false);
xhr.send();

console.log(xhr.status);
console.log(xhr.statusText);

pm.environment.set("authstring", xhr.result);
```

---

<div class="post-metadata">

**Author:** ![dhoyt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/dhoyt/32/238_2.png) [@dhoyt](https://community.postman.com/u/dhoyt)\
**Post date:** [February 19, 2018, 4:34pm UTC](https://community.postman.com/t/using-prerequest-script-to-setting-environmental-variable-from-restapi-result/971/2 "2018-02-19T16:34:59Z")

</div>

Check out pm.sendRequest …

[https://www.getpostman.com/docs/postman/scripts/postman\_sandbox\_api\_reference](https://www.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference)
