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

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false
    http := chilkat.NewHttp()
    http.SetRequireSslCertVerify(true)
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    http.SetAuthToken("SENDGRID_API_KEY")
    resp := chilkat.NewHttpResponse()

    day := chilkat.NewCkDateTime()
    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 {
            fmt.Println(http.LastErrorText())
        } else {
            fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        }

        http.DisposeHttp()
        resp.DisposeHttpResponse()
        day.DisposeCkDateTime()
        return
    }

    if resp.StatusCode() != 200 {
        fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        day.DisposeCkDateTime()
        return
    }

    days := chilkat.NewJsonArray()
    success = days.Load(resp.BodyStr())
    if success != true {
        fmt.Println("Unexpected response: expected JSON.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        day.DisposeCkDateTime()
        days.DisposeJsonArray()
        return
    }

    fmt.Println("Date,Requests,Delivered,Bounces,Blocks")
    row := chilkat.NewJsonObject()
    i := 0
    for (i < days.Size()) {
        days.ObjectAt2(i,row)
        fmt.Println(*row.StringOf("date"), ",", row.IntOf("stats[0].metrics.requests"), ",", row.IntOf("stats[0].metrics.delivered"), ",", row.IntOf("stats[0].metrics.bounces")
            , ",", row.IntOf("stats[0].metrics.blocks"))
        i = i + 1
    }


    http.DisposeHttp()
    resp.DisposeHttpResponse()
    day.DisposeCkDateTime()
    days.DisposeJsonArray()
    row.DisposeJsonObject()