Overview
The USSD webhook endpoint handles interactive USSD sessions, allowing users to manage contracts, confirm deliveries, and check payments through their mobile phones without internet access.Endpoint
How USSD Works
USSD (Unstructured Supplementary Service Data) is a session-based protocol:- User dials the USSD code (e.g.,
*384*1234#) - Africa’s Talking sends a POST request to your webhook
- Your server responds with menu text prefixed by
CON(continue) orEND(terminate) - User makes a selection and the process repeats until session ends
Webhook Payload
Africa’s Talking sends form-encoded data with these fields:| Field | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | Unique session identifier |
serviceCode | string | Yes | USSD code dialed (e.g., *384*1234#) |
phoneNumber | string | Yes | User’s phone number |
text | string | No | User’s input history (empty for first request) |
Understanding the text Field
The text field contains the user’s navigation path through the menu:
- Empty string: First request (show main menu)
- “1”: User selected option 1 from main menu
- “1*2”: User selected 1, then 2 from the submenu
- “123”: User’s full navigation path
Example Payloads
Initial Request (Main Menu)
User Selection (Navigate to Contracts)
Deep Navigation (Select Contract)
Response Format
Continue Session (CON)
Show menu and wait for user input:End Session (END)
Terminate the session:Implementation
The USSD handler manages session state and menu navigation:Menu Structure
Main Menu
Contracts Menu
Displays user’s active contracts:Contract Details Menu
Shows contract details and actions:Delivery Confirmation Menu
Session Management
Session Storage
Store session state in the database to maintain context:Get or Create Session
USSD Helper Functions
Build Response
Parse Input Path
Security Considerations
IP Whitelisting
Restrict USSD webhook to Africa’s Talking IPs:Input Validation
Validate all user inputs:Session Expiry
Clean up expired sessions:Best Practices
- Keep Menus Short: Maximum 8 options per menu for better UX
- Clear Text: Use simple, concise language (max 160 characters)
- Fast Response: Respond within 30 seconds to avoid timeout
- Error Handling: Always provide fallback messages
- Session Timeout: Default 5 minutes, warn users before expiry
- Navigation: Always provide “0. Back” and exit options
Testing
Simulator
Use Africa’s Talking USSD simulator:- Go to USSD Simulator
- Enter your phone number
- Dial your USSD code (e.g.,
*384*1234#) - Test menu navigation
Local Testing
Expected Response (Main Menu)
Configuration
Set your USSD webhook URL in Africa’s Talking dashboard:- Log in to Africa’s Talking Dashboard
- Navigate to USSD > Service Codes
- Select your service code
- Set Callback URL:
https://your-domain.com/api/v1/ussd/ - Save and test
Monitoring
Track Session Metrics
Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Timeout | Slow database queries | Optimize queries, add indexes |
| Invalid response | Wrong prefix (CON/END) | Verify response format |
| Session lost | Database not persisting | Check session storage logic |
| Menu loop | Wrong navigation logic | Add breadcrumb tracking |
Related Endpoints
- USSD Test - Test USSD menu generation
- Contracts API - Manage contracts
- SMS Webhook - Handle SMS notifications