Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL cAddress
LOCAL oGroupInfo
LOCAL oAddresses
LOCAL oMatches
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
oHttp:AuthToken := "SENDGRID_API_KEY"
oResp := CreateObject("Chilkat.HttpResponse")
// Take the address from the authenticated account and the group ID from configuration.
cAddress := "reader@example.com"
oHttp:SetUrlVar("group", "12345")
nSuccess := oHttp:HttpNoBody("GET", "https://api.sendgrid.com/v3/asm/groups/{$group}", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 200)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
oGroupInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oGroupInfo:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Unexpected response: expected JSON."
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
RETURN
ENDIF
IF (oGroupInfo:StringOfEquals("name", "Product updates", 1) != 1)
? "Unexpected unsubscribe group; no preference was changed."
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
RETURN
ENDIF
oAddresses := CreateObject("Chilkat.JsonObject")
oAddresses:UpdateString("recipient_emails[0]", cAddress)
nSuccess := oHttp:HttpJson("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions", oAddresses, "application/json", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 201)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
RETURN
ENDIF
nSuccess := oHttp:HttpJson("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search", oAddresses, "application/json", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 200)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
RETURN
ENDIF
oMatches := CreateObject("Chilkat.JsonArray")
nSuccess := oMatches:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Unexpected response: expected JSON."
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
oMatches:destroy()
RETURN
ENDIF
IF (oMatches:Size == 1)
? "Confirmed opt-out: " + oMatches:StringAt(0)
ELSE
? "Opt-out not confirmed. Recheck this group."
ENDIF
oHttp:destroy()
oResp:destroy()
oGroupInfo:destroy()
oAddresses:destroy()
oMatches:destroy()