# How to user array variable in chain request

**URL:** <https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596>\
**Category:** Help Hub\
**Tags:** variables, array, chain\
**Created:** [March 18, 2019, 9:49am UTC](https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596 "2019-03-18T09:49:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@roy](https://community.postman.com/u/roy)\
**Post date:** [March 18, 2019, 9:49am UTC](https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596/1 "2019-03-18T09:49:03Z")

</div>

Hi,  
I’m trying to run 2 API calls, the first will give me an array of ID’s and I want to use that array in the 2nd request.

For example, here is first response:

“status”: 1,  
“offers”: [  
{  
“id”: 9792,  
“offer\_id”: “5967372b0624dfa73e8b45a7”,  
“advertiser”: “59672fcc0624dfab3e8b4995”,  
}  
{  
“id”: 9331,  
“offer\_id”: “596fdf32b0624dfa73e8b45a7”,  
“advertiser”: “596fsdfq1121412e8b4995”,  
}  
{  
“id”: 9112,  
“offer\_id”: “5535410624dfa73e8b45a7”,  
“advertiser”: “r12341cc0624dfab3e8b4995”,  
}

I want to save all the ID’s of the offers in an array and use it to run the 2nd API call for each ID in the array  
2nd api call: [some-domain.com/offer/](http://some-domain.com/offer/)\<\>?api-key=1112222

Would appreciate your assistance.

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [March 20, 2019, 9:26am UTC](https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596/2 "2019-03-20T09:26:09Z")

</div>

I’ve never done this before, so off the top of my head here’s how I quickly performed the action you wanted:

In the example I’m calling one GET request that just returns user details.  
The example URL is:

```auto
http://localhost:3000/users/{{userId}}

```

And here’s an example response:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/3/f35c5458daa8d930e4c9c88ed079034655473b22.png)

Before we begin, I have 3 environment variables:  
“userIds” which has an array of User IDs.  
[1, 2, 3, 4, 5, 6]

“counter” which will be a number. This will be set to 0 in a previous request.

Finally, we have a “userId” which is what’s used in the URL. We’ll have some logic in place to pull the correct userId from our “userIds” array.

**Down To The Nitty Gritty!!:**

In the Pre-Request Script tab I’m taking the current value of our “counter” environment variable.

I’m also performing a JSON Parse on the “userIds” array as Postman saves as env variables as strings.

So! Our “userId” variable will be set to the User ID in our “userIds” array based on the current value of “counter” (I hope that made sense…)

There are lots of comments in the code snippet shown.

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

**In our Tests Tab:**

Again, I’m performing a JSON Parse on our “userIds” array.  
And I’m taking the current value of our counter variable again.  
I’m increasing the value of “counter” by 1 with each request.  
I have a condition, that checks if “counter” is NOT equal to the length of the array (minus 1)  
If it’s not, I’m using postman.setNextRequest() to hit the same request again (But remember, our counter variable is now greater than before)

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

You can probably tell this is pretty rushed as I’m at my place of work xD

Good luck! Hope this makes sense.

I’m sure there are MUCH better/cleaner ways of doing this.

---

<div class="post-metadata">

**Author:** ![roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@roy](https://community.postman.com/u/roy)\
**Post date:** [March 24, 2019, 12:23pm UTC](https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596/3 "2019-03-24T12:23:05Z")

</div>

Thanks a lot Pfarrell, very helpful !

---

<div class="post-metadata">

**Author:** ![rohittaneja](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rohittaneja](https://community.postman.com/u/rohittaneja)\
**Post date:** [July 26, 2020, 9:49am UTC](https://community.postman.com/t/how-to-user-array-variable-in-chain-request/4596/4 "2020-07-26T09:49:40Z")

</div>

What if the ids are hashes instead.
