SWML (SignalWire Markup Language) 🔗 ↑ TOC
SignalWire Markup Language (SWML) is a declarative markup language designed to orchestrate telecom channels, complex call flows, and teams of conversational agents using YAML or JSON documents.
SWML (pronounced "swimmel" or sometimes called S-W-M-L) is essentially a domain-specific programming language for telephony and conversational AI that maps agent development to familiar web app development paradigms.
- Building IVRs (Interactive Voice Response systems)
- Creating AI agents and conversational interfaces
- Defining call center features and workflows
- Real-time interaction updates (e.g., call transfers)
- Integration with external APIs and databases
SWML as the DNA of Programmable Communication 🔗 ↑ TOC
SWML is tightly integrated with SignalWire's infrastructure and serves as the foundational layer that:
- Exposes FreeSWITCH features in a simplified way
- Adds multiple contexts and menus in a complete document
- Enables async behaviors (start/stop recording mid-call, SMS integration during phone calls)
- Provides a framework for quickly adding powerful verbs and applications
SWML documents are written in YAML or JSON. Every script must include:
version: 1.0.0
sections:
main:
# Your script logic here
- Every SWML script starts with
version: 1.0.0
- This is currently the only supported version
- Scripts are divided into sections (arrays of statements and methods)
- Every script must have a
main
section where execution starts
- Sections behave like functions when called with
execute
- Control transfer with
transfer
doesn't return to calling section
Variable Substitution 🔗 ↑ TOC
- Use
%{ }
brackets for JavaScript expressions
- Example:
%{params.to_play}
or %{call.from}
- Supports complex expressions:
%{params.to_play.toLowerCase()}
1. Conversation Structuring and Routing 🔗 ↑ TOC
- Define call flows with clear, maintainable logic
- Agent handoffs between different conversation contexts
- Conditional routing based on user input or external data
- Loop and branching capabilities for complex workflows
2. User Interaction and Data Collection 🔗 ↑ TOC
- Seamless management of interactive prompts
- Dynamic data capture from voice, DTMF, or chat input
- Real-time response generation
- Integration with external data sources
3. Theming & Personalization 🔗 ↑ TOC
- Customize agent behavior and appearance
- Similar to CSS/theming in web frameworks
- Brand-specific responses and personality configuration
- Multi-language and localization support
4. Reusable Logic and Components 🔗 ↑ TOC
- Build modular, component-like patterns
- Reuse across different call scenarios
- Library of common functions and behaviors
- Template-based development approach
5. Security, Deployment, and Observability 🔗 ↑ TOC
- Robust security measures and authentication
- Deployment strategies and environment management
- Comprehensive logging for monitoring performance
- Debug capabilities and error handling
SWML and Call Fabric Integration 🔗 ↑ TOC
SWML works with Call Fabric's resource model using standardized addresses:
space_name.signalwire.com/context/address
Components:
- Context: Identifier indicating resource location
- Name: Unique identifier for the resource
Example: /public/Alice
for a Subscribers resource named "Alice" in the public context
- Rooms: Audio/video conferencing spaces
- Subscribers: SIP endpoints, mobile apps, authenticated accounts
- Scripts: SWML and cXML scripts for workflows
- AI Agents: Intelligent assistants for calls and data integration
- Queues: Traffic routing based on criteria
- Navigate to "Relay/SWML" section
- Switch to "SWML Scripts" tab
- Create and save new scripts
- Configure phone numbers to use saved scripts
From External Web Server 🔗 ↑ TOC
- Enable "Use External URL for SWML Script handler"
- Set web URL to serve SWML scripts
- Receive HTTP POST requests with call parameters
- Return dynamically generated SWML responses
HTTP Request Parameters 🔗 ↑ TOC
When serving from external URL, receive:
Parameter |
Type |
Description |
call |
Call object |
Properties describing the call |
vars |
Object |
Variables set in calling SWML |
params |
Object |
Parameters from execute/transfer methods |
Call Object Properties 🔗 ↑ TOC
call_id
: Unique identifier for the call
node_id
: Unique identifier for handling node
segment_id
: Unique identifier for segment
call_state
: Current state of the call
direction
: "inbound" or "outbound"
type
: "sip" or "phone"
from
: Number/URI that initiated call
to
: Destination number/URI
headers
: Associated headers array
project_id
: Project ID for the call
space_id
: Space ID for the call
version: 1.0.0
sections:
main:
- answer: {}
- execute:
dest: play_music
params:
to_play: 'https://cdn.signalwire.com/swml/April_Kisses.mp3'
play_music:
- play:
url: '%{params.to_play}'
This script:
1. Answers the incoming call
2. Executes the play_music
section with parameters
3. Plays the specified audio file
4. Uses variable substitution to access the parameter
AI Integration in SWML 🔗 ↑ TOC
version: 1.0.0
sections:
main:
- ai:
post_prompt_url: https://example.com/my-api
prompt:
text: |
You are Franklin's assistant collecting messages.
Collect the user's name and number if not known from caller ID.
After collecting the message, say goodbye and hang up.
post_prompt:
text: |
Summarize as JSON: { "contact_info": { "name": "NAME", "number": "PHONE" }, "message": "MESSAGE" }
- Natural conversation flow
- Context preservation across interactions
- Integration with SWAIG functions for tool use
- Real-time conversation modification
- Multi-language support
answer
: Answer incoming calls
play
: Play audio files or text-to-speech
record
: Record audio from callers
hangup
: End the call
execute
: Call other sections with parameters
transfer
: Transfer control to other sections
ai
: Create conversational AI agents
swaig
: Define AI gateway functions
- Integration with external LLMs and services
- Conditional logic and branching
- Loop constructs for repetitive tasks
- Error handling and fallback scenarios
- State management across sessions
- Use descriptive section names
- Keep sections focused on single responsibilities
- Document complex logic with comments
- Version control your SWML scripts
- Minimize external API calls in critical paths
- Cache frequently accessed data
- Use efficient audio formats
- Optimize for low-latency interactions
Security Considerations 🔗 ↑ TOC
- Validate all user inputs
- Secure external webhook endpoints
- Use authentication for sensitive operations
- Log security-relevant events
Testing and Debugging 🔗 ↑ TOC
- Test with various input scenarios
- Use logging for troubleshooting
- Validate with different caller types
- Monitor performance metrics
Integration Capabilities 🔗 ↑ TOC
- REST API integration for data retrieval
- Webhook callbacks for real-time events
- Database connectivity for persistent storage
- CRM and business system integration
Communication Channels 🔗 ↑ TOC
- Traditional phone calls (PSTN)
- SIP-based telephony
- WebRTC for browser-based calling
- SMS and messaging integration
- Video conferencing capabilities
- IDE plugins and syntax highlighting
- Local testing and development environments
- CI/CD pipeline integration
- Version control and deployment automation