Skip to main content

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

TypeWhen it runsCommon uses
Pre-request ScriptBefore the request is sentFetch a token, set dynamic variables, modify headers
Test ScriptAfter the response is receivedAssert 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
  • Access to the rm.* API only — no require(), no file system, no network calls
  • Isolated context — scripts cannot affect each other or the main process directly

Example: Fetch Token Before Request

// Pre-request Script
const tokenResponse = pm.sendRequest({
url: rm.environment.get('tokenUrl'),
method: 'POST',
body: {
grant_type: 'client_credentials',
client_id: rm.environment.get('clientId'),
client_secret: rm.environment.get('clientSecret'),
}
})

rm.environment.set('accessToken', tokenResponse.json().access_token)

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

LevelHow to open
CollectionClick collection name in sidebar → Scripts sub-tab
FolderClick folder name in sidebar → Scripts sub-tab
RequestRequest 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.