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

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL lcAddress
LOCAL loGroupInfo
LOCAL loAddresses
LOCAL loMatches

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
*  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loHttp.AuthToken = "SENDGRID_API_KEY"
loResp = CreateObject('Chilkat.HttpResponse')

*  Take the address from the authenticated account and the group ID from configuration.
lcAddress = "reader@example.com"
loHttp.SetUrlVar("group","12345")

lnSuccess = loHttp.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",loResp)
IF (lnSuccess <> 1) THEN
    IF (loResp.StatusCode = 0) THEN
        ? loHttp.LastErrorText
    ELSE
        ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    ENDIF

    RELEASE loHttp
    RELEASE loResp
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    RELEASE loHttp
    RELEASE loResp
    CANCEL
ENDIF

loGroupInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loGroupInfo.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Unexpected response: expected JSON."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    CANCEL
ENDIF

IF (loGroupInfo.StringOfEquals("name","Product updates",1) <> 1) THEN
    ? "Unexpected unsubscribe group; no preference was changed."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    CANCEL
ENDIF

loAddresses = CreateObject('Chilkat.JsonObject')
loAddresses.UpdateString("recipient_emails[0]",lcAddress)

lnSuccess = loHttp.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",loAddresses,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    IF (loResp.StatusCode = 0) THEN
        ? loHttp.LastErrorText
    ELSE
        ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    ENDIF

    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    RELEASE loAddresses
    CANCEL
ENDIF

IF (loResp.StatusCode <> 201) THEN
    ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    RELEASE loAddresses
    CANCEL
ENDIF

lnSuccess = loHttp.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",loAddresses,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    IF (loResp.StatusCode = 0) THEN
        ? loHttp.LastErrorText
    ELSE
        ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    ENDIF

    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    RELEASE loAddresses
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    RELEASE loAddresses
    CANCEL
ENDIF

loMatches = CreateObject('Chilkat.JsonArray')
lnSuccess = loMatches.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Unexpected response: expected JSON."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loGroupInfo
    RELEASE loAddresses
    RELEASE loMatches
    CANCEL
ENDIF

IF (loMatches.Size = 1) THEN
    ? "Confirmed opt-out: " + loMatches.StringAt(0)
ELSE
    ? "Opt-out not confirmed. Recheck this group."
ENDIF

RELEASE loHttp
RELEASE loResp
RELEASE loGroupInfo
RELEASE loAddresses
RELEASE loMatches