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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http.AuthToken = "SENDGRID_API_KEY"
$resp = New-Object Chilkat.HttpResponse

#  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 -ne $true) {
    if ($resp.StatusCode -eq 0) {
        $($http.LastErrorText)
    }
    else {
        $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    }

    exit
}

if ($resp.StatusCode -ne 200) {
    $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    exit
}

$groupInfo = New-Object Chilkat.JsonObject
$success = $groupInfo.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Unexpected response: expected JSON.")
    exit
}

if ($groupInfo.StringOfEquals("name","Product updates",$true) -ne $true) {
    $("Unexpected unsubscribe group; no preference was changed.")
    exit
}

$addresses = New-Object Chilkat.JsonObject
$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 -ne $true) {
    if ($resp.StatusCode -eq 0) {
        $($http.LastErrorText)
    }
    else {
        $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    }

    exit
}

if ($resp.StatusCode -ne 201) {
    $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    exit
}

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

    exit
}

if ($resp.StatusCode -ne 200) {
    $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    exit
}

$matches = New-Object Chilkat.JsonArray
$success = $matches.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Unexpected response: expected JSON.")
    exit
}

if ($matches.Size -eq 1) {
    $("Confirmed opt-out: " + $matches.StringAt(0))
}
else {
    $("Opt-out not confirmed. Recheck this group.")
}