Skip to main content
Bruno provides a powerful JavaScript runtime for scripting and automation. You can write scripts in three contexts: pre-request scripts, post-response scripts, and tests.

Available Contexts

Bruno scripts run in a sandboxed JavaScript environment with access to:
  • Pre-request scripts (script:pre-request): Run before the request is sent
  • Post-response scripts (script:post-response): Run after the response is received
  • Tests (tests): Run after the response to validate results

Global Objects

All scripts have access to these global objects:
bru
object
The main Bruno API object for managing variables, environment, and request flow
req
BrunoRequest
The request object (available in pre-request and post-response scripts)
res
BrunoResponse
The response object (available in post-response scripts and tests only)
test
function
Function to define test assertions
expect
object
Chai’s expect assertion library
assert
object
Chai’s assert assertion library
console
object
Console logging object with log(), info(), warn(), error(), and debug() methods

Built-in Libraries

Bruno includes several built-in libraries that you can access using require():

chai

Assertion library (automatically available as expect and assert)

moment

Date and time manipulation library

btoa

Base64 encoding function

atob

Base64 decoding function

crypto-js

Cryptographic functions

jsonwebtoken

JWT creation and verification

buffer

Buffer implementation for binary data

tv4

JSON schema validation

Example Usage

script:pre-request {
  // Set authentication header
  const token = bru.getEnvVar('api_token');
  req.setHeader('Authorization', `Bearer ${token}`);
  
  // Modify request body
  const body = req.getBody();
  body.timestamp = Date.now();
  req.setBody(body);
}

Script Execution Order

When a request is executed, scripts run in this order:
  1. Pre-request script - Modify request before sending
  2. HTTP Request - Send the request
  3. Post-response script - Process the response
  4. Assertions - Run declarative assertions from assert block
  5. Tests - Run test scripts

Runtime Modes

Bruno supports two runtime modes:
The default runtime that provides a secure, sandboxed environment. Recommended for most use cases.
  • Limited file system access
  • Better security
  • Faster startup time
Check if running in safe mode:
if (bru.isSafeMode()) {
  console.log('Running in safe mode');
}
A more permissive runtime with full Node.js capabilities. Enable this in collection settings for advanced scripting needs.
  • Full file system access
  • Additional Node.js modules
  • Custom library imports

Next Steps

Pre-request API

Modify requests before they are sent

Post-response API

Process responses and extract data

Test API

Write assertions and validate responses

Advanced Scripting

Common scripting patterns and examples

Build docs developers (and LLMs) love