Variables
Use dynamic content and expressions in your USSD flows.
Variables
Variables allow you to create dynamic, personalized USSD experiences.
Syntax
Use ${variableName} to reference a variable:
Welcome, ${name}!
Your balance is ${balance} GHS.
Creating Variables
Variables are created when:
- User input - Input nodes store responses in a variable you name
- Action results - API responses saved to variables
- Set variable - Explicitly set using an Action node
User Input Variables
When you add an Input node, you specify a variable name. The user's response is stored there.
Example:
- Input prompt: "Enter your name:"
- Variable name:
name - User enters: "John"
- Result:
${name}= "John"
System Variables
Available automatically in every session:
| Variable | Description | Example |
|---|---|---|
$phone | User's phone number | +233201234567 |
$sessionId | Current session ID | sess_abc123 |
$timestamp | Current Unix timestamp | 1703436800 |
$provider | USSD provider name | africas_talking |
$shortcode | Dialed shortcode | *123# |
Computed Variables
Use an Action node with "Set Variable" to compute values:
| Variable Name | Expression |
|---|---|
total | amount + fee |
fee | amount * 0.01 |
Arithmetic
| Operator | Description | Example |
|---|---|---|
+ | Addition | amount + fee |
- | Subtraction | balance - amount |
* | Multiplication | price * quantity |
/ | Division | total / 2 |
% | Modulo | index % 2 |
String Operations
| Function | Description | Example |
|---|---|---|
concat() | Join strings | concat(firstName, " ", lastName) |
upper() | Uppercase | upper(name) |
lower() | Lowercase | lower(email) |
substr() | Substring | substr(phone, 0, 4) |
length() | String length | length(input) |
Comparison
| Operator | Description | Example |
|---|---|---|
== | Equals | status == "active" |
!= | Not equals | type != "guest" |
> | Greater than | balance > 100 |
>= | Greater or equal | age >= 18 |
< | Less than | attempts < 3 |
<= | Less or equal | count <= 5 |
In Menu Titles
Welcome ${name}! Balance: ${balance} GHS
User sees:
Welcome John! Balance: 500 GHS
1. Send Money
2. Check Balance
In Display Messages
Transfer Summary:
Amount: ${amount} GHS
Fee: ${fee} GHS
Total: ${total} GHS
To: ${recipientName}
In Branch Conditions
| Condition | Then go to |
|---|---|
balance >= total | "Proceed" node |
| (default) | "Insufficient Funds" node |
Working with API Responses
When an Action node makes an HTTP request, the response is stored in a variable.
Accessing Response Data
If your API returns:
{
"user": {
"name": "John",
"balance": 500
},
"status": "success"
}
Access nested properties with dot notation:
| Expression | Result |
|---|---|
${response.user.name} | "John" |
${response.user.balance} | 500 |
${response.status} | "success" |
Array Access
For arrays, use index notation:
| Expression | Result |
|---|---|
${transactions[0].amount} | First transaction amount |
${items[2].name} | Third item name |
Default Values
Provide fallback values for undefined variables:
| Expression | Result |
|---|---|
${name|Guest} | Uses "Guest" if name is undefined |
${balance|0} | Uses 0 if balance is undefined |
Numbers
| Expression | Result |
|---|---|
${amount|number:2} | "1,234.56" (2 decimal places) |
${balance|currency:GHS} | "GHS 500.00" |
Dates
| Expression | Result |
|---|---|
${timestamp|date:short} | "12/25/23" |
${timestamp|date:long} | "December 25, 2023" |
${timestamp|time} | "14:30" |
Phone Numbers
| Expression | Result |
|---|---|
${phone|phone:local} | "020 123 4567" |
${phone|phone:intl} | "+233 20 123 4567" |
Naming Conventions
Use clear, descriptive names:
| Good | Bad |
|---|---|
userName | x |
accountBalance | temp |
transferAmount | data1 |
Validation
Always validate user input. In Input nodes, set validation rules:
| Validation Type | Settings |
|---|---|
| Number | Min: 1, Max: 10000 |
| Phone | Country: GH |
| Text | Min length: 3 |

