Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oReply
LOCAL nParsed

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
//  Supply the key for this One API integration from application configuration.
oHttp:SetRequestHeader("Authorization", "ONE_API_KEY")
oHttp:SetRequestHeader("Accept", "application/json")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://platform.clickatell.com/v1/balance", oResp)
IF (oResp:StatusCode == 0)
    ? "No HTTP response. Check the connection and retry the lookup."
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. ((oResp:StatusCode != 200)))
    ? "Clickatell returned HTTP " + Str(oResp:StatusCode)
    IF (nParsed == 1)
        IF (oReply:HasMember("error.description") == 1)
            ? oReply:StringOf("error.description")
        ENDIF

    ENDIF

    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nParsed != 1)
    ? "Expected a JSON response."
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

//  JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
IF ((oReply:JsonTypeOf("balance") != 2) .OR. (oReply:JsonTypeOf("currency") != 1))
    ? "The balance response is missing the amount or currency."
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "Account balance: " + oReply:StringOf("balance") + " " + oReply:StringOf("currency")

oHttp:destroy()
oResp:destroy()
oReply:destroy()