Get user balance

You need your AfricasTalking username and APIKey for any request to our API. If you don't have an APIKey, you can generate one from your applications settings page.

The PHP code snippet below shows how to get user balance from our API.

The code uses our PHP gateway class: AfricasTalkingGateway.php [ Download]

Also, please ensure that you have the php_curl module enabled. To enable it uncomment the line: ;extension=php_curl.dll in php.ini and restart apache

// Be sure to include the file you've just downloaded

// Specify your login credentials
$username   = "MyAppUsername";
$apikey     = "MyAppApiKey";

// Create a new instance of our awesome gateway class
$gateway    = new AfricasTalkingGateway($username, $apikey);

// Any gateway errors will be captured by our custom Exception class below, 
// so wrap the call in a try-catch block
  // Fetch the data from our USER resource and read the balance
  $data = $gateway->getUserData();
  echo "Balance: " . $data->balance."\n";
  // The result will have the format=> KES XXX
catch ( AfricasTalkingGatewayException $e )
  echo "Encountered an error while fetching user data: ".$e->getMessage()."\n";

// DONE!!! 

To access your data (eg. balance from our API), you will need to make a HTTP GET request to the following url:
The server shall expect:
Parameter Location Description Required
API key Header API key generated from your account settings Yes
Accept Header This is the format you would like your data formatted. It may be application/xml or application/json. The default is application/xml. The gateway classes provided uses appliaction/json. No
username URL This is your Africa'sTalking username Yes
Upon successful processing, the server shall respond in the following format:
<?xml version="1.0"?>
      <balance> XXX</balance>

{"UserData" :
    "balance" : " XXX"
Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:myAPIKey" "" --insecure