Sample code for 30+ languages & platforms
Go Requires Chilkat v11.0.0+

SendGrid: Save and verify a topic opt-out

See more SendGrid Examples

Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.

Add group suppressions ยท Check specific addresses

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()

    //  Take the address from the authenticated account and the group ID from configuration.
    address := "reader@example.com"
    http.SetUrlVar("group","12345")

    success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",resp)
    if success != true {
        if resp.StatusCode() == 0 {
            fmt.Println(http.LastErrorText())
        } else {
            fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        }

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

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

    groupInfo := chilkat.NewJsonObject()
    success = groupInfo.Load(resp.BodyStr())
    if success != true {
        fmt.Println("Unexpected response: expected JSON.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        groupInfo.DisposeJsonObject()
        return
    }

    if groupInfo.StringOfEquals("name","Product updates",true) != true {
        fmt.Println("Unexpected unsubscribe group; no preference was changed.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        groupInfo.DisposeJsonObject()
        return
    }

    addresses := chilkat.NewJsonObject()
    addresses.UpdateString("recipient_emails[0]",address)

    success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",addresses,"application/json",resp)
    if success != true {
        if resp.StatusCode() == 0 {
            fmt.Println(http.LastErrorText())
        } else {
            fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        }

        http.DisposeHttp()
        resp.DisposeHttpResponse()
        groupInfo.DisposeJsonObject()
        addresses.DisposeJsonObject()
        return
    }

    if resp.StatusCode() != 201 {
        fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        groupInfo.DisposeJsonObject()
        addresses.DisposeJsonObject()
        return
    }

    success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"application/json",resp)
    if success != true {
        if resp.StatusCode() == 0 {
            fmt.Println(http.LastErrorText())
        } else {
            fmt.Println("HTTP ", resp.StatusCode(), ": ", resp.BodyStr())
        }

        http.DisposeHttp()
        resp.DisposeHttpResponse()
        groupInfo.DisposeJsonObject()
        addresses.DisposeJsonObject()
        return
    }

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

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

    if matches.Size() == 1 {
        fmt.Println("Confirmed opt-out: ", *matches.StringAt(0))
    } else {
        fmt.Println("Opt-out not confirmed. Recheck this group.")
    }


    http.DisposeHttp()
    resp.DisposeHttpResponse()
    groupInfo.DisposeJsonObject()
    addresses.DisposeJsonObject()
    matches.DisposeJsonArray()