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 Java code snippet below shows how you can get your Africa's Talking account balance using our API.

The code uses our Java gateway class: AfricasTalkingGateway.java [ Download]

To help decode JSON responses, you will need to add one of the available JAVA libraries as a dependency. For this tutorial we chose to use json-20090211.jar which you can download from this repository.

Note On SSL Certificate Validation Issues

Some of our users have experienced SSL Certificate validation issues when connecting to our website due to our use of https. The error you see may look something like this:

Unable to find valid certification path to requested target

Please do not panic! Our certificate is perfectly valid (as evidenced by these verification tests), and the error you are seeing is quite common amongst Java clients connecting to secure websites. Here is how to fix the issue:

  • Download and compile InstallCert.java (you can find the code here).
  • On the CMD prompt(Windows) or Terminal(*nix), run: java InstallCert api.africastalking.com
    This will download a copy of our Certificate and create a file named jssecacerts in the current directory.
  • You can now add our Certificate to your trusted store by copying the created jssecacerts file to the Java security directory under $JAVA_HOME/jre/lib/security
// Make sure the downloaded jar file is in the classpath

import org.json.*;

public class TestCheckBalance 
{

	public static void main(String[] args) 
	{
		
		//Specify your credentials
		String username = "myAfricasTalkingUsername";
		String apiKey = "myAfricasTalkingAPIKey";
		
		//Create an instance of our awesome gateway class and pass your credentials
		AfricasTalkingGateway gateway = new AfricasTalkingGateway(username, apiKey);

		// Thats it, hit send and we'll take care of the rest. Any errors will
   // be captured in the Exception class as shown below
   
   try {
   	JSONObject result = gateway.getUserData();
   	System.out.println(result.getString("balance"));
   	//The result will have the format=> KES XXX
   }
   
   catch(Exception e){
   	System.out.println(e.getMessage());
   }
	}
}

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