Calling

Calling SWML overview

View as MarkdownOpen in Claude

Calling SWML is the flavor of SWML used to handle voice calls — inbound calls arriving on a phone number configured with a SWML calling handler, outbound REST-initiated calls that point at a SWML URL, and re-fetches triggered by methods that hit an external URL.

For handling inbound SMS and MMS messages, see the Messaging SWML overview.

Document structure

A Calling SWML document follows the standard SWML document structure — a top-level sections map with sections.main as the entry point. Each section contains an array of calling methods that run sequentially.

1version: 1.0.0
2sections:
3 main:
4 - answer: {}
5 - play:
6 url: "say:Hello!"
7 - hangup: {}

Webhook and variable payload

When SignalWire fetches a Calling SWML document from an external URL, it POSTs this payload to your server — on the initial inbound fetch and on every fetch triggered by a method that hits an external URL (execute with a remote URL, transfer, join_conference.wait_url, enter_queue.wait_url, and connect.confirm). Your server must respond with a valid SWML document using one of these content types: application/json, application/yaml, or text/x-yaml.

Inside the executing document, the call, params, and envs fields are available for variable expansion via ${...} (JavaScript expressions) and %{...} (path substitution). As the script runs, methods also populate the vars.* runtime scope — those values are not in the initial inbound payload, but they are propagated across transfer boundaries and delivered on subsequent fetches.

Payload
1{
2 "call": {
3 "call_id": "c2d3e4f5-a6b7-8901-cdef-234567890abc",
4 "node_id": "a1b2c3d4-1111-2222-3333-444455556666",
5 "segment_id": "d3e4f5a6-b7c8-9012-defa-345678901bcd",
6 "call_state": "created",
7 "direction": "inbound",
8 "type": "sip",
9 "from": "sip:user@example.com",
10 "to": "sip:destination@yourdomain.com",
11 "headers": [],
12 "project_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
13 "space_id": "d3e4f5a6-b7c8-9012-defa-345678901bcd"
14 },
15 "vars": {
16 "user_selection": "1"
17 },
18 "envs": {
19 "api_key": "<YOUR_API_KEY>",
20 "webhook_url": "https://example.com/webhook"
21 },
22 "params": {
23 "department": "sales"
24 }
25}
call
object

Information about the current call. 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
string

A unique identifier for the call.

call.call_state
string

The current state of the call.

call.direction
string

The direction of this call. Possible values: inbound, outbound.

call.from
string

The number/URI that initiated this call.

call.headers
object[]

The headers associated with this call.

call.headers[].name
string

The name of the header.

call.headers[].value
string

The value of the header.

call.node_id
string

A unique identifier for the node handling the call.

call.project_id
string

The Project ID this call belongs to.

call.segment_id
string

A unique identifier for the current call segment.

call.space_id
string

The Space ID this call belongs to.

call.to
string

The number/URI of the destination of this call.

call.type
string

The type of call. Possible values: sip, phone, webrtc.

call.sip_data
object

SIP-specific data for SIP calls. Only present when call.type is sip. Contains detailed SIP header information.

call.sip_data.sip_contact_host
string

The host portion of the SIP Contact header.

call.sip_data.sip_contact_params
object

Additional parameters from the SIP Contact header.

call.sip_data.sip_contact_port
string

The port from the SIP Contact header.

call.sip_data.sip_contact_uri
string

The full URI from the SIP Contact header.

call.sip_data.sip_contact_user
string

The user portion of the SIP Contact header.

call.sip_data.sip_from_host
string

The host portion of the SIP From header.

call.sip_data.sip_from_uri
string

The full URI from the SIP From header.

call.sip_data.sip_from_user
string

The user portion of the SIP From header.

call.sip_data.sip_req_host
string

The host portion of the SIP request URI.

call.sip_data.sip_req_uri
string

The full SIP request URI.

call.sip_data.sip_req_user
string

The user portion of the SIP request URI.

call.sip_data.sip_to_host
string

The host portion of the SIP To header.

call.sip_data.sip_to_uri
string

The full URI from the SIP To header.

call.sip_data.sip_to_user
string

The user portion of the SIP To header.

params
object

Parameters passed by the calling execute or transfer step. Empty {} on the initial inbound fetch. Section-scoped — each execute/transfer replaces (does not merge with) the caller’s params; when an execute returns, the caller’s original params are restored.

vars
object

Runtime variable scope, populated by methods as the script executes. Not part of the initial inbound payload — values appear here only after a method that sets them has run. The full vars object is propagated across transfer boundaries (and on remote-URL execute) and delivered as a top-level vars field on subsequent webhook payloads.

Created by: set — explicitly create or update variables. Method outputs — many methods auto-populate variables (e.g., prompt_value, record_url, return_value). See each method’s reference page for what it sets.

Removed by: unset.

Scope: Global within a single call session. Variables persist across all sections and through execute calls. Connecting to a new call leg resets the vars object to an empty state.

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.

envs
object

Environment variables configured at the account or project level. Account/project-scoped 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.

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.

See Variables for variable-expansion syntax, deployment-mode differences, and tips on accessing nested fields and array elements.

Methods

A method is a single step in a SWML document — each item in a sections.<name> array invokes one method. Each method’s reference page documents its parameters, defaults, and any output variables it sets. Methods below are grouped by what they do.

Conversational AI

Hand the call to an AI agent that holds a natural conversation, recognizes intent, and can call out to your backend via SWAIG functions or webhooks.

Call lifecycle

Control when the call is answered, ended, and what kind of party is on the other end before deciding how to handle it.

Audio & speech

Play audio to the caller, prompt for input, and stream live transcription or translation. Includes noise reduction for clearer audio.

Connecting parties

Bring other phones, SIP endpoints, video rooms, or queued agents into the call.

Recording & taps

Record the call to storage, or stream call media to an external sink in real time.

Messaging & side channels

Send messages, faxes, DTMF, and other out-of-band signals from inside a call. Also collects payments and emits custom project events.

Control flow

Direct execution within a SWML document — call subroutines, jump between labels, branch on expressions, or tail-call into another document entirely.

State & integration

Manage script variables, pause execution, and reach your backend over HTTP.