How to create a variable automatically increment by 1 after every request in postman

Hi everyone, I have a json pattern as bellow:

                "timestamp": 1676947910,
                "message": {
                    "mid": "{{$randomBitcoin}}",
                    "text": "0"

I want the text field auto increment by 1 after every request.

You’ll need to store the value in variable. (Collection or Environment).

You then update your request to use the variable value. {{variableName}}, similar to how you’ve got the random variable set.

In the Tests tab for the request, you will then retrieve the variable, increment it by one and then resave it.

let myVar = pm.collectionVariables.get("variableName");
myVar ++ // increment by 1
pm.collectionVariables.set("variableName", myVar);

Using variables | Postman Learning Center

Where in Postman I can run this code so that it will run before or after request?


The pre-request script tab runs before every request, and the tests tab runs after each request.

If you are new to Postman I would recommend the Postman training links which are located under “other resources” in the Learning Centre.

Other resources | Postman Learning Center

The “Galaxy API’s 101” course gets you used to sending requests and the GUI.

The “Galaxy Testing and Automation” gets you used to testing your responses and using variables, basic scripting, etc.

Also take a look at the following.

Postman JavaScript reference | using-environment-variables-in-scripts

Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.

Thank you I found it.

Hi @michaelderekjones

I have a follow up question to this original question.
I have been trying to get it work without success for quite some time.

Can I do as you describe, but get it to run from the cloud - ie a scheduled run or a monitor?
I can get it to work fine when I run manually. (From the web-app or desktop app).

But from experience and from the quote below I think perhaps this is not possible to get the incrementing of a variable to work when running from the cloud.

" When you set a variable value using pm.variables.set , the value is local and will only persist for the current request or collection run."

Please can you confirm if I am correct?
Or if I can do it from scheduled runs, please explain.


This should be its own question.

I think you’ve sort of answered your own question.

pm.variables is local and will only persist for the current request.

Therefore, use as different variable type. Collection or environment.

I don’t use scheduled runs or monitors, but I’m assuming they just run an existing collection. Therefore a collection variable sounds like the way to go.

Many Thanks Mike,

I will make it its own question then.
I have tried collection variables and environment variables.
Nothing works and nothing useful from Postman support either (yet).



When I say “new question”, I mean a brand new post. Rather than tagged into the end of a solved topic.

As mentioned, I don’t use scheduled runs or monitors, but as far as I’m aware, collection variables should work as both methods “execute” collections.

I can only recommend similar troubleshooting steps to if you were working with the collection directly is to console log the variables to ensure they are being set correctly.

That is a new post is it not??

I pasted a link to it here for convenience.