Creating Global Variables in Postman

Creating Global Variables in Postman

Let’s imagine a situation where we have a GET request that serves to Get an order. Its requirement is to be updated with the orderId of the item that has to be ordered. In order to get that orderId, we have to run a POST request before this GET request. When the POST request generates the orderId, we have to add it as a parameter in to the GET request. We can do that manually, but since the orderId value changes, we should manually change that for each test (or at least after some time when it expires). While running automated tests, this could take us time, and we would get failed test results.

The solution for this case is to use Global Variables.

Here is the POST request response result:

{
     "created": true,
     "orderId": "1jrdcbkc2136910"
}

Now, when this response is generated, the orderId value should be taken and automatically updated as a global variable that could be used in the next upcoming test requests. We can automate that by uploading a piece of test script to this POST request:

const response = pm.response.json();
pm.globals.set("orderId", response.orderId);

We can check if this Global variable has been created by going to the Environment section by clicking on the Variables in this request icon (or clicking on the eye icon in earlier versions of Postman) in the top right corner inside of Postman inside the pop-up window, we should see the orderId updated in the variable section and its most recent value in the current value section.

Next, we have to update the GET request with an orderId parameter key, and for its value we have to set a variable placeholder that contains the global variable key name, just like this: {{orderId}}

This way, the GET request should be automatically updated with the previously generated orderId so it could successfully run the test and provide passed results.

Scroll to Top