Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
day: HCkDateTime;
days: HCkJsonArray;
row: HCkJsonObject;
i: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_putAuthToken(http,'SENDGRID_API_KEY');
resp := CkHttpResponse_Create();
day := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(day);
CkDateTime_AddDays(day,-1);
CkHttp_SetUrlVar(http,'end',CkDateTime__getAsIso8601(day,'YYYY-MM-DD',False));
CkDateTime_AddDays(day,-6);
CkHttp_SetUrlVar(http,'start',CkDateTime__getAsIso8601(day,'YYYY-MM-DD',False));
success := CkHttp_HttpNoBody(http,'GET','https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 200) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
days := CkJsonArray_Create();
success := CkJsonArray_Load(days,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
Memo1.Lines.Add('Date,Requests,Delivered,Bounces,Blocks');
row := CkJsonObject_Create();
i := 0;
while (i < CkJsonArray_getSize(days)) do
begin
CkJsonArray_ObjectAt2(days,i,row);
Memo1.Lines.Add(CkJsonObject__stringOf(row,'date') + ',' + IntToStr(CkJsonObject_IntOf(row,'stats[0].metrics.requests')) + ',' + IntToStr(CkJsonObject_IntOf(row,'stats[0].metrics.delivered'))
+ ',' + IntToStr(CkJsonObject_IntOf(row,'stats[0].metrics.bounces')) + ',' + IntToStr(CkJsonObject_IntOf(row,'stats[0].metrics.blocks')));
i := i + 1;
end;
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkDateTime_Dispose(day);
CkJsonArray_Dispose(days);
CkJsonObject_Dispose(row);