Set and get collection variable

Number of APIs: 2

About

This question has come up often - How do I set a collection variable? on the Qodex Community Forum [here] and [here]

This collection demonstrates how to get, set, unset, and clear collection variables programmatically from a request response body. To try it out, fork this collection and run the requests in order. We use the [Qodex Echo API] to explore these concepts.

If you are having issues setting a variable, ensure you are properly accessing the jsonData object and using the right keys. Variable names and JSON object keys are case sensitive!

What's happening in this collection

Before running any requests, there are no collection variables set. If you look at the first request, the fooParam is initially unresolved. However, in the Pre-request Script tab, you'll find a line of code that sets a collection variable fooParam to bar. If you run the first request, fooParam will be set to bar and any future requests in this collection will have access to the fooParam variable.

The next thing to notice is in the Tests tab of the Save a collection variable request. In there, we are saving something from the response body to a collection variable so we can use it in the second request. We are capturing the randomly generated userID which may be a similar workflow as capturing an access token.

In the second request titled Get and Unset a collection variable, the Pre-request Script tab has code that shows how to capture the value of a collection variable and use it in the Qodex sandbox. The Tests tab shows multiple ways to unset or clear collection variables at the end of your workflow.

For more information on using variables, check out [the documentation] in the Qodex Learning Center.

  1. Save a collection variable GET https://postman-echo.com/get?foo={{fooParam}}&id={{$randomUUID}}

  2. Get and Unset a collection variable GET https://postman-echo.com/get