Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
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.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
day: TCkDateTime;
days: TChilkatJsonArray;
row: TChilkatJsonObject;
i: Integer;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken := 'SENDGRID_API_KEY';
resp := TChilkatHttpResponse.Create(Self);
day := TCkDateTime.Create(Self);
day.SetFromCurrentSystemTime();
day.AddDays(-1);
http.SetUrlVar('end',day.GetAsIso8601('YYYY-MM-DD',0));
day.AddDays(-6);
http.SetUrlVar('start',day.GetAsIso8601('YYYY-MM-DD',0));
success := http.HttpNoBody('GET','https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
days := TChilkatJsonArray.Create(Self);
success := days.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
Memo1.Lines.Add('Date,Requests,Delivered,Bounces,Blocks');
row := TChilkatJsonObject.Create(Self);
i := 0;
while (i < days.Size) do
begin
days.ObjectAt2(i,row.ControlInterface);
Memo1.Lines.Add(row.StringOf('date') + ',' + IntToStr(row.IntOf('stats[0].metrics.requests')) + ',' + IntToStr(row.IntOf('stats[0].metrics.delivered')) + ','
+ IntToStr(row.IntOf('stats[0].metrics.bounces')) + ',' + IntToStr(row.IntOf('stats[0].metrics.blocks')));
i := i + 1;
end;