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

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkJsonArrayW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    http.put_AuthToken(L"SENDGRID_API_KEY");
    CkHttpResponseW resp;

    //  Take the address from the authenticated account and the group ID from configuration.
    const wchar_t *address = L"reader@example.com";
    http.SetUrlVar(L"group",L"12345");

    success = http.HttpNoBody(L"GET",L"https://api.sendgrid.com/v3/asm/groups/{$group}",resp);
    if (success != true) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"%s\n",http.lastErrorText());
        }
        else {
            wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        }

        return;
    }

    if (resp.get_StatusCode() != 200) {
        wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        return;
    }

    CkJsonObjectW groupInfo;
    success = groupInfo.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Unexpected response: expected JSON.\n");
        return;
    }

    if (groupInfo.StringOfEquals(L"name",L"Product updates",true) != true) {
        wprintf(L"Unexpected unsubscribe group; no preference was changed.\n");
        return;
    }

    CkJsonObjectW addresses;
    addresses.UpdateString(L"recipient_emails[0]",address);

    success = http.HttpJson(L"POST",L"https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",addresses,L"application/json",resp);
    if (success != true) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"%s\n",http.lastErrorText());
        }
        else {
            wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        }

        return;
    }

    if (resp.get_StatusCode() != 201) {
        wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        return;
    }

    success = http.HttpJson(L"POST",L"https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,L"application/json",resp);
    if (success != true) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"%s\n",http.lastErrorText());
        }
        else {
            wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        }

        return;
    }

    if (resp.get_StatusCode() != 200) {
        wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        return;
    }

    CkJsonArrayW matches;
    success = matches.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Unexpected response: expected JSON.\n");
        return;
    }

    if (matches.get_Size() == 1) {
        wprintf(L"Confirmed opt-out: %s\n",matches.stringAt(0));
    }
    else {
        wprintf(L"Opt-out not confirmed. Recheck this group.\n");
    }
    }