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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
'  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
set resp = Server.CreateObject("Chilkat.HttpResponse")

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

success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",resp)
If (success <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    End If

    Response.End
End If

If (resp.StatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    Response.End
End If

set groupInfo = Server.CreateObject("Chilkat.JsonObject")
success = groupInfo.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected response: expected JSON.") & "</pre>"
    Response.End
End If

If (groupInfo.StringOfEquals("name","Product updates",1) <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected unsubscribe group; no preference was changed.") & "</pre>"
    Response.End
End If

set addresses = Server.CreateObject("Chilkat.JsonObject")
success = 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 <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    End If

    Response.End
End If

If (resp.StatusCode <> 201) Then
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    Response.End
End If

success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"application/json",resp)
If (success <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    End If

    Response.End
End If

If (resp.StatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    Response.End
End If

set matches = Server.CreateObject("Chilkat.JsonArray")
success = matches.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected response: expected JSON.") & "</pre>"
    Response.End
End If

If (matches.Size = 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Confirmed opt-out: " & matches.StringAt(0)) & "</pre>"
Else
    Response.Write "<pre>" & Server.HTMLEncode( "Opt-out not confirmed. Recheck this group.") & "</pre>"
End If


%>
</body>
</html>