javascript@v1

Execute JavaScript code.

Versionjavascript@v1
Runs ininbound-call and your own branches · api-initiated and inbound-sms · hangup, postHangup and events.ready

Parameters

NameTypeRequiredDefaultDescription
codestringyes1..10240 characters

Every parameter that accepts ${{ }} can be computed from an expression.

How it works

Execute JavaScript code with a modern runtime. You can access all variables directly from the code.

Examples

example: increment an existing variable

variables:
  $errors: 0

branches:
  inbound-call:
    actions:
      - action: javascript@v1
        params:
          code: $errors++

You may also return values

- action: javascript@v1
  params:
    code: |
      return [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0)
  result: $sum

- action: play@v3
  params:
    tts:
      text: 1+2+3 equals ${{ $sum }}

You can also create new variables from the js code

Reminder: variables MUST start with a dollar sign

- action: javascript@v1
  params:
    code: |
      let $animals = ['dogs', 'cats', 'birds']
      $animals.push('fishes')

      // To create new variables, return an object.
      // Keys starting with $ will be created as variables.
      return {
        $animals,
        $greeting: 'hello',
        ignored: 'not starting with $'
      }

- action: play@v3
  params:
    tts:
      text: ${{ $greeting }}! I like ${{ $animals.join(', ') }}

Result

Set result: $myVariable to keep it. The value returned by the code (any JSON value). The variable is not set when the code returns nothing (undefined or null) or throws

Type: any JSON value

6

Did this page help you?