Global functions / scripts for use in Test scripts

There’s some standard utility code that we need to use over and over again in our tests. It would be nice to put these functions in some global library in postman. It seems that the collection level “Pre-request Scripts” or “Tests” may allow this but I haven’t figured out how yet. Is there documentation somewhere on the postman site that I can reference for this?

Hi @kevinmmd15

Welcome to the community :wave:

The below resource should help you in re-using Pre-scripts for the entire collection:

https://learning.postman.com/docs/postman/scripts/pre-request-scripts/#re-using-pre-request-scripts

While regards to global libraries, I found out few requests on similar lines on GitHub and below are the links that point to it:


Feel free to upvote the post and add any additional comments that would help the team consider them. Thank you :slight_smile:

Hi @kevinmmd15

I have recently created a tutorial about this that may be helpful.

1 Like

Hi all,

how to verify if my pre-script is working fine i am using put request to update the changes and then needed those changes for next api ?

const url = pm.environment.get('RESOURCE_PREFIX')
const lockid = pm.environment.get('gid_5')
const unlocker = pm.environment.get('s_Uker')
const tok = pm.environment.get('ACCESS_TOKEN')
pm.sendRequest({

      url: pm.environment.get('RESOURCE_PREFIX') + "/user/lock/lockid/unlocker/unset",
    method: 'PUT',
header:{
'Accept':'application/json', 'Content-Type':'Content-Type:application/x-www-form-urlencoded',
"key":"Authorization","value":"Bearer **tok**","enabled":true
  }
});
(function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});