Keep a value from one collection run to the next

A login script sets accessToken, the next requests use it, the run ends. Next run, the token is gone and the login runs again. That's by design: each run, and each iteration of a run, starts from the saved environment. It keeps runs repeatable.

Sometimes you do want to carry something over: the id of a test account created once, a sync cursor, the last processed event. Here's how to do it on purpose.

What lasts how long

Set byLasts
pm.environment.set / pm.variables.set in a scriptuntil the end of the current iteration
a --data rowthat iteration
--var, --env-var, --env-filethat run
the saved environment (app or requestarium env set)until someone changes it

So the only thing that outlives a run is the saved environment.

Save a value after a run

The JSON report has every request's response, so a CI step can take the value straight from the response that produced it, and save it:

Terminal
requestarium run-collection "Sync API" --env Staging --out report.json

cursor=$(jq -r 'last(.results[] | select(.name == "List events") | .response.body | fromjson | .nextCursor) // empty' report.json)
if [ -n "$cursor" ]; then
  requestarium env set Staging syncCursor "$cursor"
fi

The next run reads {{syncCursor}} from the environment like any other value. (Each request's variablesSet in the report lists the names of the variables its scripts changed, which is handy to check the value was set at all.)

For a secret, such as a long-lived token, pipe it in and mark it secret, so it never appears in the CI log:

Terminal
jq -r 'last(.results[] | select(.name == "Create service token") | .response.body | fromjson | .token) // empty' report.json \
  | requestarium env set Staging serviceToken - --secret

Treat report.json itself as sensitive in that case: it holds the response body with the token.

Before you do: should it carry over?

Values carried between runs make runs depend on each other. If one run fails halfway, the next one starts from a half-finished state, and "it failed because the previous run failed" is the hardest kind of failure to debug.

Common cases, and the usually better answer:

You want to keepBetter
an access tokenlog in at the start of each run, in a setup step
a test user you createdcreate it in setup, delete it in cleanup; or use a fixed test account saved once in the environment
an order id to check latercheck it in the same run
a pagination cursorpage through in one run with setNextRequest

What really needs to carry over: state that lives in another system on purpose, like a sync cursor, or something that's slow or costly to create, like a provisioned tenant. See keep API tests independent.

In CI, mind concurrency

Two pipelines that both read and write syncCursor in the same environment will overwrite each other. Use a variable per pipeline (syncCursor_nightly), or a separate environment.