Skip to content

Script files

A script file is a .js file that sends your saved requests by their # @name, with real logic between them: loops, branches, waiting for a job to finish, checks. Use one when a runbook would be all steps and no text.

A .js file that calls send("…") appears in the tree as a script; other .js files, such as request handlers, stay hidden. Right-click a folder and choose New script… to start from a template.

reindex.js
const index = input("indexName", "products-v2"); // asks, and sets {{indexName}}
confirm(`Copy every document into ${index}?`); // "no" stops the script
const start = send("startReindex"); // the request named startReindex
vars.set("jobId", start.body.jobId); // {{jobId}} for the requests after this
poll(() => send("reindexStatus"), {
until: r => r.body.state === "done",
failIf: r => r.body.state === "failed",
every: "30s",
giveUpAfter: "2h",
});
send("swapAlias", { alias: "products" }); // a value for this request only
log("Reindex finished");

send runs a request exactly as pressing Send would: the same environment, @variables, client.global values, scripts, cookies and chaining. While you type send(", the folder’s request names are suggested.

Press Run (or Ctrl+Enter). The output lists each log line and each request in order; select a request to see its response. Stop cancels the script, even while it’s waiting.

A script file open in Reqjet, with its output listing each request it sent and each log lineA script file open in Reqjet, with its output listing each request it sent and each log line
A script's output lists each log line and request in order.

Unlike request scripts, a script file has no 30-second limit.

On the command line: reqjet run reindex.js --env dev --var indexName=products-v3. --var answers input(), and --yes answers confirm().

Function What it does
send("name", { key: value }) Sends a request and returns its response: status, ok (a 2xx status), body, text, headers.valueOf(name) and tests. The name is looked up in the script’s folder and below; use "file.http#name" when it’s in more than one file. The optional values apply to that request only. A request that gets no response throws an error.
vars.get(name), vars.set(name, value) The script’s values. Every request it sends sees them as {{name}}, before anything else.
env.get(name) A value from the selected environment.
resolve("{{baseUrl}}/x") Fills in {{variables}} as a request would.
input(name, default, description) Asks for a value and sets it as {{name}}. Cancelling stops the script.
confirm(message) Asks before going on. “No” stops the script, and a try/catch doesn’t catch that.
poll(fn, { until, failIf, every, giveUpAfter }) Calls fn until until(result) holds. It fails when failIf(result) holds, or after giveUpAfter. The defaults are 10s and 30m.
sleep("5s") Waits.
log(...) Writes to the output.
test(name, fn), assert(condition, message) Records pass or fail checks.
client.global The session’s script values, shared with request scripts.

The request script helpers, such as reqjet.http.get, jsonPath and crypto, work too.