0
0
Postmantesting~5 mins

Local variables in Postman

Choose your learning style9 modes available
Introduction

Local variables store data temporarily during a single request in Postman. They help keep information handy without affecting other requests.

When you want to save a value to use later in the same request.
When you need to pass data between scripts within one request.
When you want to avoid changing global or environment variables accidentally.
When testing APIs that require temporary tokens or session IDs.
When you want to keep your tests clean and isolated.
Syntax
Postman
pm.variables.set("variableName", "value");
let value = pm.variables.get("variableName");

Local variables exist only during the request execution.

Use pm.variables.set to create or update, and pm.variables.get to read.

Examples
Sets a local variable userId and then reads it back to print.
Postman
pm.variables.set("userId", "12345");
let id = pm.variables.get("userId");
console.log(id);
Saves a token from the response JSON into a local variable for use in the same request.
Postman
pm.variables.set("token", pm.response.json().token);
let token = pm.variables.get("token");
Sample Program

This test saves a local variable and checks if it holds the correct value.

Postman
pm.test("Save and use local variable", function () {
    pm.variables.set("tempValue", "hello");
    let val = pm.variables.get("tempValue");
    pm.expect(val).to.eql("hello");
});
OutputSuccess
Important Notes

Local variables do not persist after the request finishes.

They have higher priority than environment and global variables during the request.

Use local variables to avoid side effects on other requests or tests.

Summary

Local variables store data only during one request.

Use pm.variables.set and pm.variables.get to work with them.

They help keep tests clean and isolated.