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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
reviewedDraftId: PWideChar;
draft: HCkJsonObject;
created: HCkJsonObject;
draftId: PWideChar;
releaseTime: HCkDateTime;
schedule: HCkJsonObject;
scheduled: HCkJsonObject;

begin
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 = '') then
  begin
    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) then
      begin
        if (CkHttpResponse_getStatusCode(resp) = 0) then
          begin
            Memo1.Lines.Add(CkHttp__lastErrorText(http));
          end
        else
          begin
            Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
          end;
        Exit;
      end;
    if (CkHttpResponse_getStatusCode(resp) <> 201) then
      begin
        Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
        Exit;
      end;

    created := CkJsonObject_Create();
    success := CkJsonObject_Load(created,CkHttpResponse__bodyStr(resp));
    if (success <> True) then
      begin
        Memo1.Lines.Add('Unexpected response: expected JSON.');
        Exit;
      end;
    draftId := CkJsonObject__stringOf(created,'id');
    if (CkJsonObject_HasMember(created,'id') <> True) then
      begin
        Memo1.Lines.Add('Missing id in response.');
        Exit;
      end;
    Memo1.Lines.Add('Draft created: ' + draftId + '. Review it in Marketing Campaigns before scheduling.');
    Exit;
  end;
//  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) then
  begin
    if (CkHttpResponse_getStatusCode(resp) = 0) then
      begin
        Memo1.Lines.Add(CkHttp__lastErrorText(http));
      end
    else
      begin
        Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
      end;
    Exit;
  end;
if (CkHttpResponse_getStatusCode(resp) <> 201) then
  begin
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;

scheduled := CkJsonObject_Create();
success := CkJsonObject_Load(scheduled,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Unexpected response: expected JSON.');
    Exit;
  end;
Memo1.Lines.Add(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);