Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

SendGrid: Create a newsletter draft, then schedule the reviewed draft

See more SendGrid Examples

Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.

Create a Single Send · Schedule a Single Send

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.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;

    //  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
    const wchar_t *reviewedDraftId = L"";
    if (reviewedDraftId == L"") {
        CkJsonObjectW draft;
        draft.UpdateString(L"name",L"November newsletter");
        draft.UpdateString(L"send_to.list_ids[0]",L"YOUR_LIST_UUID");
        draft.UpdateInt(L"email_config.sender_id",12345);
        draft.UpdateInt(L"email_config.suppression_group_id",67890);
        draft.UpdateString(L"email_config.design_id",L"YOUR_NEWSLETTER_DESIGN_UUID");
        success = http.HttpJson(L"POST",L"https://api.sendgrid.com/v3/marketing/singlesends",draft,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;
        }

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

        const wchar_t *draftId = created.stringOf(L"id");
        if (created.HasMember(L"id") != true) {
            wprintf(L"Missing id in response.\n");
            return;
        }

        wprintf(L"Draft created: %s. Review it in Marketing Campaigns before scheduling.\n",draftId);
        return;
    }

    //  The example selects tomorrow at the current UTC time. Use your approved send time.
    CkDateTimeW releaseTime;
    releaseTime.SetFromCurrentSystemTime();
    releaseTime.AddDays(1);
    CkJsonObjectW schedule;
    schedule.UpdateString(L"send_at",releaseTime.getAsTimestamp(false));
    http.SetUrlVar(L"draft",reviewedDraftId);

    success = http.HttpJson(L"PUT",L"https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",schedule,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;
    }

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

    wprintf(L"%s: %s\n",scheduled.stringOf(L"status"),scheduled.stringOf(L"send_at"));
    }