Template functions
Reference for built-in transformation functions
Template functions provide simple text transformations for common operations like converting to lowercase, URL encoding, and date formatting. They complement JavaScript expressions by handling specific formatting tasks that don’t require complex logic.
For information about variable scopes, see the Variables Reference. For JavaScript expressions and data manipulation, see the Expressions Reference.
Template functions are only available in SWAIG (SignalWire AI Gateway) contexts, specifically within:
- AI function
data_mapprocessing (expressions, webhooks, output) - Webhook responses to SWAIG functions
- AI prompt variable expansion
They are not available in regular SWML methods or general variable contexts. For regular SWML variable manipulation, use JavaScript expressions with the ${expression} syntax instead.
Reference
lc
Converts a string to lowercase. Commonly used to normalize user input for case-insensitive comparisons or ensure consistent casing when accessing object properties dynamically.
Syntax: ${lc:<value>}
Example:
enc:url
Encodes a string for safe use in URLs by converting special characters to percent-encoded equivalents. Always use this when including variables in URL query parameters or paths to prevent special characters from breaking URLs or causing unexpected behavior.
Syntax: ${enc:url:<value>}
Example:
strftime_tz
Formats the current date and time using standard strftime format codes with timezone support. This generates timestamps at the moment the template is evaluated, not when the SWML script was created.
Syntax: @{strftime_tz <timezone> <format>}
Common format codes:
Example:
fmt_ph
Formats a phone number using specified international format standards. Supports multiple format types for different use cases, with optional separators for improved text-to-speech pronunciation.
Syntax: @{fmt_ph <format> <phone_number>} or @{fmt_ph <format>:sep:<separator> <phone_number>}
Available formats:
national- National format (default)international- International format with country codeRFC3966- RFC 3966 format (tel: URI)e164- E.164 format (+1234567890)
Example:
expr
Evaluates simple arithmetic expressions with literal numbers. Supports addition, subtraction, multiplication, division, and parentheses for grouping. Only works with literal numbers and cannot reference variables.
Syntax: @{expr <expression>}
Example:
echo
Returns the argument unchanged. Primarily useful for debugging template evaluation or forcing explicit variable expansion in complex nested scenarios.
Syntax: @{echo <text>}
Example:
separate
Inserts spaces between each character in a string to improve text-to-speech pronunciation. Particularly useful for spelling out confirmation codes, license plates, serial numbers, or any text that should be read character-by-character.
Syntax: @{separate <text>}
Example:
In this example, if code is “ABC123”, the AI will pronounce “A B C 1 2 3” instead of trying to say “ABC123” as a word.
sleep
Pauses execution for the specified number of seconds. Can be used for rate limiting, timing coordination, or testing purposes.
Syntax: @{sleep <seconds>}
Use sparingly in production environments. Excessive delays can cause timeouts, impact call quality, and degrade user experience. Best suited for development, testing, or specific rate-limiting scenarios.
Example:
Function chaining
Prefix functions (using ${...} syntax) can be chained together to apply multiple transformations in sequence. The transformations are applied from left to right.
Syntax: ${func1:func2:func3:<value>}
Example: