Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

SendGrid: Report daily requests, deliveries, bounces, and blocks

See more SendGrid Examples

Retrieve seven completed UTC dates and print one row per date. The counts are account-level aggregates; a day’s delivered count does not identify which messages sent that day were delivered.

Global email statistics

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.JsonArray,
  Chilkat.HttpResponse,
  Chilkat.CkDateTime,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  day: TCkDateTime;
  days: TJsonArray;
  row: TJsonObject;
  i: Integer;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  success := False;
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
  http.AuthToken := 'SENDGRID_API_KEY';
  resp := THttpResponse.Create;

  day := TCkDateTime.Create;
  day.SetFromCurrentSystemTime();
  day.AddDays(-1);
  http.SetUrlVar('end',day.GetAsIso8601('YYYY-MM-DD',False));
  day.AddDays(-6);
  http.SetUrlVar('start',day.GetAsIso8601('YYYY-MM-DD',False));

  success := http.HttpNoBody('GET','https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day',resp);
  if (success <> True) then
    begin
      if (resp.StatusCode = 0) then
        begin
          WriteLn(http.LastErrorText);
        end
      else
        begin
          WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
        end;
      Exit;
    end;
  if (resp.StatusCode <> 200) then
    begin
      WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
      Exit;
    end;

  days := TJsonArray.Create;
  success := days.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Unexpected response: expected JSON.');
      Exit;
    end;
  WriteLn('Date,Requests,Delivered,Bounces,Blocks');
  row := TJsonObject.Create;
  i := 0;
  while (i < days.Size) do
    begin
      days.ObjectAt2(i,row);
      WriteLn(row.StringOf('date') + ',' + row.IntOf('stats[0].metrics.requests') + ',' + row.IntOf('stats[0].metrics.delivered') + ',' + row.IntOf('stats[0].metrics.bounces')
           + ',' + row.IntOf('stats[0].metrics.blocks'));
      i := i + 1;
    end;



  http.Free;
  resp.Free;
  day.Free;
  days.Free;
  row.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.