Pre-request Scripts
A pre-request script runs before the HTTP request is sent. Use it to prepare dynamic data, fetch tokens, or modify variables.
Common Use Cases
Set a dynamic timestamp
rm.environment.set('timestamp', Date.now().toString())
Generate a unique request ID
The sandbox does not expose crypto, so build an id from the globals that are
available (Date and Math):
const requestId = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
rm.environment.set('requestId', requestId)
Tip: to get a real UUID v4, put the built-in
{{$randomUUID}}variable directly in a header or body field instead of generating one in script.
Build a derived value from environment variables
// Combine two variables into a composite value
const host = rm.environment.get('apiHost')
const version = rm.environment.get('apiVersion')
rm.variables.set('baseUrl', `https://${host}/api/${version}`)
// Now use {{baseUrl}} in your request URL
Set variables based on environment
const env = rm.environment.get('envName')
if (env === 'production') {
rm.environment.set('logLevel', 'error')
} else {
rm.environment.set('logLevel', 'debug')
}
Accessing Variables
// Get a variable (checks environment → collection vars → globals)
const baseUrl = rm.variables.get('baseUrl')
// Get specifically from environment
const token = rm.environment.get('accessToken')
// Get from collection variables
const version = rm.collectionVariables.get('apiVersion')
// Get from globals
const globalKey = rm.globals.get('globalKey')
Setting Variables
// Persist to environment (saved to DB)
rm.environment.set('key', 'value')
rm.collectionVariables.set('key', 'value')
rm.globals.set('key', 'value')
// Request-scoped (available for this request only, not saved to DB)
rm.variables.set('key', 'value')
Setting a token for auth
A common pattern is to select a token in a pre-request script and use it in Bearer auth:
const token = rm.environment.get("platformUserToken") || rm.environment.get("platformToken");
rm.variables.set("activeToken", token);
// Now use {{activeToken}} in your Bearer Token auth field
Logging
Use console.log() for debugging — output appears in the in-app Console panel at the bottom of the window (toggle it with Ctrl+ `):
console.log('token:', rm.environment.get('accessToken'))