# Cancel call to get a new token if the current one hasn't expired

**URL:** <https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543>\
**Category:** Help Hub\
**Tags:** authentication\
**Created:** [May 5, 2025, 4:42pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543 "2025-05-05T16:42:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![G-Laser](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@G-Laser](https://community.postman.com/u/G-Laser)\
**Post date:** [May 5, 2025, 4:42pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/1 "2025-05-05T16:42:53Z")

</div>

I have a call that grabs a token from one of my servers. A token has a lifespan of 15 minutes. I have code in the Pre-Request that checks to see if 15 minutes has passed. If it has, it will grab a new token. If not, I use the pm.execution.setNetxRequest(null); but no matter what, it still executes the call.  
Is it possible to halt a request from being made within its own Pre-request?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 5, 2025, 4:57pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/2 "2025-05-05T16:57:45Z")

</div>

Hey @G-Laser 👋🏼

What’s the full script you have, it will make it a lot easier to provide additional help here.

Are you using this as a standalone request or running it with the Collection Runner?

---

<div class="post-metadata">

**Author:** ![G-Laser](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@G-Laser](https://community.postman.com/u/G-Laser)\
**Post date:** [May 5, 2025, 5:28pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/3 "2025-05-05T17:28:59Z")

</div>

Right now, it’s just a stand-alone request. My other tests that use it inherit the token from what that call returns.  
**Here is that calls Pre-request.**

```auto
require("btoa");
const tokenTimestamp = pm.environment.get('token_timestamp');
const timeDifference = Date.now() - tokenTimestamp;
const minutes = Math.floor(timeDifference / (1000 * 60));
console.log("Number of minutes: " + minutes);
if(tokenTimestamp == null || minutes > 15) {
    pm.environment.set("base64_encoded_client", btoa(pm.environment.get("username") + ":" + pm.environment.get("password")));
} else {
    pm.execution.setNextRequest(null);
    console.log("Call cancelled");
}

```

In the Post-response, it just sets the tokens and the current timestamp since I expect it to only run if pm.execution.setNextRequest(null) is not executed.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 5, 2025, 6:15pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/4 "2025-05-05T18:15:26Z")

</div>

The `setNextRequest` function can only be used with the Collection Runner, it won’t do anything if it’s used in a standalone request.

 ![1000015942](https://us1.discourse-cdn.com/getpostman/original/3X/2/6/2660676c222e62036c526e83a52352f30c1d8e79.png)

---

<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:** [May 6, 2025, 2:39pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/5 "2025-05-06T14:39:25Z")

</div>

pm.setNextRequest() sets the next request that will run after the current request has completed. (Including any pre or post request code).

It will always run the current request.

Sounds like you want the pm.execution.skipRequest() function.

[https://blog.postman.com/optimize-your-api-testing-workflow-with-postmans-skip-request-feature](https://blog.postman.com/optimize-your-api-testing-workflow-with-postmans-skip-request-feature)

I have a few API’s where I have similar code. I normally put the code in the pre-request script at the folder or collection level, so it checks the token expiry for each request in that collection or folder. I don’t need to skip or change the execution flow that way. It will either get a new token or it won’t and won’t be reliant on the Collection Runner to work or test.

---

<div class="post-metadata">

**Author:** ![G-Laser](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@G-Laser](https://community.postman.com/u/G-Laser)\
**Post date:** [May 7, 2025, 3:29pm UTC](https://community.postman.com/t/cancel-call-to-get-a-new-token-if-the-current-one-hasnt-expired/79543/6 "2025-05-07T15:29:52Z")

</div>

Yep! That’s EXACTLY what I needed. THANK YOU!!!
