AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
; Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$oHttp.AuthToken = "SENDGRID_API_KEY"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oDay = ObjCreate("Chilkat.CkDateTime")
$oDay.SetFromCurrentSystemTime()
$oDay.AddDays(-1)
$oHttp.SetUrlVar("end",$oDay.GetAsIso8601("YYYY-MM-DD",False))
$oDay.AddDays(-6)
$oHttp.SetUrlVar("start",$oDay.GetAsIso8601("YYYY-MM-DD",False))
$bSuccess = $oHttp.HttpNoBody("GET","https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$oDays = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oDays.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
Exit
EndIf
ConsoleWrite("Date,Requests,Delivered,Bounces,Blocks" & @CRLF)
$oRow = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oDays.Size)
$oDays.ObjectAt2($i,$oRow)
ConsoleWrite($oRow.StringOf("date") & "," & $oRow.IntOf("stats[0].metrics.requests") & "," & $oRow.IntOf("stats[0].metrics.delivered") & "," & $oRow.IntOf("stats[0].metrics.bounces") _
& "," & $oRow.IntOf("stats[0].metrics.blocks") & @CRLF)
$i = $i + 1
Wend