# Using Variables in Packages

**URL:** <https://community.postman.com/t/using-variables-in-packages/60678>\
**Category:** Help Hub\
**Tags:** scripts, package-library\
**Created:** [May 6, 2024, 2:22am UTC](https://community.postman.com/t/using-variables-in-packages/60678 "2024-05-06T02:22:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [May 6, 2024, 2:22am UTC](https://community.postman.com/t/using-variables-in-packages/60678/1 "2024-05-06T02:22:04Z")

</div>

- **Using Variables in Package Library** :
- **How does one access variables in the package library?** : I have a JSON schema checker that I need to use in every API endpoint that I use. To do this in the post request scripts I create the local variable which I use to test the schema against.

I would like to move this code into the package library so that all this code is in the one place (as this feature is designed for). Since each API has a different schema to do this, I need to be able to pass this variable into the Package.

I have tried setting and getting the variable via pm.collectionVariables.set/get and pm.global.set/get.

Setting this variable in either a pre or post script returns the error “ReferenceError: schema is not defined”.

Is there a way to do this? I was hoping there would be the ability to add a parameter to the package call, or to pass in a collection variable, but I can’t see one.

Thanks

- **Platform Details** :  
Version  
11.0.6  
UI version  
11.0.6-ui-240502-1530  
Desktop platform version  
11.0.4  
Architecture  
x64  
OS platform  
win32 10.0.22631

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 6, 2024, 11:45am UTC](https://community.postman.com/t/using-variables-in-packages/60678/2 "2024-05-06T11:45:57Z")

</div>

Hi @birddog-davek. Welcome to the Postman Community!

Since Packages can contain JavaScript functions, you can pass variables into them as you would in a regular functions in javascript. To simulate your scenario, take a look at the screenshots below.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/d/bd389f8d726b0e0b839b03e44acb400800164946.png)

Here, I have a package that exports two functions. One validates my API status code, and the other validates my response JSON against a given schema. Both of these functions take in external variables.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/0/d0c21a62b337f3cfbd14be18f0b363dbb224812e.png)

Here, you can see the schema being passed into the validator to check against the provided response.

---

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [May 7, 2024, 12:48am UTC](https://community.postman.com/t/using-variables-in-packages/60678/3 "2024-05-07T00:48:24Z")

</div>

Thanks for that super concise answer, and this is exactly what I was hoping for. I hadn’t thought about having functions inside of the packages, I was thinking about the package itself as a function.

Cheers for the screenshots too, these really help me, and hopefully others, to see how to implement this.

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 8:33am UTC](https://community.postman.com/t/using-variables-in-packages/60678/4 "2024-05-07T08:33:18Z")

</div>

I’m glad you found it helpful @birddog-davek. Cheers!!

---

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [May 8, 2024, 12:19am UTC](https://community.postman.com/t/using-variables-in-packages/60678/5 "2024-05-08T00:19:20Z")

</div>

@gbadebo-bello I’ve got one more question related to your screenshots.

In them you use the variable “code” that is passed in as an argument to the function as part of the title of the test. When I try to do this all I get back is the ${code} string, not the 200 that is passed in.

Do you know why?

![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/4/b4deae5efecb15bec26bb3a0f1be238e21a1d2c2.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/b/4bafa39b7eaafe4b3fa696bdb11ec38dc1ee1de4.png)

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 8, 2024, 9:27am UTC](https://community.postman.com/t/using-variables-in-packages/60678/6 "2024-05-08T09:27:45Z")

</div>

Hi @birddog-davek. In your screenshot, you’re making use of template literals. Template literals work only with backticks(`), you’re using an apostrophe/single quote instead.

When you use a single quote in JavaScript, it effectively becomes a string. You need to modify line 2 in your screenshot to

`return pm.test(`Returns response status code: ${code}`, () => {`

---

<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:** [May 11, 2024, 9:28am UTC](https://community.postman.com/t/using-variables-in-packages/60678/7 "2024-05-11T09:28:36Z")

</div>

Closed on May 11, 2024, 9:28am UTC

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