Fetch Wallet Balance

Fetch your wallet balance by sending a HTTP GET request to the following endpoint:

Endpoints

Live: https://payments.africastalking.com/query/wallet/balance
Sandbox: https://payments.sandbox.africastalking.com/query/wallet/balance

Request Parameters

In addition to the standard request header, the body of the request should be a JSON object containing the following fields:

Parameter Description
username
String
Required
An Africa's Talking application username which represents the app whose payment wallet balance you would like to query.

Query Wallet Balance Response

The body of the response will be a JSON object containing the following fields:

Parameter Description
status
String
Status of the request. Possible values are:
  • Success - The request has been accepted.
  • Failed - The request has failed for some other reason; description field field will have more info in the case of an error.
balance
String
Balance of the payment wallet. The format of this string is: (3-digit Currency Code)(space)(Decimal Value) e.g KES 1.50
errorMessage
String
Optional
A message detailing what happened with a failed request.
{
    "status": "Success",
    "balance": "KES 47.7191"
}

Sample query wallet balance request code

The PHP code snippet below shows how to send a fetch wallet balance request.

The code uses our PHP SDK.

<?php
use AfricasTalking\SDK\AfricasTalking;

// Set your app credentials
$username = "MyAppsUsername";
$apikey   = "MyAppAPIKey";

// Initialize the SDK
$AT       = new AfricasTalking($username, $apiKey);

// Get the payments service
$payments = $AT->payments();

function fetchWalletBalance() {
    try {
        // Fetch the wallet balance
        $balance = $payments->fetchWalletBalance();

        print_r($balance);
    } catch(Exception $e) {
        echo "Error: ".$e.getMessage();
    }
}

fetchWalletBalance();

The JavaScript code snippet below shows how to fetch wallet balance using our API.

The code uses our Node SDK.



// Set your app credentials
const credentials = {
    apiKey: 'MyAppAPIkey',
    username: 'MyAppUsername'
}

// Initialize the SDK
const AfricasTalking = require('africastalking')(credentials);
// Get the payments service
const payments   = AfricasTalking.PAYMENTS;


function fetchBalance() {
    // Fetch the wallet balance
    payments.fetchWalletBalance()
        .then(response => {
            console.log(response);
        })
        .catch(error => {
            console.log(error);
        });
}

fetchBalance();

The Java code snippet below shows how to fetch wallet balance using our API.

The code uses our Java SDK.

/* Import SDK classes */
import com.africastalking.Callback;
import com.africastalking.AfricasTalking;
import com.africastalking.PaymentService;
import com.africastalking.payment.response.WalletBalanceResponse;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;

public class TestFetchWalletBalance
{
    public static void main(String[] args)
    {
		/* Your app credentials */
		String USERNAME = "sandbox";
		String API_KEY = "";

		/* Initialize SDK */
		AfricasTalking.initialize(USERNAME, API_KEY);

		/* Get an instance of the PaymentService */
        PaymentService payment = AfricasTalking.getService(AfricasTalking.SERVICE_PAYMENT);

		/* And query the balance (synchronously) */
		try {
            WalletBalanceResponse response = payment.fetchWalletBalance();
            System.out.println(response.toString());
		} catch(Exception ex) {
			ex.printStackTrace();
		}
   	}
}

The Python code snippet below shows how to fetch wallet balance using our API.

The code uses our Python SDK.

# works with both python 2 and 3
from __future__ import print_function

import africastalking

class PAYMENTS:
    def __init__(self):
		# Set your app credentials
        self.username = "YOUR_USERNAME"
        self.api_key = "YOUR_API_KEY"
		# Initialize the SDK
        africastalking.initialize(self.username, self.api_key)
		# Get the payments service
        self.payment = africastalking.Payment

    def wallet_balance(self):
        try:
			# Fetch the wallet balance
            res = self.payment.wallet_balance()
            print (res)
        except Exception as e:
            print ("Received error response:%s" %str(e))

if __name__ == '__main__':
    PAYMENTS().wallet_balance()

The Ruby code snippet below shows how to fetch wallet balance using our API.

The code uses our Ruby SDK.

require 'AfricasTalking'

# Your app credentials
username = "MyApp_Username"
apikey   = "MyApp_APIKey"

# Initialize SDK
AT=AfricasTalking::Initialize.new username, apikey

# Get an instance of the PaymentService
account = AT.payments

# And query the balance
begin
    walletDetails = account.fetchWalletBalance
    puts walletDetails.to_yaml
rescue AfricasTalking::AfricasTalkingException => ex
    puts 'Encountered an error: ' + ex.message
end