# Local variable usage in subsequent script and request

**URL:** <https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [March 4, 2024, 12:14am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395 "2024-03-04T00:14:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![krurai](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@krurai](https://community.postman.com/u/krurai)\
**Post date:** [March 4, 2024, 12:14am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/1 "2024-03-04T00:14:44Z")

</div>

I’m running 4 requests as a part of 1 collection.

If I set a variable in Request1 using `pm.variables.set("name", "Postman");`  
I’m not able to access it in subsequent requests without `pm.variables.get("name")` within the same collection run  
I understand this is because it is set as a local variable  
I need to use `pm.variables.get("name")` in every request I want to use name.

But, I used the following to fetch UserId from my POST request:  
const resData = pm.response.json();  
const userId = resData.id;  
//fetch the userID and set variable to be used in next request  
pm.variables.set(“userId”, userId);

And I’m able to use this userId in all of the subsequent API request calls without the need for get (These request are part of same collection and are run at once)

I do not understand how is the behaviour varying(userId vs name) in my script

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [March 4, 2024, 9:10am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/2 "2024-03-04T09:10:10Z")

</div>

Hi @krurai, Welcome to the Postman Community!

If it works for the “userID” variable, it definitely should work for the “name” variable. Is it possible for you to share a screenshot of your script tab where this variables are being set?

---

<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:** [March 4, 2024, 10:56am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/3 "2024-03-04T10:56:59Z")

</div>

Local variables are only available to that request or collection run.

Therefore, they won’t be available to your second request unless you use the collection runner.

[Store and reuse values using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/variables/)

```auto
const resData = pm.response.json();
const userId = resData.id;
//fetch the userID and set variable to be used in next request
pm.variables.set(“userId”, userId);

```

In the above example, the const on the second line and the pm.variables.set are both local variables. With the same name, so please be wary of scoping issues.

You don’t actually need the pm.variables.set() in this scenario, as the initial ‘const userId’ will be available to your second request as long as you use the collection runner.

It’s also worth noting how you are using the variable in the second request. Local variables can only be used in the scripting tabs. (Pre-request and tests tabs).

They can’t be called in the body or URL like a collection, environment or global variable using {{variableName}};

While you can use local variables, its sometimes better to just use collection variables for storing these types of values.

---

<div class="post-metadata">

**Author:** ![krurai](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@krurai](https://community.postman.com/u/krurai)\
**Post date:** [March 4, 2024, 11:28am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/4 "2024-03-04T11:28:44Z")

</div>

Hi @gbadebo-bello,

This is my Request1

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/5/d5bea96036020f48f144a21749e680ba3375b7a1.png)  
I’m setting both userId and Appname

This is my Request2

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/2/c28f706977ad30ca30db3487ddf1d4d5509403db.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/a/9a659137b2d0cdbad9cbb20f24fb8c054255d1e0.png)

The userId is being fetched from Request1 and there is no get function

But I see an error that Appname is not defined

```auto
POST https://gorest.co.in/public/v2/users
GET https://gorest.co.in/public/v2/users/6758642
ReferenceError: Appname is not defined

```

---

<div class="post-metadata">

**Author:** ![krurai](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@krurai](https://community.postman.com/u/krurai)\
**Post date:** [March 4, 2024, 11:39am UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/5 "2024-03-04T11:39:47Z")

</div>

Hi @michaelderekjones,  
Yes, I’m using the “Run collection” option. I was trying to figure out how set, get works and came across this scenario.

I understand how ‘const userId’ will be available in my second request, but I’m curious why the other variable was not available and I needed a get function.

Could you please elaborate on using the same name

```auto

```

```auto
pm.variables.set(“userId”, userId);

```

Could you please elaborate on the potential scoping issues you mentioned with the variable name ‘userId’? I’m new to Postman and I’m still trying to understand the best practices.

---

<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:** [March 4, 2024, 12:20pm UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/6 "2024-03-04T12:20:23Z")

</div>

After some testing, it would appear that pm.variables.set is not the same as just setting it using const or let.

Variables set using pm.variables **are** available in places like the params, body and URL.

Even though when you hover over the variable, it tells you that its unresolved, but it does actually seem to work. (Logically it is unresolved, as the local variable is only set on run time).

For example…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/499328ff36fbca9c7ac3bae6a9527ffd9bdecb8b.png)

You can see from the console log, that the value set in the pre-request script is being read.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/8/7817984a89ee1c10811854654257445a75ef83cc.png)

Variable set using const or let are not available unless you then use them to set one of the valid variable types.

Now we’ve got my initial misunderstanding out of the way.

The problem you are facing is that you can’t just console log the variable in the second request by its name.

You need to use the “get” command in scripts.

```auto
console.log(pm.variables.get("Appname"));

```

[Using variables in scripts | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/variables/#using-variables-in-scripts)

Using variables in scripts is different to how they are used in the URL, query parameters, body, etc.

---

<div class="post-metadata">

**Author:** ![ddegey](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ddegey/32/27840_2.png) [@ddegey](https://community.postman.com/u/ddegey)\
**Post date:** [March 13, 2024, 8:43pm UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/7 "2024-03-13T20:43:53Z")

</div>

> [@krurai](#):
>
> Could you please elaborate on the potential scoping issues you mentioned with the variable name ‘userId’? I’m new to Postman and I’m still trying to understand the best practices.

Hello @krurai,

I’m new to Postman too (just a couple of days).

1. const and let variable are just local to the pre or test script, like in a module.

2. to use a variable in the request definition (url, param, header etc…) in the form {{userId}}, you must use one of the form :

- pm.globals.set

- pm.collectionVariables.set

- pm.environment.set

- pm.variables.set

1. as far as I know, the preceding order is used if you have the same name used for a global, a collection, an env, or a “shadow” variable. Global is masked by collection, collection is masked by env, and the last wins it all.

2. what I call (don’t know the exact terminology) “shadow” variable is : the fact that using just pm.variables.set(‘userId’, ‘user001’) will create a variable for the time of the run. It won’t appear in any “Variables” tab.

3. so far, best practice seems to be :

- don’t use globals
- use collectionVariables if you need to set it manuelly and it does not depends of an environment
- use an environement variable when needed
- prefer the last one if you only need it at run time and you set it in pre/test script

1. all variable set by programmation in pre/test script keep there type (array, object/string, number) BUT as soon as you’re editing one in a “Variables” tab, it just turn to a simple string. Object/Array will then need to be json parsed…

Hope this could help you too.

Best

Didier

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 12, 2024, 8:44pm UTC](https://community.postman.com/t/local-variable-usage-in-subsequent-script-and-request/58395/8 "2024-04-12T20:44:04Z")

</div>

Closed on April 12, 2024, 8:44pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
