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

C
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
#include <C_CkDateTime.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkHttpResponse resp;
    const char *reviewedDraftId;
    HCkJsonObject draft;
    HCkJsonObject created;
    const char *draftId;
    HCkDateTime releaseTime;
    HCkJsonObject schedule;
    HCkJsonObject scheduled;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    CkHttp_putAuthToken(http,"SENDGRID_API_KEY");
    resp = CkHttpResponse_Create();

    //  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
    reviewedDraftId = "";
    if (reviewedDraftId == "") {
        draft = CkJsonObject_Create();
        CkJsonObject_UpdateString(draft,"name","November newsletter");
        CkJsonObject_UpdateString(draft,"send_to.list_ids[0]","YOUR_LIST_UUID");
        CkJsonObject_UpdateInt(draft,"email_config.sender_id",12345);
        CkJsonObject_UpdateInt(draft,"email_config.suppression_group_id",67890);
        CkJsonObject_UpdateString(draft,"email_config.design_id","YOUR_NEWSLETTER_DESIGN_UUID");
        success = CkHttp_HttpJson(http,"POST","https://api.sendgrid.com/v3/marketing/singlesends",draft,"application/json",resp);
        if (success != TRUE) {
            if (CkHttpResponse_getStatusCode(resp) == 0) {
                printf("%s\n",CkHttp_lastErrorText(http));
            }
            else {
                printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
            }

            CkHttp_Dispose(http);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(draft);
            return;
        }

        if (CkHttpResponse_getStatusCode(resp) != 201) {
            printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
            CkHttp_Dispose(http);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(draft);
            return;
        }

        created = CkJsonObject_Create();
        success = CkJsonObject_Load(created,CkHttpResponse_bodyStr(resp));
        if (success != TRUE) {
            printf("Unexpected response: expected JSON.\n");
            CkHttp_Dispose(http);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(draft);
            CkJsonObject_Dispose(created);
            return;
        }

        draftId = CkJsonObject_stringOf(created,"id");
        if (CkJsonObject_HasMember(created,"id") != TRUE) {
            printf("Missing id in response.\n");
            CkHttp_Dispose(http);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(draft);
            CkJsonObject_Dispose(created);
            return;
        }

        printf("Draft created: %s. Review it in Marketing Campaigns before scheduling.\n",draftId);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(draft);
        CkJsonObject_Dispose(created);
        return;
    }

    //  The example selects tomorrow at the current UTC time. Use your approved send time.
    releaseTime = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(releaseTime);
    CkDateTime_AddDays(releaseTime,1);
    schedule = CkJsonObject_Create();
    CkJsonObject_UpdateString(schedule,"send_at",CkDateTime_getAsTimestamp(releaseTime,FALSE));
    CkHttp_SetUrlVar(http,"draft",reviewedDraftId);

    success = CkHttp_HttpJson(http,"PUT","https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",schedule,"application/json",resp);
    if (success != TRUE) {
        if (CkHttpResponse_getStatusCode(resp) == 0) {
            printf("%s\n",CkHttp_lastErrorText(http));
        }
        else {
            printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
        }

        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(draft);
        CkJsonObject_Dispose(created);
        CkDateTime_Dispose(releaseTime);
        CkJsonObject_Dispose(schedule);
        return;
    }

    if (CkHttpResponse_getStatusCode(resp) != 201) {
        printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(draft);
        CkJsonObject_Dispose(created);
        CkDateTime_Dispose(releaseTime);
        CkJsonObject_Dispose(schedule);
        return;
    }

    scheduled = CkJsonObject_Create();
    success = CkJsonObject_Load(scheduled,CkHttpResponse_bodyStr(resp));
    if (success != TRUE) {
        printf("Unexpected response: expected JSON.\n");
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(draft);
        CkJsonObject_Dispose(created);
        CkDateTime_Dispose(releaseTime);
        CkJsonObject_Dispose(schedule);
        CkJsonObject_Dispose(scheduled);
        return;
    }

    printf("%s: %s\n",CkJsonObject_stringOf(scheduled,"status"),CkJsonObject_stringOf(scheduled,"send_at"));


    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(draft);
    CkJsonObject_Dispose(created);
    CkDateTime_Dispose(releaseTime);
    CkJsonObject_Dispose(schedule);
    CkJsonObject_Dispose(scheduled);

    }