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

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
//  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();

Chilkat.CkDateTime day = new Chilkat.CkDateTime();
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) {
    if (resp.StatusCode == 0) {
        Debug.WriteLine(http.LastErrorText);
    }
    else {
        Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
    }

    return;
}

if (resp.StatusCode != 200) {
    Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
    return;
}

Chilkat.JsonArray days = new Chilkat.JsonArray();
success = days.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Unexpected response: expected JSON.");
    return;
}

Debug.WriteLine("Date,Requests,Delivered,Bounces,Blocks");
Chilkat.JsonObject row = new Chilkat.JsonObject();
int i = 0;
while ((i < days.Size)) {
    days.ObjectAt2(i,row);
    Debug.WriteLine(row.StringOf("date") + "," + Convert.ToString(row.IntOf("stats[0].metrics.requests")) + "," + Convert.ToString(row.IntOf("stats[0].metrics.delivered"))
         + "," + Convert.ToString(row.IntOf("stats[0].metrics.bounces")) + "," + Convert.ToString(row.IntOf("stats[0].metrics.blocks")));
    i = i + 1;
}