Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Clickatell One API: Read the account balance and currency

See more Clickatell Examples

Request the messaging balance associated with a One API integration key. The response supplies both the decimal amount and its currency. Read the amount as text for display to preserve its precision. A zero balance is a valid result. For example, a billing dashboard can display 1267.92865 ZAR.

Clickatell API reference

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        // Supply the key for this One API integration from application configuration.
        http.setRequestHeader("Authorization", "ONE_API_KEY");
        http.setRequestHeader("Accept", "application/json");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpNoBody("GET", "https://platform.clickatell.com/v1/balance", resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println("No HTTP response. Check the connection and retry the lookup.");
            return;
        }

        JsonObject reply = new JsonObject();
        boolean parsed;
        try {
            reply.load(resp.getBodyStr());
            parsed = true;
        } catch (ChilkatException e) {
            parsed = false;
        }
        if ((!success) || ((resp.getStatusCode() != 200))) {
            System.out.println("Clickatell returned HTTP " + resp.getStatusCode());
            if (parsed) {
                if (reply.hasMember("error.description")) {
                    System.out.println(reply.stringOf("error.description"));
                }
            }

            return;
        }

        if (!parsed) {
            System.out.println("Expected a JSON response.");
            return;
        }

        // JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
        if ((reply.jsonTypeOf("balance") != 2) || (reply.jsonTypeOf("currency") != 1)) {
            System.out.println("The balance response is missing the amount or currency.");
            return;
        }

        System.out.println("Account balance: " + reply.stringOf("balance") + " " + reply.stringOf("currency"));
    }
}