Skip to main content
Bruno provides powerful scripting capabilities through the Script component, allowing you to write JavaScript code that executes before sending requests (pre-request) or after receiving responses (post-response).

Overview

Scripts in Bruno are JavaScript code snippets that run in a sandboxed environment. They’re managed by the Script component in RequestPane/Script/index.js.

Script Types

Bruno supports two types of scripts:

Pre-Request Scripts

Execute before the request is sent. Use to set variables, modify headers, or prepare data.Accessed via the “Pre Request” tab in the Script pane.

Post-Response Scripts

Execute after receiving the response. Use to extract data, set variables, or chain requests.Accessed via the “Post Response” tab in the Script pane.

Script Editor

The Script component provides two CodeMirror editors:
  • Pre-request editor: preRequestEditorRef - Mode: javascript
  • Post-response editor: postResponseEditorRef - Mode: javascript
Both editors include:
  • Syntax highlighting for JavaScript
  • Auto-hints for req, res, and bru objects
  • Keyboard shortcuts (Cmd/Ctrl+Enter to run, Cmd/Ctrl+S to save)

Error Indicators

The Script tab shows status dots to indicate script state:
From Script component
Error status dots appear when a script fails execution, helping you quickly identify problematic scripts.

Pre-Request Scripts

Pre-request scripts execute before Bruno sends the HTTP request. They have access to the req and bru objects.

Available Objects

object
Request object with methods to modify the outgoing request:
  • req.setHeader(name, value): Set request header
  • req.getHeader(name): Get request header
  • req.setBody(data): Set request body
  • req.getUrl(): Get request URL
  • req.setUrl(url): Set request URL
object
Bruno utility object:
  • bru.setVar(name, value): Set collection variable
  • bru.getVar(name): Get collection variable
  • bru.setEnvVar(name, value): Set environment variable
  • bru.getEnvVar(name): Get environment variable

Pre-Request Script Examples

Post-Response Scripts

Post-response scripts execute after receiving the API response. They have access to req, res, and bru objects.

Available Objects

object
Response object with response data:
  • res.status: HTTP status code
  • res.statusText: Status text (e.g., “OK”)
  • res.headers: Response headers object
  • res.body: Parsed response body
  • res.getHeader(name): Get specific header
  • res.getBody(): Get response body
object
Original request object (read-only in post-response)
object
Bruno utility object (same as pre-request)

Post-Response Script Examples

From the test suite:
bruno-tests/collection/auth/bearer/via auth/Bearer Auth 200.bru
More complex example:

Collection-Level Scripts

Scripts can be configured at the collection level in collection.bru, as shown in the test suite:
bruno-tests/collection/collection.bru
Collection-level scripts run for every request in the collection, unless overridden at the folder or request level.

Script Execution Order

When a request is sent, scripts execute in this order:
1

Collection Pre-Request Script

If configured in collection.bru, runs first.
2

Folder Pre-Request Script

If the request is in a folder with scripts, folder script runs next.
3

Request Pre-Request Script

Request-specific pre-request script runs last.
4

HTTP Request Sent

Bruno sends the request with all modifications applied.
5

Request Post-Response Script

Request-specific post-response script runs first.
6

Folder Post-Response Script

Folder post-response script runs next.
7

Collection Post-Response Script

Collection post-response script runs last.

Available Node.js Modules

Bruno scripts run in a sandboxed environment with access to common Node.js modules:
The available modules depend on the bruno-js package configuration. Check packages/bruno-js for the complete list of bundled libraries.

Debugging Scripts

Console Logging

Use console.log() to debug scripts. Output appears in the response pane’s Console tab:

Error Handling

Status Indicators

The Script tab shows error indicators:
Component behavior
Errors are stored in:
  • item.preRequestScriptErrorMessage
  • item.postResponseScriptErrorMessage

Common Patterns

Pre-Request Script
Post-Response Script

Best Practices

Scripts should be focused and easy to understand. Complex logic should be broken into multiple scripts or extracted to collection-level scripts.
Add console.log statements to track script execution and debug issues. Logs appear in the Response Console tab.
Wrap risky operations in try-catch blocks to prevent script failures from breaking your workflow.
Put common logic (auth token refresh, signature generation) in collection-level scripts to avoid duplication.
Store reusable values as variables. Use environment variables for environment-specific values and collection variables for runtime data.
Add comments to explain what the script does, especially for complex transformations or auth flows.

Script Storage

Scripts are stored in the .bru file format:
The Redux store manages draft scripts:
  • updateRequestScript: Updates pre-request script
  • updateResponseScript: Updates post-response script

Next Steps

Tests

Learn how to write test assertions

Collection Settings

Configure collection-level scripts

Environment Variables

Manage variables across environments

Authentication

Use scripts for dynamic authentication