Scripting
Rostyman lets you run JavaScript before and after every request. Scripts use the rm.* API — Rostyman's own scripting namespace (if you've used other API tools, it's a similar concept).
Script Types
| Type | When it runs | Common uses |
|---|---|---|
| Pre-request Script | Before the request is sent | Fetch a token, set dynamic variables, modify headers |
| Test Script | After the response is received | Assert status codes, validate response body, set variables from response |
Script Editor
Both script types use the Monaco editor (the same editor as VS Code) with:
- JavaScript syntax highlighting
- Auto-completion for
rm.*API - Error highlighting
Execution Environment
Scripts run in a sandboxed Node.js vm context in the Electron main process with:
- A 5-second timeout — scripts that run longer are killed automatically
- The
rm.*API, pluspm.*/pw.*/tc.*/bru.*compatibility aliases so scripts imported from Postman, Hoppscotch, Thunder Client, and Bruno run unchanged - A restricted set of JavaScript globals:
JSON,Math,Date,RegExp,Array,Object,String,Number, theparse*/encode*/decode*helpers, andatob/btoa - No
require(), no file system, no network calls, no DOM, and nosetTimeout/setInterval - Isolated context — scripts cannot affect each other or the main process directly
Example: Select a Token Before Request
Scripts cannot make network calls. A common pattern is to read a token that was set beforehand (manually, by a login request's test script, or in an environment variable) and apply it as a request-scoped variable for auth:
// Pre-request Script
const token = rm.environment.get('accessToken')
rm.variables.set('activeToken', token)
// Now reference {{activeToken}} in your Bearer Token auth field
Example: Test Response
// Test Script
rm.test('Status is 200', () => {
rm.expect(rm.response.code).to.equal(200)
})
rm.test('Response has user ID', () => {
const body = rm.response.json()
rm.expect(body).to.have.property('id')
rm.expect(body.id).to.be.a('number')
})
// Save the ID for use in other requests
rm.environment.set('userId', rm.response.json().id)
Script Execution Order
When a request is inside folders and a collection, scripts execute in a specific order. Pre-request scripts run from outermost to innermost, and post-response scripts run in reverse.
1. Collection pre-request script
2. Root folder pre-request script
3. Sub-folder pre-request script(s) — outermost → innermost
4. Request pre-request script
← Request is sent →
5. Request post-response script
6. Sub-folder post-response script(s) — innermost → outermost
7. Root folder post-response script
8. Collection post-response script
Variable Cascade
Variables set by rm.environment.set(), rm.collectionVariables.set(), or rm.globals.set() in earlier scripts are available in later scripts in the chain. This means a collection script can set a token that folder and request scripts can read.
Where to Add Scripts
| Level | How to open |
|---|---|
| Collection | Click collection name in sidebar → Scripts sub-tab |
| Folder | Click folder name in sidebar → Scripts sub-tab |
| Request | Request builder → Scripts tab (Pre-request / Post-response) |
Test Results
After a request with test scripts, the Tests tab in the response panel shows:
✓ Status is 200
✓ Response has user ID
2 passed, 0 failed
Failed tests show the assertion message and expected vs actual values.
Code Snippets
The script editor includes a Snippets panel (click the icon in the toolbar) with ready-to-insert code examples for common tasks:
- Set/get environment variable
- Set/get collection variable
- Set/get global variable
- Log to console
- Parse JSON response
- Write assertions
Click any snippet to insert it at the cursor position.
Beautify
Click the Beautify button (or press Shift+Alt+F) in the script editor toolbar to auto-format your JavaScript. This uses the Monaco formatter and respects standard JS style.
AI-Assisted Scripting
Open the AI Assistant from the right panel and describe what you want the script to do. The AI can:
- Write a pre-request script from a natural language description
- Suggest test assertions based on the response body you paste
- Explain what an existing script does
- Fix errors in a failing script
Generated scripts are inserted directly into the script editor. You can edit them further before saving.