# Send request after x amount of time passed, otherwise skip

**URL:** <https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405>\
**Category:** Help Hub\
**Tags:** newman-project, collection, keycloak\
**Created:** [February 7, 2024, 9:09am UTC](https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405 "2024-02-07T09:09:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviation-observer-43](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/aviation-observer-43/32/9862_2.png) [@aviation-observer-43](https://community.postman.com/u/aviation-observer-43)\
**Post date:** [February 7, 2024, 9:09am UTC](https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405/1 "2024-02-07T09:09:54Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

Hi, all.  
Is there a way, using Postman and ideally run the that collection in newman later, to repeat a collection that has three requests multiple times but send the first request every, say, 20 minutes?

We plan to use newman as a helper tool to perform load/stress tests on our system. Since newman doesn’t handle Oauth 2.0 authentication set on Authentication tab in the Postman collection, I need to make a request to fetch bearer token from Keycloak.

During our stress tests, I don’t want to simply loop over the collection because that would send a request to our KC server with every loop. Is there a way to write a contidtion in pre-request or test tab that will fire the KC request on the first loop and then every X minutes or X loops, otherwise it should skip the KC request and just send the requests that we use to test our system…

---

<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 7, 2024, 11:04am UTC](https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405/2 "2024-02-07T11:04:45Z")

</div>

Store the expiry date of the token, and instead of using the authentication helper, you can have a pre-request script that mimics the functionality.

The following is an example of authenticating to Microsoft.

```auto
let currentDateTime = Date.now();
let tokenExpiry = pm.environment.get("bearerTokenExpiresOn")
// console.log("currentDateTime: " + currentDateTime);
// console.log("tokenExpiry: " + tokenExpiry);
if (!pm.environment.get("bearerToken") || currentDateTime > tokenExpiry) {
    pm.test("Pre-request check for Environment Variables", function () {
        let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
        vars.forEach(function (item) {
            // console.log(item);
            pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.undefined;
            pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.empty;
        });
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.environment.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.environment.get("clientId"), disabled: false },
                    { key: "scope", value: pm.environment.get("scope"), disabled: false },
                    { key: "username", value: pm.environment.get("username"), disabled: false },
                    { key: "password", value: pm.environment.get("password"), disabled: false },
                    { key: "client_secret", value: pm.environment.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                pm.test("Pre-request Microsoft login Status code is 200", () => {
                    pm.expect(res).to.have.status(200);
                    let resJson = res.json();
                    // console.log(resJson);
                    pm.environment.set("bearerToken", resJson.id_token);
                    pm.environment.set("bearerTokenExpiresOn", Date.now() + resJson.expires_in * 1000);
                    // console.log("bearerTokenExpiresOn: " + pm.environment.get("bearerTokenExpiresOn"));
                });
            }
        });
    });
};

```

However, please note, this won’t persist through collection runs. You will have to authenticate at least once per collection run.

---

<div class="post-metadata">

**Author:** ![aviation-observer-43](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/aviation-observer-43/32/9862_2.png) [@aviation-observer-43](https://community.postman.com/u/aviation-observer-43)\
**Post date:** [February 8, 2024, 11:19am UTC](https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405/3 "2024-02-08T11:19:47Z")

</div>

Thanks, man. This works!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 12, 2024, 10:09am UTC](https://community.postman.com/t/send-request-after-x-amount-of-time-passed-otherwise-skip/57405/4 "2024-02-12T10:09:12Z")

</div>

Closed on February 12, 2024, 10:09am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
