Store objects and arrays in variables, and read them back
Saving one id from a response is easy: pm.environment.set('userId', body.id). Sooner or later you need more: the whole created user, a list of ids to delete at the end, the address from step 2 to compare in step 5.
Variables in Requestarium hold text. That's the one rule to remember, and the rest of this post is what follows from it.
Saving an object
// Create user — post-response script
const user = pm.response.json();
pm.environment.set('createdUser', user);
pm.environment.set('createdUserId', user.id);Objects and arrays are stored as JSON text. Numbers and booleans become their text form (42, true).
Reading it back in a later request
// Get user — post-response script
const expected = JSON.parse(pm.environment.get('createdUser'));
const actual = pm.response.json();
pm.test('read returns what create returned', () => {
pm.expect(actual.email).to.equal(expected.email);
pm.expect(actual.address).to.eql(expected.address);
});eql compares objects deeply, so address matches when every field matches, in any key order.
Collecting a list across requests
A run that creates several records and deletes them at the end needs a list:
// After each "Create …" request
const ids = JSON.parse(pm.environment.get('createdIds') ?? '[]');
ids.push(pm.response.json().id);
pm.environment.set('createdIds', ids);// Clean-up request, pre-request script: build the body from the list
const ids = JSON.parse(pm.environment.get('createdIds') ?? '[]');
pm.request.body.raw = JSON.stringify({ ids });If your API has no bulk delete, loop over the list with one request and pm.execution.setNextRequest. See control the order of a collection run.
Using an object in a request body
A variable is pasted into the body as text, so a JSON object can go straight in:
{
"user": {{createdUser}},
"note": "copied from the create step"
}No quotes around {{createdUser}}: it's already JSON. With quotes, you'd send a string that contains JSON.
The same goes for numbers and booleans:
{ "quantity": {{quantity}}, "giftWrap": {{giftWrap}} }Strings need quotes, as usual: "email": "{{email}}".
Picking a field without a script
If you only need one field in a later request, you don't need to parse anything in a script. Save it on its own (pm.environment.set('city', user.address.city)) and use {{city}}. In a test suite, Save to variable takes a JSON path such as $.address.city and does the same without code.
What happens after the run
Values that scripts set are passed to the requests after them in the same iteration. They aren't written back to the environment, and every iteration starts fresh. That's on purpose: a run doesn't leave stale ids in your shared Staging environment.
If a value must survive between runs, write it to the environment yourself, for example with requestarium env set Staging lastOrderId ord_123 in a CI step. Most values shouldn't: a test that depends on the previous run fails the first time anyone runs it.
Debugging
Print what's stored:
console.log('createdIds =', pm.environment.get('createdIds'));The console output is in the response panel, in the run's expanded row, and in the CLI with --verbose. The JSON report also lists every variable a script set, under variablesSet.