Variables
SWML provides a powerful variable system that allows you to access call information, store data, and pass parameters between sections.
This page is the authoritative technical reference for global variable scopes (call, params, vars, envs) and variable management in SWML.
For information about using JavaScript expressions with variables, see the Expressions Reference. For template transformation functions, see the Template Functions Reference.
Syntax
SWML uses the ${variable} syntax for variable substitution:
Reference
SWML provides several variable scopes that are available throughout your scripts. These scopes give you access to call information, script parameters, and stored data.
call
Information about the current call. Contains the following properties.
Scope: Call-specific and read-only. Each call leg (A-leg, B-leg) has its own unique call object with different call_id, from, to, etc.
When connecting to a new leg, the call object is re-initialized with the new leg’s data.
call.call_id
A unique identifier for the call.
call.call_state
The current state of the call.
call.direction
The direction of this call. Possible values: inbound, outbound
call.from
The number/URI that initiated this call.
call.headers
The headers associated with this call.
call.headers[].name
The name of the header.
call.headers[].value
The value of the header.
call.node_id
A unique identifier for the node handling the call.
call.project_id
The Project ID this call belongs to.
call.segment_id
A unique identifier for the current call segment.
call.sip_data
SIP-specific data for SIP calls. Only present when call.type is sip. Contains detailed SIP header information.
call.space_id
The Space ID this call belongs to.
call.to
The number/URI of the destination of this call.
call.type
The type of call. Possible values: sip, phone, webrtc
sip_data.sip_contact_host
The host portion of the SIP Contact header.
sip_data.sip_contact_params
Additional parameters from the SIP Contact header.
sip_data.sip_contact_port
The port from the SIP Contact header.
sip_data.sip_contact_uri
The full URI from the SIP Contact header.
sip_data.sip_contact_user
The user portion of the SIP Contact header.
sip_data.sip_from_host
The host portion of the SIP From header.
sip_data.sip_from_uri
The full URI from the SIP From header.
sip_data.sip_from_user
The user portion of the SIP From header.
sip_data.sip_req_host
The host portion of the SIP request URI.
sip_data.sip_req_uri
The full SIP request URI.
sip_data.sip_req_user
The user portion of the SIP request URI.
sip_data.sip_to_host
The host portion of the SIP To header.
sip_data.sip_to_uri
The full URI from the SIP To header.
sip_data.sip_to_user
The user portion of the SIP To header.
envs
Environment variables configured at the account or project level in your SignalWire configuration. These provide access to account-wide settings and configuration values.
Coming soon!
The envs object is included in POST request bodies to external servers, but the ability to set environment variables in the SignalWire Dashboard is not yet available in production. This feature is coming soon.
Scope: Account/project-level and read-only. Set in your SignalWire account configuration, not within SWML scripts.
Fallback behavior: When you reference a variable without a scope prefix (e.g., ${my_variable}), SWML first checks vars. If not found in vars, it automatically falls back to envs.
Example keys: envs.api_key, envs.webhook_url, envs.account_setting
Scope: Section-scoped and read-only. Each section has its own independent params that must be explicitly passed via execute or transfer. Params do not persist after a section completes.
When an execute call returns, the calling section’s original params are restored.
Example keys: params.audio_file, params.volume, params.department
vars
Script variables that can be set, modified, and accessed throughout your SWML script.
Created by:
setmethod - Create or update user-defined variables- Method outputs - Many methods automatically create variables (e.g.,
prompt_value,record_url,return_value)
Managed with:
unsetmethod - Remove variables
Example keys: vars.user_choice, vars.counter, vars.prompt_value, vars.record_url
Scope: Global within a single call session. Variables persist across all sections and through execute calls.
However, connecting to a new leg of a call will reset the vars object to an empty state.
Variable access: Variables can be accessed with or without the vars. prefix.
When you reference a variable without a scope prefix (e.g., ${my_variable}), SWML first checks vars. If not found in vars, it automatically falls back to envs.
Example: Variables in JSON format
When SWML communicates with your SWML server, the following request body format is used to represent variables:
Variables in serverless and server-based SWML
All SWML variables (call, params, vars, envs) are available in both serverless (Dashboard-hosted) and server-based (external URL) deployments.
Serverless (dashboard-hosted) scripts
When SWML is executed directly from the SignalWire Dashboard, access variables using the ${} syntax:
Server-based (external URL) scripts
When SWML is served from your web server, SignalWire sends the current variable state as JSON in the POST request body (see the JSON format example above).
You have two options for working with these variables in your SWML response:
Option 1: Use variable expansion syntax
Use ${} syntax in your returned SWML, and SignalWire will substitute the values at runtime:
Option 2: Extract and insert values server-side
Extract variables from the request body in your server code and insert them directly into the SWML response:
Both approaches produce the same result. Use variable expansion (${}) for simpler cases, or extract values server-side when you need to perform logic or transformations on the data.
See the Deployment Guide for complete server setup instructions.
Accessing variable data
Variables can contain different types of data - simple values, nested objects, or arrays. Use dot notation (.) for object properties, bracket notation ([]) for array elements, or combine both for complex data structures.
Simple values
Access variables directly by name:
Nested objects
Access nested properties using dot notation:
Arrays
Access array elements using bracket notation with zero-based indexing:
Arrays of objects
Combine bracket and dot notation to access properties in array elements: