Skip to content

JavaScript Scripts

JavaScript scripts run in a Node.js 24 child process using vm.runInContext for sandboxing. The sandbox provides fetch (Node 18+ built-in), a logger object, and injected data source helpers — nothing else.

Available globals

GlobalDescription
fetchBuilt-in Node.js fetch — make HTTP requests
authHeadersAuth headers for the task's configured auth
datasourcesData source connections — see Data Sources
loggerStructured logging — .info(), .warn(), .error(), .debug()
payloadWebhook or MQTT payload string (if triggered by one)
JSONStandard JSON object
consoleconsole.log / console.error (mapped to logger.info / logger.error)

Blocked

  • require / import — no module loading
  • process — no process object (no process.env, process.exit, etc.)
  • fs, child_process, os — no Node built-ins beyond fetch

Example: call a REST API

javascript
const response = await fetch('https://api.example.com/users', {
  headers: authHeaders
})

if (!response.ok) {
  logger.error(`API error: ${response.status}`)
  return
}

const users = await response.json()
logger.info(`Found ${users.length} users`)

const active = users.filter(u => u.active)
logger.info(`Active: ${active.length}`)

console.log(JSON.stringify(active, null, 2))

Example: POST data

javascript
const payload = {
  report: 'nightly',
  timestamp: new Date().toISOString(),
  count: 99
}

const res = await fetch('https://hooks.example.com/report', {
  method: 'POST',
  headers: {
    ...authHeaders,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(payload)
})

logger.info(`Webhook response: ${res.status}`)

Example: use a data source

javascript
// Async helpers are injected per datasource endpoint
const users = await datasources.MyApi.getUsers()
logger.info(`Users: ${users.length}`)

Error handling

Unhandled promise rejections and thrown errors are caught by the sandbox and recorded as ERROR level in the run log. The run is marked Failed.

javascript
try {
  const res = await fetch('https://api.example.com/data', { headers: authHeaders })
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  const data = await res.json()
  logger.info(`Got ${data.items.length} items`)
} catch (err) {
  logger.error(`Failed: ${err.message}`)
}