# Collection Runner - Skip API call based on condition in script

**URL:** <https://community.postman.com/t/collection-runner-skip-api-call-based-on-condition-in-script/38359>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [August 19, 2022, 6:15pm UTC](https://community.postman.com/t/collection-runner-skip-api-call-based-on-condition-in-script/38359 "2022-08-19T18:15:28Z")\
**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 19, 2022, 6:15pm UTC](https://community.postman.com/t/collection-runner-skip-api-call-based-on-condition-in-script/38359/1 "2022-08-19T18:15:28Z")

</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:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 19, 2022, 9:18pm UTC](https://community.postman.com/t/collection-runner-skip-api-call-based-on-condition-in-script/38359/2 "2022-08-19T21:18:28Z")

</div>

Hi @ontherocks

You could move your `postman.setNextRequest();` calls into the “tests” tab instead.

The pre-req runs before the call is executed and the tests tab after the call executes. This means that once API 1 is called you would set in the tests tab that the next one to run is API 2. Then when API 2 runs, the tests tab would set API 3 in the conditional statement.

The else would need `postman.setNextRequest(null);` (notice the null, this is used to end the next request sequence).

This would stop API 3 from being triggered if the array is emtpy.

---

<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 20, 2022, 2:13pm UTC](https://community.postman.com/t/collection-runner-skip-api-call-based-on-condition-in-script/38359/3 "2022-08-20T14:13:36Z")

</div>

@w4dd325 I did some further experiments with your suggestion. Now it works. Thanks 👌
