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

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
//  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
Dim resp As New Chilkat.HttpResponse

Dim day As New Chilkat.CkDateTime
success = day.SetFromCurrentSystemTime()
success = day.AddDays(-1)
success = http.SetUrlVar("end",day.GetAsIso8601("YYYY-MM-DD",False))
success = day.AddDays(-6)
success = 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
    If (resp.StatusCode = 0) Then
        System.DebugLog(http.LastErrorText)
    Else
        System.DebugLog("HTTP " + Str(resp.StatusCode) + ": " + resp.BodyStr)
    End If

    Return
End If

If (resp.StatusCode <> 200) Then
    System.DebugLog("HTTP " + Str(resp.StatusCode) + ": " + resp.BodyStr)
    Return
End If

Dim days As New Chilkat.JsonArray
success = days.Load(resp.BodyStr)
If (success <> True) Then
    System.DebugLog("Unexpected response: expected JSON.")
    Return
End If

System.DebugLog("Date,Requests,Delivered,Bounces,Blocks")
Dim row As New Chilkat.JsonObject
Dim i As Int32
i = 0
While (i < days.Size)
    success = days.ObjectAt2(i,row)
    System.DebugLog(row.StringOf("date") + "," + Str(row.IntOf("stats[0].metrics.requests")) + "," + Str(row.IntOf("stats[0].metrics.delivered")) + "," + Str(row.IntOf("stats[0].metrics.bounces")) _
         + "," + Str(row.IntOf("stats[0].metrics.blocks")))
    i = i + 1
Wend