Get user balance

You need your AfricasTalking username and APIKey for any request to our API. If you don't have an APIKey, please click here to get one.

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    : 'api.africastalking.com',
		 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.setEncoding('utf8');
    	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
    			console.log(logStr);
    		}
    		catch (error) {
    			console.log('Error: ' + error);
    		}
    	});
    });
	request.end();
	
}

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


To access your data (eg. balance from our API), you will need to make a HTTP GET request to the following url:
GET URL: https://api.africastalking.com/version1/user
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
<?xml version="1.0"?>
  <AfricastalkingResponse>
    <UserData>
      <balance> XXX</balance>
    </UserData>
</AfricastalkingResponse>
JSON

{"UserData" :
  {
    "balance" : " XXX"
  }
}
Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:myAPIKey" "https://api.africastalking.com/version1/user?username=myUsername" --insecure