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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
parsed: Boolean;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
//  Supply the key for this One API integration from application configuration.
CkHttp_SetRequestHeader(http,'Authorization','ONE_API_KEY');
CkHttp_SetRequestHeader(http,'Accept','application/json');

resp := CkHttpResponse_Create();
success := CkHttp_HttpNoBody(http,'GET','https://platform.clickatell.com/v1/balance',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add('No HTTP response. Check the connection and retry the lookup.');
    Exit;
  end;
reply := CkJsonObject_Create();
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((success <> True) or ((CkHttpResponse_getStatusCode(resp) <> 200))) then
  begin
    Memo1.Lines.Add('Clickatell returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if (parsed = True) then
      begin
        if (CkJsonObject_HasMember(reply,'error.description') = True) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.description'));
          end;
      end;
    Exit;
  end;
if (parsed <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;

//  JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
if ((CkJsonObject_JsonTypeOf(reply,'balance') <> 2) or (CkJsonObject_JsonTypeOf(reply,'currency') <> 1)) then
  begin
    Memo1.Lines.Add('The balance response is missing the amount or currency.');
    Exit;
  end;
Memo1.Lines.Add('Account balance: ' + CkJsonObject__stringOf(reply,'balance') + ' ' + CkJsonObject__stringOf(reply,'currency'));

CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);