Variables

Use dynamic content and expressions in your USSD flows.

Variables

Variables allow you to create dynamic, personalized USSD experiences.

Basics

Variables store data during a session and can be used in node content.

Syntax

Use ${variableName} to reference a variable:

Welcome, ${name}!
Your balance is ${balance} GHS.

Creating Variables

Variables are created when:

  1. User input - Input nodes store responses in a variable you name
  2. Action results - API responses saved to variables
  3. Set variable - Explicitly set using an Action node

Variable Types

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:

VariableDescriptionExample
$phoneUser's phone number+233201234567
$sessionIdCurrent session IDsess_abc123
$timestampCurrent Unix timestamp1703436800
$providerUSSD provider nameafricas_talking
$shortcodeDialed shortcode*123#

Computed Variables

Use an Action node with "Set Variable" to compute values:

Variable NameExpression
totalamount + fee
feeamount * 0.01

Expressions

Use expressions for dynamic values and conditions.

Arithmetic

OperatorDescriptionExample
+Additionamount + fee
-Subtractionbalance - amount
*Multiplicationprice * quantity
/Divisiontotal / 2
%Moduloindex % 2

String Operations

FunctionDescriptionExample
concat()Join stringsconcat(firstName, " ", lastName)
upper()Uppercaseupper(name)
lower()Lowercaselower(email)
substr()Substringsubstr(phone, 0, 4)
length()String lengthlength(input)

Comparison

OperatorDescriptionExample
==Equalsstatus == "active"
!=Not equalstype != "guest"
>Greater thanbalance > 100
>=Greater or equalage >= 18
<Less thanattempts < 3
<=Less or equalcount <= 5

Logical

OperatorDescriptionExample
&&Andage >= 18 && verified
||Oradmin || manager
!Not!expired

Using Variables in Nodes

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

ConditionThen go to
balance >= total"Proceed" node
(default)"Insufficient Funds" node

In API URLs

https://api.example.com/users/${phone}

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:

ExpressionResult
${response.user.name}"John"
${response.user.balance}500
${response.status}"success"

Array Access

For arrays, use index notation:

ExpressionResult
${transactions[0].amount}First transaction amount
${items[2].name}Third item name

Default Values

Provide fallback values for undefined variables:

ExpressionResult
${name|Guest}Uses "Guest" if name is undefined
${balance|0}Uses 0 if balance is undefined

Formatting

Numbers

ExpressionResult
${amount|number:2}"1,234.56" (2 decimal places)
${balance|currency:GHS}"GHS 500.00"

Dates

ExpressionResult
${timestamp|date:short}"12/25/23"
${timestamp|date:long}"December 25, 2023"
${timestamp|time}"14:30"

Phone Numbers

ExpressionResult
${phone|phone:local}"020 123 4567"
${phone|phone:intl}"+233 20 123 4567"

Best Practices

Naming Conventions

Use clear, descriptive names:

GoodBad
userNamex
accountBalancetemp
transferAmountdata1

Validation

Always validate user input. In Input nodes, set validation rules:

Validation TypeSettings
NumberMin: 1, Max: 10000
PhoneCountry: GH
TextMin length: 3

Error Handling

Use Branch nodes to check for undefined variables before using them:

ConditionThen go to
balance != undefined"Show Balance" node
(default)"Fetch Balance" node