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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply the key for this One API integration from application configuration.
http:set_request_header("Authorization", "ONE_API_KEY")
http:set_request_header("Accept", "application/json")

local resp = chilkat.HttpResponse.new()
local success = http:http_no_body("GET", "https://platform.clickatell.com/v1/balance", resp)
if resp.status_code == 0 then
    print("No HTTP response. Check the connection and retry the lookup.")
    return
end

local reply = chilkat.JsonObject.new()
local parsed = reply:load(resp.body_str)
if (not success) or ((resp.status_code ~= 200)) then
    print("Clickatell returned HTTP " .. resp.status_code)
    if parsed then
        if reply:has_member("error.description") then
            print(reply:string_of("error.description"))
        end
    end

    return
end

if not parsed then
    print("Expected a JSON response.")
    return
end

-- JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
if (reply:json_type_of("balance") ~= 2) or (reply:json_type_of("currency") ~= 1) then
    print("The balance response is missing the amount or currency.")
    return
end

print("Account balance: " .. reply:string_of("balance") .. " " .. reply:string_of("currency"))