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

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_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");

let resp = chilkat::HttpResponse::new();
success = http.http_no_body("GET", "https://platform.clickatell.com/v1/balance", &resp).is_ok();
if resp.status_code() == 0 {
    println!("No HTTP response. Check the connection and retry the lookup.");
    return;
}

let reply = chilkat::JsonObject::new();
let parsed = reply.load(&resp.body_str()).is_ok();
if (!success) || ((resp.status_code() != 200)) {
    println!("Clickatell returned HTTP {}", resp.status_code());
    if parsed {
        if reply.has_member("error.description") {
            println!("{}", reply.string_of("error.description").unwrap_or_default());
        }

    }

    return;
}

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

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

println!("Account balance: {} {}", reply.string_of("balance").unwrap_or_default(), reply.string_of("currency").unwrap_or_default());