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 theScript 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
TheScript component provides two CodeMirror editors:
- Pre-request editor:
preRequestEditorRef- Mode:javascript - Post-response editor:
postResponseEditorRef- Mode:javascript
- Syntax highlighting for JavaScript
- Auto-hints for
req,res, andbruobjects - 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 thereq and bru objects.
Available Objects
Request object with methods to modify the outgoing request:
req.setHeader(name, value): Set request headerreq.getHeader(name): Get request headerreq.setBody(data): Set request bodyreq.getUrl(): Get request URLreq.setUrl(url): Set request URL
Bruno utility object:
bru.setVar(name, value): Set collection variablebru.getVar(name): Get collection variablebru.setEnvVar(name, value): Set environment variablebru.getEnvVar(name): Get environment variable
Pre-Request Script Examples
- Set Dynamic Headers
- Basic Auth Encoding
- Set Variables
- Modify Request Body
- Dynamic URL
Post-Response Scripts
Post-response scripts execute after receiving the API response. They have access toreq, res, and bru objects.
Available Objects
Response object with response data:
res.status: HTTP status coderes.statusText: Status text (e.g., “OK”)res.headers: Response headers objectres.body: Parsed response bodyres.getHeader(name): Get specific headerres.getBody(): Get response body
Original request object (read-only in post-response)
Bruno utility object (same as pre-request)
Post-Response Script Examples
- Extract & Store Token
- Chain Requests
- Extract Pagination Info
- Conditional Logic
- Response Processing
From the test suite:More complex example:
bruno-tests/collection/auth/bearer/via auth/Bearer Auth 200.bru
Collection-Level Scripts
Scripts can be configured at the collection level incollection.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: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
Useconsole.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
item.preRequestScriptErrorMessageitem.postResponseScriptErrorMessage
Common Patterns
Token Refresh Flow
Token Refresh Flow
Pre-Request Script
Post-Response Script
Request Signing
Request Signing
Conditional Request Modification
Conditional Request Modification
Data Extraction & Chaining
Data Extraction & Chaining
Best Practices
Keep Scripts Simple
Keep Scripts Simple
Scripts should be focused and easy to understand. Complex logic should be broken into multiple scripts or extracted to collection-level scripts.
Use Console Logging
Use Console Logging
Add console.log statements to track script execution and debug issues. Logs appear in the Response Console tab.
Handle Errors Gracefully
Handle Errors Gracefully
Wrap risky operations in try-catch blocks to prevent script failures from breaking your workflow.
Leverage Collection Scripts
Leverage Collection Scripts
Put common logic (auth token refresh, signature generation) in collection-level scripts to avoid duplication.
Use Variables Wisely
Use Variables Wisely
Store reusable values as variables. Use environment variables for environment-specific values and collection variables for runtime data.
Document Complex Scripts
Document Complex Scripts
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:
updateRequestScript: Updates pre-request scriptupdateResponseScript: 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