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

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    let http = CkoHttp()!
    http.requireSslCertVerify = true
    //  Supply the key for this One API integration from application configuration.
    http.setRequestHeader(name: "Authorization", value: "ONE_API_KEY")
    http.setRequestHeader(name: "Accept", value: "application/json")

    let resp = CkoHttpResponse()!
    success = http.httpNoBody(verb: "GET", url: "https://platform.clickatell.com/v1/balance", response: resp)
    if resp.statusCode.intValue == 0 {
        print("No HTTP response. Check the connection and retry the lookup.")
        return
    }

    let reply = CkoJsonObject()!
    var parsed: Bool = reply.load(json: resp.bodyStr)
    if (success != true) || ((resp.statusCode.intValue != 200)) {
        print("Clickatell returned HTTP \(resp.statusCode.intValue)")
        if parsed == true {
            if reply.hasMember(jsonPath: "error.description") == true {
                print("\(reply.string(of: "error.description")!)")
            }

        }

        return
    }

    if parsed != true {
        print("Expected a JSON response.")
        return
    }

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

    print("Account balance: \(reply.string(of: "balance")!) \(reply.string(of: "currency")!)")

}