0
0
Postmantesting~5 mins

Global variables in Postman

Choose your learning style9 modes available
Introduction

Global variables let you save values that you can use in many tests or requests. This helps avoid repeating the same data over and over.

You want to store a user token to use in many API requests.
You need to save a base URL that is used in all your requests.
You want to share data between different test scripts in Postman.
You want to keep a value that changes during testing and use it later.
You want to avoid hardcoding values in multiple places.
Syntax
Postman
// Set a global variable
pm.globals.set('variableName', 'value');

// Get a global variable
let value = pm.globals.get('variableName');

// Remove a global variable
pm.globals.unset('variableName');

// Clear all global variables
pm.globals.clear();

Use pm.globals.set to save a value globally.

Use pm.globals.get to read the saved value.

Examples
This saves 'abc123' as 'authToken' globally and then reads it back to print.
Postman
pm.globals.set('authToken', 'abc123');
let token = pm.globals.get('authToken');
console.log(token);
This removes the 'authToken' global variable. Getting it after returns undefined.
Postman
pm.globals.unset('authToken');
let token = pm.globals.get('authToken');
console.log(token);
This clears all global variables and prints an empty object.
Postman
pm.globals.clear();
console.log(pm.globals.toObject());
Sample Program

This script saves a user ID globally and then checks if it was saved correctly in a test.

Postman
// Set a global variable
pm.globals.set('userId', 'user_001');

// Use the global variable in a test
let userId = pm.globals.get('userId');
pm.test('User ID is set correctly', function () {
    pm.expect(userId).to.eql('user_001');
});
OutputSuccess
Important Notes

Global variables are shared across all collections and requests in Postman.

Be careful not to store sensitive data in global variables if sharing your workspace.

Use environment variables if you want variables specific to a project or environment.

Summary

Global variables store data accessible everywhere in Postman.

Use pm.globals.set and pm.globals.get to manage them.

They help avoid repeating values and share data between tests.