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

AutoIt
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")

;  Take the address from the authenticated account and the group ID from configuration.
Local $sAddress = "reader@example.com"
$oHttp.SetUrlVar("group","12345")

$bSuccess = $oHttp.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",$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

$oGroupInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oGroupInfo.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
    Exit
EndIf

If ($oGroupInfo.StringOfEquals("name","Product updates",True) <> True) Then
    ConsoleWrite("Unexpected unsubscribe group; no preference was changed." & @CRLF)
    Exit
EndIf

$oAddresses = ObjCreate("Chilkat.JsonObject")
$oAddresses.UpdateString("recipient_emails[0]",$sAddress)

$bSuccess = $oHttp.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",$oAddresses,"application/json",$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 <> 201) Then
    ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$bSuccess = $oHttp.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",$oAddresses,"application/json",$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

$oMatches = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oMatches.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
    Exit
EndIf

If ($oMatches.Size = 1) Then
    ConsoleWrite("Confirmed opt-out: " & $oMatches.StringAt(0) & @CRLF)
Else
    ConsoleWrite("Opt-out not confirmed. Recheck this group." & @CRLF)
EndIf