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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
#  Supply the key for this One API integration from application configuration.
CkHttp_SetRequestHeader $http "Authorization" "ONE_API_KEY"
CkHttp_SetRequestHeader $http "Accept" "application/json"

set resp [new_CkHttpResponse]

set success [CkHttp_HttpNoBody $http "GET" "https://platform.clickatell.com/v1/balance" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "No HTTP response. Check the connection and retry the lookup."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    exit
}

set reply [new_CkJsonObject]

set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1]  ||  [[CkHttpResponse_get_StatusCode $resp] != 200]} then {
    puts "Clickatell returned HTTP [CkHttpResponse_get_StatusCode $resp]"
    if {$parsed == 1} then {
        if {[CkJsonObject_HasMember $reply "error.description"] == 1} then {
            puts [CkJsonObject_stringOf $reply error.description]
        }

    }

    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {$parsed != 1} then {
    puts "Expected a JSON response."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

#  JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
if {expr [[CkJsonObject_JsonTypeOf $reply "balance"] != 2]  ||  [[CkJsonObject_JsonTypeOf $reply "currency"] != 1]} then {
    puts "The balance response is missing the amount or currency."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

puts "Account balance: [CkJsonObject_stringOf $reply balance] [CkJsonObject_stringOf $reply currency]"

delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply