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 Node.js code snippet below shows how to get user balance from our API.

// Import the required libraries
var https = require('https');

// Your login credentials
var username = 'MyUsername';
var apikey   = 'MyApiKey';

function getUserData() {
	//create request parameters
	var request_parameters = {
		 host    : '',
		 port    : 443,
		 path    : '/version1/user?username=' + username,
		 method  : 'GET',
		 rejectUnauthorized : false,
		 requestCert        : true,
		 agent              : false,
		 headers : {
		 	 'apikey' : apikey,
		 	 'Accept' : 'application/json'
		var request = https.request(request_parameters, function (response) {
    	response.on('data', function (data_chunk) {
    		try {
    			if (response.statusCode != 200)
    				throw data_chunk;
    			var jsObject = JSON.parse(data_chunk);
    			var userData = jsObject.UserData;
    			var logStr   = 'Balance : ' + jsObject.balance; // The result will have the format=> KES XXX
    		catch (error) {
    			console.log('Error: ' + error);

console.log("Call get user function");

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