Accessing Collection Variables in Postman

Collection variables

You can access collection variables (and all variables) in the pre-request and test script sections using pm.variables.get("variableName").

However, you can only define and update collection variables by editing the collection details via modal.

Note: For your current solution using environment variables getting messy, remember you can always use pm.environment.set() to reset the value or pm.environment.unset() to clear it.


Setting collection variables manually and then getting them was always possible.

Setting collection variables in scripts and not just manually became possible in version 7.9.0 which was released in October 2019. As of writing this, there is still obsolete misinformation about it out there - on the internet in general - but sadly also here at Stack Overflow.

Although this question has already been correctly answered, I am adding this answer in an attempt to clear up any remaining confusion.

A collection variable can be set in a script

To find out who is right and who is wrong, I made a simple little experiment.
Below I describe what I did. You can replicate the exact same experiment yourself.

I have created a Postman collection named ManipCollVars.
(ManipulateCollectionVariables seemed a little too long.)
You can download and save it to your local drive from:
https://user.it.uu.se/%7Ehesc0353/postman/ManipCollVars.pm_coll.json.

Then - from your Postman desktop app (not the chrome extension) - import ManipCollVars (Collections > Import > File > Upload Files) as shown in the figure below.
(The dummy GET request is https://postman-echo.com/get.)

Import the collection 'ManipCollVars'.

^ click to enlarge

The initial value of the collection variable CollectionVar

In the left pane, on the same line as ManipCollVars is displayed, click on the ellipsis to the right (the three mini circles: •••), and then Edit. See the figure below.

Edit the collection properties of 'ManipCollVars'.

Next click on the Variables tab. Note that the CURRENT VALUE of CollectionVar is "Initial Value".

The CURRENT VALUE of CollectionVar equals 'Initial Value'.

Run the dummy request and the Tests script

Click on the request ManipCollVars-Request, and then on its Tests tab as shown in the figure below.
Focus on lines 7-11:

// Will now try to change `CollectionVar` to some new value:
pm.collectionVariables.set('CollectionVar', 'Some New Value');
// Then print the new value of `CollectionVar` to the console:
console.log(pm.collectionVariables.get('CollectionVar'));
// ^ Does CollectionVar contain "Initial Value" or "Some New Value"? ^

Click on the blue Send button, and then open the Console in the bottom left corner as in the figure below. Note how the value of the collection variable has changed from "Initial Value" to "Some New Value". - Issue settled!

The value of CollectionVar is now 'Some New Value'.

Double-checking the value of the collection variable

To double-check that the value has indeed changed, click once again on the ellipsis (•••) next ManipCollVars > Edit > Variables. Note that the CURRENT VALUE of CollectionVar is now "Some New Value". - Confirmed!

The value of CollectionVar has changed to 'Some New Value'.

^ click to enlarge

References

  • Correct answer to this question
  • Defining variables in scripts
  • Postman Quick Reference Guide: Collection variables
  • Using collection variables in scripts
  • Answer to "Using Collection Variables in Postman"
  • Postman: The Easy Way to Work with APIs

Postman v7.9.0 added support for new pm.collectionVariables, so you can update them on test scripts:

pm.collectionVariables.set("collection_variable", newValue);

https://github.com/postmanlabs/postman-app-support/issues/5053#issuecomment-542555156

Tags:

Postman