C++ Requires Chilkat v11.0.0+
C++
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.
Chilkat C++ Downloads
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkHttp http;
http.put_RequireSslCertVerify(true);
// Supply the key for this One API integration from application configuration.
http.SetRequestHeader("Authorization","ONE_API_KEY");
http.SetRequestHeader("Accept","application/json");
CkHttpResponse resp;
success = http.HttpNoBody("GET","https://platform.clickatell.com/v1/balance",resp);
if (resp.get_StatusCode() == 0) {
std::cout << "No HTTP response. Check the connection and retry the lookup." << "\r\n";
return;
}
CkJsonObject reply;
bool parsed = reply.Load(resp.bodyStr());
if ((success != true) || ((resp.get_StatusCode() != 200))) {
std::cout << "Clickatell returned HTTP " << resp.get_StatusCode() << "\r\n";
if (parsed == true) {
if (reply.HasMember("error.description") == true) {
std::cout << reply.stringOf("error.description") << "\r\n";
}
}
return;
}
if (parsed != true) {
std::cout << "Expected a JSON response." << "\r\n";
return;
}
// JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
if ((reply.JsonTypeOf("balance") != 2) || (reply.JsonTypeOf("currency") != 1)) {
std::cout << "The balance response is missing the amount or currency." << "\r\n";
return;
}
std::cout << "Account balance: " << reply.stringOf("balance") << " " << reply.stringOf("currency") << "\r\n";
}