Arythmatic Flow Docs
Docs / Testing

Pre and post-request scripts

Run JavaScript before a request goes out and after the response arrives — to sign requests, capture values, and write tests with logic.

Scripts are for what assertions cannot express. Reach for an assertion first; use a script when you need logic.

Pre-request scripts#

Run before the request is sent, and can change it.

request.headers["Authorization"] = "Bearer " + env.get("token");
request.headers["X-Run-At"] = new Date().toISOString();
request.body = JSON.stringify({ ...JSON.parse(request.body), stamped: true });

Post-request scripts#

Run after the response arrives. Use them to assert with logic, and to capture values for later requests.

test("created with an id", () => {
  if (!response.json?.id) throw new Error("no id in the response");
});

env.set("charge_id", response.json.id);

What is available#

NameWhat it is
requestmethod, url, headers, body — writable in a pre-request script
responsestatus, headers, body, time, and json (the parsed body, or null). Post-request only.
env.get(key) / env.set(key, value)Read and write environment variables
test(name, fn)An assertion — it fails if fn throws
consolePresent, but its output is discarded
What is not available

The sandbox is deliberately small. crypto, fetch, require and Node's built-ins are not available — use {{$uuid}} in the request itself if you need a random id. There is also no response.statusText; use status.

Capturing a value for the next request#

  1. In the first request's post-request script, save what you need.
    env.set("charge_id", response.json.id);
  2. Reference it in a later request.Use it as {{charge_id}} in the URL, a header or the body.
  3. Run the collection in order.The value is set by the time the later request runs.
When to use a workflow instead

For anything more involved than passing a value forward — branching, retries, loops — a workflow says it more clearly than a script.

Failures#

A script that throws is reported and fails the request, and each test() appears alongside your declarative assertions. Scripts run sandboxed with a time limit, so a runaway loop fails that one request rather than the whole run.

Frequently asked questions#

How do I pass a token from one request to the next?

In the first request's post-request script call env.set with the value, then reference it as a variable in the later request. The value is stored in the active environment.

Can I use npm packages in a script?

No. The sandbox exposes request, response, env, test and console only — no require, no fetch, no Node built-ins. This keeps scripts safe to run anywhere, including from the CLI.