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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  reply: TJsonObject;
  parsed: Boolean;

begin
  success := False;

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

  resp := THttpResponse.Create;
  success := http.HttpNoBody('GET','https://platform.clickatell.com/v1/balance',resp);
  if (resp.StatusCode = 0) then
    begin
      WriteLn('No HTTP response. Check the connection and retry the lookup.');
      Exit;
    end;
  reply := TJsonObject.Create;
  parsed := reply.Load(resp.BodyStr);
  if ((success <> True) or ((resp.StatusCode <> 200))) then
    begin
      WriteLn('Clickatell returned HTTP ' + resp.StatusCode);
      if (parsed = True) then
        begin
          if (reply.HasMember('error.description') = True) then
            begin
              WriteLn(reply.StringOf('error.description'));
            end;
        end;
      Exit;
    end;
  if (parsed <> True) then
    begin
      WriteLn('Expected a JSON response.');
      Exit;
    end;

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


  http.Free;
  resp.Free;
  reply.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.