# Global functions / scripts for use in Test scripts

**URL:** <https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [May 11, 2020, 8:58pm UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558 "2020-05-11T20:58:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinmmd15](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinmmd15/32/2609_2.png) [@kevinmmd15](https://community.postman.com/u/kevinmmd15)\
**Post date:** [May 11, 2020, 8:58pm UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/1 "2020-05-11T20:58:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kurmavatar](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kurmavatar/32/3026_2.png) [@Kurmavatar](https://community.postman.com/u/Kurmavatar)\
**Post date:** [May 12, 2020, 10:36am UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/2 "2020-05-12T10:36:34Z")

</div>

Hi @kevinmmd15

Welcome to the community 👋

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](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:

> <https://github.com/postmanlabs/postman-app-support/issues/8022>
>
> \<!--
> Please read through the \[guidelines\](https://github.com/postmanlabs/postma…n-app-support#guidelines-for-reporting-issues) before creating a new issue.
> \--\>
> 
> \*\*Is your feature request related to a problem? Please describe.\*\*
> I have code that I use in every collection. I globalize this by writing a the methods in the "pre request scripts" section at the collection level and setting it to an environment variable, but then I have to copy that into each collection, which fragments my code base.
> 
> \*\*Describe the solution you'd like\*\*
> A workspace scoped pre request script/test option would allow us to put common code in one space.
> 
> \*\*Describe alternatives you've considered\*\*
> Alternatively if there was a way to reference a request from an external collection, I could place the common code in that request, and reference it at the collection level.

> <https://github.com/postmanlabs/postman-app-support/issues/8069>
>
> \<!--
> Please read through the \[guidelines\](https://github.com/postmanlabs/postma…n-app-support#guidelines-for-reporting-issues) before creating a new issue.
> \--\>
> 
> \*\*Is your feature request related to a problem? Please describe.\*\*
> If I write custom methods that I want to use in multiple collections I have to duplicate that code in each collection in each workspace.
> 
> \*\*Describe the solution you'd like\*\*
> As a developer I would like to have a request in its own collection that I can write all of my custom methods in, and set them to a global variable. I would like to call that request at the beginning of all of my collections using postman.setNextRequest({{request\_id}}) 
> 
> \*\*Describe alternatives you've considered\*\*
> Perhaps this could be accomplished somehow using the Postman API.

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

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [May 13, 2020, 5:56pm UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/3 "2020-05-13T17:56:57Z")

</div>

Hi @kevinmmd15

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

[![](https://img.youtube.com/vi/5fIlkhK24Qw/maxresdefault.jpg "Reuse tests and skip collection tests in Postman") ](https://www.youtube.com/watch?v=5fIlkhK24Qw)

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 10, 2020, 3:00pm UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/4 "2020-09-10T15:00:48Z")

</div>

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 ?

```auto
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);
});

```

---

<div class="post-metadata">

**Author:** ![Anumundada](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anumundada](https://community.postman.com/u/Anumundada)\
**Post date:** [October 28, 2020, 6:03am UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/5 "2020-10-28T06:03:09Z")

</div>

Hello,

I was intending to write a function for logging in, in the PRE-REQ section.  
When I run the pm.sendrequest as Pre- Req section it gives the same error, when I write that as a function or set it in a global/environment variable I get an error.

Function declaration in the Preq tab –

//try one  
pm.environment.set(“Login”, function Login() {

```
pm.sendRequest({

    url: pm.environment.get("url")+"/api/systems/"+pm.globals.get("ID")+"/login", 

    method: 'POST',

    header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'UserID': 'admin',

        'DomainLogin': '',

        'ClientTime': '2020-10-28T07:29:19.724+10:00',

        'MessageDetails': '{"QueryParameters":[{"Key":"Version","Value":"1004.9.2.372"},{"Key":"Device","Value":"Windows (windows-10)"},{"Key":"Browser","Value":"Chrome (86.0.4240.75)"},{"Key":"ClientTime","Value":"2020-10-28T07:29:19.723+10:00"}],"SortOrder":[],"Filters":[]}',  

        'X-XSS-Protection': '1', 

        'Password': '7878',

        'Connection': 'keep-alive'

        }

}, function (err, res) {

    

        pm.globals.set("Authorization", res.json().AuthToken);

        console.log(pm.globals.get("Authorization"));

});

```

} + ‘; Login();’);

//second try  
globalThis.Log = function(){

```
pm.sendRequest({

    url: pm.environment.get("url")+"/api/systems/"+pm.globals.get("ID")+"/login", 

    method: 'POST',

    header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'UserID': 'admin',

        'DomainLogin': '',

        'ClientTime': '2020-10-28T07:29:19.724+10:00',

        'MessageDetails': '{"QueryParameters":[{"Key":"Version","Value":"1004.9.2.372"},{"Key":"Device","Value":"Windows (windows-10)"},{"Key":"Browser","Value":"Chrome (86.0.4240.75)"},{"Key":"ClientTime","Value":"2020-10-28T07:29:19.723+10:00"}],"SortOrder":[],"Filters":[]}',  

        'X-XSS-Protection': '1', 

        'Password': '7878',

        'Connection': 'keep-alive'

        }

}, function (err, res) {

    

        pm.globals.set("Authorization", res.json().AuthToken);

        console.log(pm.globals.get("Authorization"));

});

```

}

Preq section of the API–

//First try as an environment Var

eval(pm.environment.get(“Login”));

//second try as function

Log();

ERROR::  
Error details are currently disabled.  
To enable it, please set **TraceConfiguration.DisplayErrorTraces** to **true**.  
For example by overriding your Bootstrapper’s **Configure** method and calling  
 **environment.Tracing(enabled: false, displayErrorTraces: true);**."}

---

<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:** [October 9, 2025, 10:16am UTC](https://community.postman.com/t/global-functions-scripts-for-use-in-test-scripts/12558/6 "2025-10-09T10:16:58Z")

</div>

Closed on October 9, 2025, 10:16am UTC


