Skip to content
Vegha Docs

Scripting API Reference

This page lists the objects and functions available inside pre-request and post-response scripts. All of them run inside the Jint sandbox.

The bru object reads and writes variables and the environment.

MemberDescription
bru.getVar(name)Returns the value of a variable, resolving across all scopes
bru.setVar(name, value)Sets a runtime variable for the current run
bru.getEnvVar(name)Returns a value from the active environment
bru.setEnvVar(name, value)Sets a value in the active environment
const base = bru.getEnvVar("base_url");
bru.setVar("health_url", base + "/health");

See Variable types for how scopes and precedence interact.

The req object represents the outgoing request. It is available in both pre-request and post-response scripts; mutating it in a pre-request script changes what is sent.

MemberDescription
req.urlThe request URL — readable and writable
req.methodThe HTTP method, for example GET or POST
req.headersThe request headers, as a key/value map
req.bodyThe request body
req.headers["X-Request-Id"] = bru.getVar("request_id");

The res object represents the response. It is available only in post-response scripts and the Tests tab.

MemberDescription
res.statusThe numeric HTTP status code
res.statusTextThe HTTP status text, for example OK
res.headersThe response headers, as a key/value map
res.bodyThe response body (parsed for JSON responses)
res.responseTimeThe total response time in milliseconds
bru.setVar("access_token", res.body.token);

Used in the Tests tab to write assertions. See Assertions & tests for a fuller guide.

FunctionDescription
test(name, fn)Defines a named test; fn contains the assertions
expect(value)Begins an assertion chain on value

Common expect chains:

ChainAsserts
.to.equal(x)Value equals x
.to.existValue is defined and not null
.to.include(x)String or array contains x
.to.be.below(n)Number is less than n
.to.have.lengthOf(n)Array or string has length n
test("status is 200", () => {
expect(res.status).to.equal(200);
});