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 by | Lasts |
|---|---|
pm.environment.set / pm.variables.set in a script | until the end of the current iteration |
a --data row | that iteration |
--var, --env-var, --env-file | that 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:
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"
fiThe 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:
jq -r 'last(.results[] | select(.name == "Create service token") | .response.body | fromjson | .token) // empty' report.json \
| requestarium env set Staging serviceToken - --secretTreat 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 keep | Better |
|---|---|
| an access token | log in at the start of each run, in a setup step |
| a test user you created | create it in setup, delete it in cleanup; or use a fixed test account saved once in the environment |
| an order id to check later | check it in the same run |
| a pagination cursor | page 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.