# Setting environment variables from within visualizer templates

**URL:** <https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068>\
**Category:** Help Hub\
**Tags:** setenvironmentvariab, template, visualizer\
**Created:** [June 2, 2020, 9:14pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068 "2020-06-02T21:14:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![themarty](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@themarty](https://community.postman.com/u/themarty)\
**Post date:** [June 2, 2020, 9:14pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068/1 "2020-06-02T21:14:58Z")

</div>

I’ve created a topic before, looking for a way to interact with Postman from within a template. It seemed that wasn’t possible.  
For a new personal project I’m looking for something similar, yet my hopes are that it’s closer to what postman currently offers.

I know you can set environment variables using `pm.environment.set()`. I’ve also found out you can do this in a template using ` ${pm.environment.set(‘key’, ‘value’)}`. What I would like to do though, is setting the value with javascript. But so far all my efforts are without result.

It would be incredibly handy if this was possible. Currently I have a lot of ‘list’ calls that I visualize with a template. There are lots of other calls that need information from that list call (such as an id). Now, from my other topic I know these calls can’t be invoked directly, but I was hoping that maybe I could create a button in my template that would allow me to set an environment variable (with the value of an id) so that I could achieve the same thing using two clicks instead of one. ( that way I could simply use the environment variable in those subsequent calls instead of having to copy-paste data into parameters the whole time)

Is this possible?

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [June 3, 2020, 12:45pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068/2 "2020-06-03T12:45:52Z")

</div>

Hi @themarty! This is an interesting one. 🤔 Have you tried anything with [pm.getData](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmgetdata)?

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [June 3, 2020, 1:02pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068/3 "2020-06-03T13:02:53Z")

</div>

Actually can I ask where the id value you’re trying to set as the var value is coming from?

---

<div class="post-metadata">

**Author:** ![themarty](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@themarty](https://community.postman.com/u/themarty)\
**Post date:** [June 3, 2020, 8:25pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068/4 "2020-06-03T20:25:22Z")

</div>

Hi @suesmith  
Thank you for your reply.

I have tried adding a function to the response data, but it simply gets ‘lost’ - meaning: if I console.log the entire object in my template, then the function is not there anymore. I’ve tried it with a simple function (`prop = function() { x=1; }`), but that already didn’t work, let alone trying to pass something more complicated.  
I wouldn’t know of any other way to use pm.getData to be able to set a postman environment variable - but if you have suggestions, then they are more then welcome! 🙂

The id value is in the resultset of an API call. So in my template I have something like this:

{{#each response}}  
\<tr\>  
\<td\>{{id}}\</td\>  
\<td\>{{name}}\</td\>  
[…]  
\</tr\>  
{{/each}}

And what I want to accomplish is that when a User clicks on the value of the id, a corresponding environment value is set. For example something like `pm.environment.set("user_id", {{id}});`  
If that would work then my other Postman calls needing that user-id could be written as (for example) {{API\_DOMAIN}}{{BASE\_URL}}/user/{{user\_id}}/. Without the need of having to manually copy-paste the user-id of the user you’re interested in, from the list of users.

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [June 3, 2020, 9:22pm UTC](https://community.postman.com/t/setting-environment-variables-from-within-visualizer-templates/13068/5 "2020-06-03T21:22:10Z")

</div>

Thanks for the additional info, I had a mess about and found the same as yourself… If I come across a workaround I’ll let you know but sadly I suspect this is going to be problematic atm based on these feature requests:

> <https://github.com/postmanlabs/postman-app-support/issues/7610>
>
> Using the Visualisations, it's possible create interactions as well while displa…ying the response data.
> It would be a great addition if these interactions would allow a developer to generate actions that execute requests from the same collection thereby also being able to fill the parameters of such a request with data from the current repsonse. This would allow for a complete interactive way of browsing through the API.
> 
> For example:
> \* A call is done to retrieve an overview of users (/api/users/)
> \* You would click on the id of a user and this would trigger the call that retrieves information about this user (/api/user/{id})
> \* Among that information is an overview of services that this user has signed up for. Clicking on the id of a service would then trigger the call with more information about that server (/api/service/{id})
> etc.
> 
> I know it's currently possible to set variables based on the repsonse of a call and use those values in subsequent calls, but this doesn't work when there's multiple values to choose from (as is the case in my example)
> 
> With this addition you would be able to set up an interactive way of browsing through the data of your whole api, all from within Postman.

> <https://github.com/postmanlabs/postman-app-support/issues/8341>
>
> \*\*Is your feature request related to a problem? Please describe.\*\*
> I want to be… able to set variables using visualizer feature.
> 
> \*\*Describe the solution you'd like\*\*
> pm object in visualizer template should expose all pm features like collectionVariables, globals, test etc.
> 
> \*\*Describe alternatives you've considered\*\*
> Maybe if pm cannot be exposed so we can pass external functions in data object?
> 
> \*\*Additional context\*\*
> Sample:
> 
> \`\`\`
> var template = \`
> \<a onclick="set('{{id}}')"\>{{id}}\</a\>
> 
> \<script\>
> function set(id) {
> pm.collectionVariables.set("id", id);
> }
> \`;
> 
> pm.visualizer.set(template, {
> response: pm.response.json()
> });
> \</script\>
> \`\`\`
> Sample code above can set some id in collection from visualizer. So i can see some data and then choose what I want.

Sorry I can’t be more help!
