# How to grab variable from one API and add it to the endpoint of another api so it is called many times?

**URL:** <https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446>\
**Category:** Help Hub\
**Created:** [October 2, 2020, 9:58pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446 "2020-10-02T21:58:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 2, 2020, 9:58pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/1 "2020-10-02T21:58:45Z")

</div>

I have two issues that i need help with …

1. In API 1 - i want to loop the response body of GET API and find all the “header” that contain “Delete”

When the script finds something that contains “Delete”, grab the announcementid and set it in an array …

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

the code below,  
is finding the ones that don’t say “Delete”, but the array that should show the announcement ids that have delete in them are not showing i need this to show …

var jsonData = pm.response.json();

var resultCount = jsonData.length;

// Test arrays

var hasDelete = ;

var doesntHaveDelete = ;

// Loop through and set arrays with matching data

for (i = 0; i \< resultCount; i++) {

var id = jsonData[i].announcementId;

var modelString = jsonData[i].header.toLowerCase();

if (modelString.includes(“Delete”)) {

```
hasDelete.push({

  "announcementId": id,

  "hasDelete": modelString.includes("Delete")

});

```

} else {

```
doesntHaveDelete.push({

  "announcementId": id

});

```

}

}

// Check that each object in response contained keyword and length matches from test

pm.test(“Expect response to contain Delete”, function() {

console.log(hasDelete);

console.log(doesntHaveDelete);

pm.expect(hasDelete.length).to.equal(resultCount);

});

2nd issue:  
Now i want to add that array of AnnouncementIDs and add it to API 2’ endpoint:

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

and i want to do this as many times as there is a number within the Announcement id array.

Please help …

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 5, 2020, 11:47am UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/2 "2020-10-05T11:47:58Z")

</div>

Easy fix for your first one. You to `toLowerCase()` on the header, but the string you’re trying to match has an upper case: `Delete`.

```auto
var jsonData = pm.response.json();
var resultCount = jsonData.length;

// Test arrays
var hasDelete = [];
var doesntHaveDelete = [];

// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
  var id = jsonData[i].announcementId;
  var modelString = jsonData[i].header.toLowerCase();
  if (modelString.includes('delete')) {
    hasDelete.push( id );
  } else {
    doesntHaveDelete.push( id );
  }
}

// Check that each object in response contained keyword and length matches from test
pm.test(“Expect response to contain Delete”, function() {
  console.log(hasDelete); 
  console.log(doesntHaveDelete);
  pm.expect(hasDelete.length).to.equal(resultCount);
});

pm.collectionVariables.set('deleteIds', JSON.stringify(hasDelete));

```

i don’t expect your assertion to be true 100% of the time. You’re testing to make sure that every result that comes back in the response has the word delete in the header.

For Part 2:  
You want to iterate over each of the hasDelete ids from your first request. So in your pre-request script add the following snippet:

```auto
let deleteIds = pm.collectionVariables.get('deleteIds');
if(deleteIds) {
  deleteIds = JSON.parse(deleteIds);
}
else {
  deleteIds = [];
}

let accouncementId = deleteIds.pop();
pm.collectionVariables.set('announcementId', announcementId);
pm.collectionVariables.set('deleteIds', JSON.stringify(deleteIds));

```

Set the announcementId variable to your query parameter close to what you have in your screenshot (you don’t need the dollar sign).  
In your test script, you would add whatever tests you want to assert on, but you will also need to add the following script:

```auto
try {
  const deleteIds = JSON.parse(pm.collectionVariables.get('deleteIds'));
  if(deleteIds.length > 0) {
    postman.setNextRequest('120856 - GET DeleteAnnouncement'); // This is the name of the request in Postman
  }
} 
catch(err) {
  console.log(err);
}

```

The combination of the pre-request script and the test script will allow you to iterate over all the ids in the `deleteIds` variable.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 5, 2020, 3:14pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/3 "2020-10-05T15:14:24Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> try {
> const deleteIds = JSON.parse(pm.collectionVariables.get('deleteIds'));
> if(deleteIds.length > 0) {
> postman.setNextRequest('120856 - GET DeleteAnnouncement'); // This is the name of the request in Postman
> }
> } 
> catch(err) {
> console.log(err);
> }
> 
> ```

Thank you so much the first part is working perfectly,

the second part i am getting this error:

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

Also the last try and catch condition that should go in the test tab of API2 correct?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 5, 2020, 4:08pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/4 "2020-10-05T16:08:26Z")

</div>

You have a typo in line 3:

```auto
deleteIds = JSON.parse(deleteIds);

```

And yes, the try catch goes in the **tests** tab of the second request

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 5, 2020, 4:33pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/5 "2020-10-05T16:33:00Z")

</div>

Thanks, did what you said , but now i am seeing a 404 error … it seems the announcementId is not coming up as a variable …

i changed collection to environment just to show you:

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

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 5, 2020, 4:45pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/6 "2020-10-05T16:45:52Z")

</div>

You’re saving the ids as objects.

The example I provided was saving them as strings, which is why what I provided isn’t working for you.

If you wish to continue saving them as objects, that’s fine, you’ll just need to access the id like this:

```auto
pm.environment.set('announcementId', announcementId.announcementId);

```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 5, 2020, 4:57pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/7 "2020-10-05T16:57:20Z")

</div>

Ahh got ya, how would i save the id as something other than object?

I did try this its working but its only sending a request to the first announcementId  
its not looking at the whole array … (11)

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 5, 2020, 5:10pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/8 "2020-10-05T17:10:56Z")

</div>

The code snippet i provided above will save it as an integer.

Are you using the prerequest script I provided? It loads the ids, removes the first one, and uses that in the request. Then it saves the updated array.

That process repeats until there are no more.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 5, 2020, 6:53pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/9 "2020-10-05T18:53:41Z")

</div>

Hi I changed the object to int as you had described for the first API,

and followed your steps closely for the pre req and test scripts on the 2nd API:

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/8/58cc284b01ae10f12f6a743b681befcb8912a76a.png)

But i am getting this issue now:

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 5, 2020, 7:18pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/10 "2020-10-05T19:18:57Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> let deleteIds = pm.collectionVariables.get('deleteIds');
> if(deleteIds) {
> deleteIds = JSON.parse(deleteIds);
> }
> else {
> deleteIds = [];
> }
> 
> let announcementId = deleteIds.pop();
> pm.collectionVariables.set('announcementId', announcementId);
> pm.collectionVariables.set('deleteIds', JSON.stringify(deleteIds));
> 
> ```

Ah this was my fault. I made a typo in the prerequest script. I had it spelled **accouncementId** instead of **announcementId**.

The above script should make it start working.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 5, 2020, 8:11pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/11 "2020-10-05T20:11:22Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> let deleteIds = pm.collectionVariables.get('deleteIds');
> if(deleteIds) {
> deleteIds = JSON.parse(deleteIds);
> }
> else {
> deleteIds = [];
> }
> 
> let announcementId = deleteIds.pop();
> pm.collectionVariables.set('announcementId', announcementId);
> pm.collectionVariables.set('deleteIds', JSON.stringify(deleteIds));
> 
> ```

Hi did exactly that… now getting a 500 error… i know its not the security since the other api are showing 200

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

and if i change the announcementid varible to a number it works fine … its something with the announcementid var

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 6, 2020, 11:32am UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/12 "2020-10-06T11:32:50Z")

</div>

does it work if you manually type in 171 in the url instead of using the variable? The console makes them look the exact same to me.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 6, 2020, 1:15pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/13 "2020-10-06T13:15:10Z")

</div>

Yes, if i add 186 manually at the top within the url it is working fine,  
its not working if i add the variable.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 6, 2020, 1:26pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/14 "2020-10-06T13:26:20Z")

</div>

But what if you add **171** at the top? I’m asking specifically about that value.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 6, 2020, 1:29pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/15 "2020-10-06T13:29:02Z")

</div>

> [@allenheltondev](#):
>
> at t

I just executed it right this moment, if I add 170 at the top which came from the array,  
it is working fine

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

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 6, 2020, 3:23pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/16 "2020-10-06T15:23:14Z")

</div>

Hi so i tried this…

1. Added a slide through a post request
2. Got the Posted request and added it in a Array in the Get command
3. Used the delete and was able to delete one of the sectionids , but i want it to delete all the sectionids by clicking send once …  
i clicked send again and than it deleted another sectionid

how can i make it so it deletes all the sectionids within that array at one click?

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/4/c4325ddaaf5b82f435ddaa4d36a3669d2087a211.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/4/64d06fcf5227a58ebcbc4d8b4330a04ffebba791.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/7/e7bae8ffd7cf0797c18349721d003a1836fe1733.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 6, 2020, 4:26pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/17 "2020-10-06T16:26:27Z")

</div>

You can’t. The method I provided is for running through the collection runner or a monitor/newman.

The alternative would be to update the API to take in multiple values.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 6, 2020, 4:31pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/18 "2020-10-06T16:31:21Z")

</div>

Oh got ya… so we cant add a loop that specifies if that array has 7 ids - send request to all 7 ids …?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 6, 2020, 4:43pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/19 "2020-10-06T16:43:06Z")

</div>

You can, but it has to be through the collection runner.

Hitting send is going to fire the one request only.

Of course you can add code in there like `pm.sendRequest`, but I don’t think that’s going to get you what you’re after.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [October 6, 2020, 4:51pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/20 "2020-10-06T16:51:06Z")

</div>

Also you can do is save those values in variable and delete it within loop. And sorry Allen flagged ur post by mistake or use pre script

[Next page](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446.md?page=2)
