Global functions via Collection level folder?

I am very sorry if this has already been covered. I have found conflicting information online and just want to know if this is an actual available feature or not.

I want to know if it’s possible to have global functions at the Collection level folder (either in the pre-request or tests section) where I can store any functions I use for nearly every test.

For example say for every request I do the following:


could I instead create a function in say the pre-request section at the Collection level:

function hello() {

and they for all my requests I could just call hello(); (I know this isn’t a very practical example) but you know what I mean.

I have also tried this via postman.setGlobalVariable but it did not seem to work either.

Simply defining a function inside the collection pre-request / tests will not make it available everywhere (but it would be nice :slight_smile: ) .

The only way I know to reuse code / define it once run it multiple times is to pack the respective function is a Postman variable and when needed to use eval to run it.

Is this what you are looking for?


Hi @vdespa - thanks for your reply. Your suggestion is what I believe my colleague and I ended up doing. I had come back to this thread to provide an update :slightly_smiling_face:

In the Collection Pre-request script section I add my function(s):

postman.setGlobalVariable("regexFunction", () => {

let regex = /<td>[0-9]{8}<.td>/g;
let regexMatch = bodyResponse.match(regex);
return match[0];

Then in my request under Tests:

pageRegex = eval(pm.globals.get("regexFunction"));

Not the greatest code, but it’s there just to provide context :slight_smile:


That’s what I do @russellg
I used to have evals() that called a function to send a Slack message if there’s a test failure.

1 Like

That is on the todo list for sure. Not trying to derail my own thread, but if you have any resources that helped you achieve that I’d like to see them.

The problem had perplexed me for a while until I found the common way mentioned above. However, it still leaves a warning icon for each eval line, which indicates “eval can be harmful” in the postman interface.
Recently, I’ve found another way and post it here:
Users can create a prototype object with the proper function you want in the pre-request script section, like this:

Object.prototype.sayHello = function(name){
    console.log(`Hello! ${name}`);

and call that function everywhere after that. It just required a defined object, like this:

let obj = {};

Or you don’t even need the declaration of the object but use some built-in objects instead, like lodash (you pretend it has the function :smile: )


It’s working on my side.


Fantastic hack! Thanks

However, you don’t seem to be able to set variables (at any scope - maybe global, but that is undesirable) inside such functions. The value is not seen outside.

Oct. 2020 and I’m dealing with the same. The reason I’m even needing this is to simplify calls like the following with a helper function. Anyone know a more elegant solution or are we still hooking with Object.prototype?

    // The order of function calls below determines the order of precendence
function replaceContext(val) {
    return pm.variables.replaceIn(pm.environment.replaceIn(pm.collectionVariables.replaceIn(val)));

It works for me. Thanks for your sharing, Russellg.

even this solution works:

utils = {
     sayHello: function (){
       console.log(Hello! ${name});

But I have found that pm.environent.set doesn’t work in this function you created if that function is in a collection script, it works if that function is in the request

I can get the variable using pm.environent.get but not set it

Can anyone help on this?

1 Like

I think this is an issue with scope. I was logging the variable and the pm.environment.get() and both would match but once the execution continued it was gone, This might be a bug. I have a variable that needs to be set in the prerequest. I am trying to get the variable to set so I can reference it in the body with {{variable}}. return and capture it in the request I am able to save it.

collection script:
function (arg){
do something
return arg;

request prerequest script:
variable = obj.function_name(arg)

Just ran into a need for this and it worked - thanks for posting this solution !

What is the overhead here using all functions in the Collection folder level? For all requests, the functions will be redefined over and over again, doesn’t it?

  • We have 100 functions written at the collection folder level.
  • We have 1000 requests

Every time when a request is called, the 100 functions will be redefined.

What do you think about it?

1 Like

Feb 2022 - yeah works well, but noticed i am not getting the eval warnings anymore…thoughts?

You can do the following at the collection level and it is available under the pm command structure globally without any special declarations.

Collection Level Pre-Request

Object.prototype.log = (msg) => { + " | " + msg);

User in a Test Script for a Request called “Get User Information”

pm.log("thiis is my log message");


"Get User Information | thiis is my log message"

However all variables are local so if you need something, you need to pass it or use the get/set variable commands to retrieve the values.

Collection Level Pre-Request

Object.prototype.stop = (postman) => {

Test Script on a Request or Folder.


If this solves the problem neatly for you please mark is as such. Tested on v9. Glad I can help.


Oh my GOOD!
Finally! Thanks a lot!!

This is a very good suggestion:

So, part of this works as advertised and I’m able to define a prototype on collection or folder level under test scripts

Object.prototype.setGlobalVariable = (value) => {
    pm.globals.set('globalVariable', value);
    console.log(pm.globals.get('globalVariable')); // console returns the actual value when this function runs on collection level.

…and call it on request level


…however, if, on the next line, I run:


… console shows ‘undefined’ when trying to get the global variable value on request level.

So, currently, I’ve found no way to set a global/environment/collection variable from inside a function and then get the value back outside the function… very weird.

To carry on with the strategy to use Object.prototype.myFunction = function(){};

I was having the same issue with not able to write any collection variables within this defined function. I discovered if I pass in the ‘pm’ instance, it appears to work!

In Collection Pre-request Script:

Object.prototype.myGlobalFunction = function(myPm){
     myPm.collectionVariables.set("myKey", "my value");

In Request Pre-request Script:


Check the collection variables, and you now have a populated variable from the function prototype.


Just note that settimeout will not work inside this function. They messed up something with scoping… I am using settimeout for retry logic (try 3 times, 1000ms, 2000ms, 3000ms then fail)