Skip to main content

Mustache Template Parameters in cXML applications

Certain incoming message information can be extracted and added to an cXML application response by using parameters. Mustache templates are parameters in curly braces, like {{Name}}, which output the given value of a parameter.

Mustache template variables are used to make your cXML application dynamic. Variables can be parameters of the HTTP payload for the request, or a query string variable you set in your webhook (https://yourspace.signalwire.com/laml-bins/your-xml-bin-id?YOURVARIABLE=123). Either method of Mustache variable declaration will work well in translating values into your cXML application.

Which parameters work as mustache templates in an cXML application or webhook?

Type of ParameterDefault Parameter Names
General Parameters{{From}}, {{To}}, {{AccountSid}} (Equivalent to Project ID), {{APIVersion}}
Messaging Specific{{SmsSid}}, {{Body}}, {{NumSegments}} (Number of segments), {{NumMedia}} (Number of Media files)
Voice Specific{{CallSid}}, {{CallStatus}}, {{Direction}}, {{ParentCallSid}}
Inbound Domain Apps{{SipUser}}

Messaging Examples

In looking at how to forward an incoming message to another number, the mustache parameters {{From}} and {{Body}} can be retrieved and stored as variables to then be utilized in forwarding the message.

Please follow this knowledge base article for more information on forwarding an incoming message to another number:

Following a similar format to the above article to show all the available message parameters, an cXML application/webhook can use the below XML:

<Response>
<Message to='+1XXXXXXXXXX' from='+1XXXXXXXXXX'> Message received with the following details:
MessageSid:{{MessageSid}}
SmsSid:{{SmsSid}}
AccountSid:{{AccountSid}}
From:{{From}}
To:{{To}}
Body:{{Body}}
Number of Media:{{NumMedia}}
Media:{{Media}}
Number of Segments: {{NumSegments}}

</Message>
</Response>

Voice Examples

The following XML will result in all calls being forwarded to the specified number.

Notice what is declared in the third line, the {{From}} parameter is being assigned as the value of the callerId property.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="{{From}}">
<Number>+19074864171</Number>
</Dial>
</Response>

In this case, we are using the {{From}} value so that the caller ID for the forwarded call echoes the original calling number. In this way, your calls will have the correct caller identification.

Using Custom Mustache Templates

A custom mustache template can be created by adding the mustache variable within an cXML application/Webhook URL or a SIP Address.

With cXML Applications or Webhooks

Using the below XML in an XML Bin/Webhook, the Say verb will state the value of {{YOURVARIABLE}}. We will set this value in our webhook by amending the end of the URL. We can do this by following the syntax of ?YOURVARIABLE=YOURVALUE. Assuming we want to make the value of the variable equal to Something, our XML Bin URL or Webhook URL will look similar to: https://YOURSPACE.signalwire.com/laml-bins/xxxx-xxxx-xxxx-xxxx-xxxx?YOURVARIABLE=Something

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>{{YOURVARIABLE}}</Say>
</Response>
Mustache Variable

Remember that after copying and pasting the XML bin URL into the phone setting options, manually add the mustache variable to the end of the URL to create and declare your custom variable.

For example, to set the variable to 123, we would enter it as '?YOURVARIABLE=123' to the end of your URL.

With Domain Applications

When calling a Domain Application that handles incoming calls with a LaML Webhook, you can still pass the custom mustache variable in the SIP URL, much like you would with Webhooks, by adding ?YOURVARIABLE=YOURVALUE at the end: sip:123456789@YOURSPACE-EXAMPLE.dapp.signalwire.com?YOURVARIABLE=Something.

Assuming the Domain App calls the same XML Bin/Webhook as the one we outlined above, we just need to prefix the variable name with SipHeader_X-, making the end result SipHeader_X-YOURVARIABLE.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>{{SipHeader_X-YOURVARIABLE}}</Say>
</Response>

This is particularly useful in BYOC setups where you wish to send calls to SignalWire and adjust the FROM number on the fly using a custom variable:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="{{SipHeader_X-FROM_NUMBER}}">
<Number>{{SipUser}}</Number>
</Dial>
</Response>

If you wished to call +1111111111 from +123456789, your SIP URL would look like this:

sip:+1111111111@YOURSPACE-EXAMPLE.dapp.signalwire.com?FROM_NUMBER=%2B123456789

URL Encoding

Notice the encoded + in the FROM_NUMBER, as %2B? Since it is a reserved character, we need to encode it when passing it in the URL. To learn more about URL Encoding, please read this page.

Further References

For more information about SignalWire cXML and Mustache templates, please review this SignalWire resource HERE.

For further information about the functions of mustache templates, please read HERE.