# Conditional API call based on response from an earlier API call - Collection runner

**URL:** <https://community.postman.com/t/conditional-api-call-based-on-response-from-an-earlier-api-call-collection-runner/38306>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [August 18, 2022, 6:23pm UTC](https://community.postman.com/t/conditional-api-call-based-on-response-from-an-earlier-api-call-collection-runner/38306 "2022-08-18T18:23:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ontherocks](https://avatars.discourse-cdn.com/v4/letter/o/a183cd/32.png) [@ontherocks](https://community.postman.com/u/ontherocks)\
**Post date:** [August 18, 2022, 6:23pm UTC](https://community.postman.com/t/conditional-api-call-based-on-response-from-an-earlier-api-call-collection-runner/38306/1 "2022-08-18T18:23:23Z")

</div>

I have three APIs. API#1, API#2, API#3  
API#1 returns names e.g. `{{endPoint}}/GetNames`  
API#2 returns phone number(s) for a particular name e.g. `{{endPoint}}/GetPhoneNo/{{name}}`  
API#3 returns carrier names for a particular phone number e.g. `{{endPoint}}/GetCarrier/{{phoneNo}}`  
I get an array of names from API#1 response body. I store them in an environment variable `nameArray` in the _Tests_ script of API#1

**Tests of API#1**

```auto
pm.variables.set("nameArray", pm.response.json().names);
pm.variables.set("phoneNoArray", []);

```

I use the `nameArray` in Pre-request Script of API#2 to call API#2 on a per name basis. Then in the Tests Script of API#2, I populate `phoneNoArray` from the response body of API#2.

**Pre-request Script of API#2**

```auto
if(nameArray.length){
    pm.variables.set("name", nameArray.shift());
    postman.setNextRequest(pm.info.requestName);
}

```

**Tests of API#2**

```auto
pm.variables.get("phoneNoArray").push(pm.response.json().phoneNo);

```

Now, there could be a scenario where, none of the names have a phone number i.e. `phoneNoArray` is empty. In this case I don’t want to call API#3 as it would be an invalid call. So I have the following

**Pre-request Script of API#3**

```auto
if(phoneNoArray.length){
    pm.variables.set("phoneNo", phoneNoArray.shift());  
    postman.setNextRequest(pm.info.requestName);
}
else{
    //If there are no phone numbers, do not call API#3
    postman.setNextRequest();
}

```

This obviously doesn’t work because it would make one call to API#3 as we can’t skip call to API#3 when the checking condition is itself in Pre-request Script of API#3. The check needs to be in API#2. I am not sure how do I put the check in API#2 as it runs in a loop.

---

<div class="post-metadata">

**Author:** ![francisadvlighting](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@francisadvlighting](https://community.postman.com/u/francisadvlighting)\
**Post date:** [February 20, 2024, 3:53am UTC](https://community.postman.com/t/conditional-api-call-based-on-response-from-an-earlier-api-call-collection-runner/38306/2 "2024-02-20T03:53:36Z")

</div>

If you do get to the scenario when none of the names have a phone number, you can actually call something like

> postman.setNextRequest(null);

which should terminate the program. According to this documentation [Conditional Workflows in Postman | Postman Blog](https://blog.postman.com/conditional-workflows-in-postman/)  
You may want to call that in your API#2

---

<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:** [February 20, 2024, 12:25pm UTC](https://community.postman.com/t/conditional-api-call-based-on-response-from-an-earlier-api-call-collection-runner/38306/3 "2024-02-20T12:25:06Z")

</div>

@francisadvlighting

No you can’t as setNextRequest() just sets the next request to run after current request and all of the code in the pre-request script and tests tab have completed.

So as the original poster stated, it will still run at least once.

However, things have moved on since the original query, and Postman now has a skip request feature.

[The New Skip Request Feature - 📰 Postman News - Postman Community](https://community.postman.com/t/the-new-skip-request-feature/54806)
