Asterisk Dialplan: Variables (Latest Versions)
Updated guide for modern Asterisk (PJSIP era): variables with real configs, common mistakes, and troubleshooting steps.
Variables are one of the most powerful features in the Asterisk dialplan. They allow calls to become dynamic, intelligent, and data-driven instead of fixed and repetitive.
Modern telecom systems—IVR routing, CRM integration, call center logic, AI calling, and reporting—depend heavily on correct use of dialplan variables.
This guide explains variables in latest Asterisk with PJSIP, including real production examples and common mistakes engineers face.
What Are Variables in Asterisk?
A variable is a named value stored during call execution.
It can hold:
- Caller number
- Dialed extension
- Queue name
- Database result
- Time or status information
Variables make routing decisions possible.
How to Set and Use Variables
Setting a Variable
same => n,Set(USER_ID=1001)
Reading a Variable
same => n,NoOp(User is ${USER_ID})
Syntax rule: ${VARIABLE_NAME}
Built-In Variables Every Engineer Must Know
- ${EXTEN} → dialed extension
- ${CONTEXT} → current context
- ${CALLERID(num)} → caller number
- ${CALLERID(name)} → caller name
- ${CHANNEL} → channel identifier
- ${UNIQUEID} → unique call ID
- ${DIALSTATUS} → result after Dial()
These are used in almost every production dialplan.
Example: Logging Caller Information
exten => s,1,NoOp(Incoming call from ${CALLERID(num)})
same => n,NoOp(Unique call id ${UNIQUEID})
same => n,Hangup()
Channel Variables vs Global Variables
Channel Variable
Exists only during the current call:
same => n,Set(TEMP_VALUE=123)
Global Variable
Defined in extensions.conf:
[globals]
COMPANY_NAME=MYLINEHUB
same => n,NoOp(${COMPANY_NAME})
Passing Variables Between Call Legs
To share data after Dial():
same => n,Set(__CAMPAIGN_ID=42)
Prefix meanings:
- _VAR → inherited by next channel
- __VAR → inherited by all future channels
Critical for:
- Call center tracking
- CRM logging
- Recording metadata
Using Variables Inside Dial()
same => n,Set(AGENT=1001)
same => n,Dial(PJSIP/${AGENT},20)
Enables dynamic routing from:
- Database lookup
- Queue decision
- Time-based rule
Conditional Logic Using Variables
same => n,GotoIf($["${CALLERID(num)}"="9999"]?vip)
same => n(vip),Playback(welcome-vip)
same => n,Hangup()
This creates VIP routing.
Using Database Values as Variables
same => n,Set(USERNAME=${DB(users/${CALLERID(num)})})
same => n,NoOp(User from DB is ${USERNAME})
Used in:
- CRM lookup
- Blacklist checking
- Dynamic IVR personalization
Common Mistakes with Variables
- Forgetting ${} syntax
- Using variable before setting it
- Confusing channel vs global scope
- Not inheriting variables across Dial()
Debugging Variables in CLI
core set verbose 5
same => n,NoOp(VAR=${VAR_NAME})
NoOp is the fastest debugging tool in dialplan.
Real-World Production Example
[from-trunk]
exten => s,1,Set(__CALL_START=${EPOCH})
same => n,Set(__CALLER=${CALLERID(num)})
same => n,Dial(PJSIP/1001,20)
same => n,Set(CALL_END=${EPOCH})
same => n,NoOp(Call duration $[${CALL_END}-${CALL_START}])
same => n,Hangup()
This simple logic powers:
- Call analytics
- Billing engines
- AI reporting systems
Key Takeaway
Variables transform Asterisk from a simple PBX into a programmable telecom platform.
Mastering: Set(), built-in variables, inheritance, conditions, and database usage is essential for building modern call center and AI-driven telecom systems.
Want to see API-driven CRM + Telecom workflows in action? Try the WhatsApp bot or explore the demos.
Comments (0)
Be the first to comment.