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

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL lcReviewedDraftId
LOCAL loDraft
LOCAL loCreated
LOCAL lcDraftId
LOCAL loReleaseTime
LOCAL loSchedule
LOCAL loScheduled

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')

*  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
lcReviewedDraftId = ""
IF (lcReviewedDraftId = "") THEN
    loDraft = CreateObject('Chilkat.JsonObject')
    loDraft.UpdateString("name","November newsletter")
    loDraft.UpdateString("send_to.list_ids[0]","YOUR_LIST_UUID")
    loDraft.UpdateInt("email_config.sender_id",12345)
    loDraft.UpdateInt("email_config.suppression_group_id",67890)
    loDraft.UpdateString("email_config.design_id","YOUR_NEWSLETTER_DESIGN_UUID")
    lnSuccess = loHttp.HttpJson("POST","https://api.sendgrid.com/v3/marketing/singlesends",loDraft,"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 loDraft
        CANCEL
    ENDIF

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

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

    lcDraftId = loCreated.StringOf("id")
    IF (loCreated.HasMember("id") <> 1) THEN
        ? "Missing id in response."
        RELEASE loHttp
        RELEASE loResp
        RELEASE loDraft
        RELEASE loCreated
        CANCEL
    ENDIF

    ? "Draft created: " + lcDraftId + ". Review it in Marketing Campaigns before scheduling."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loDraft
    RELEASE loCreated
    CANCEL
ENDIF

*  The example selects tomorrow at the current UTC time. Use your approved send time.
loReleaseTime = CreateObject('Chilkat.CkDateTime')
loReleaseTime.SetFromCurrentSystemTime()
loReleaseTime.AddDays(1)
loSchedule = CreateObject('Chilkat.JsonObject')
loSchedule.UpdateString("send_at",loReleaseTime.GetAsTimestamp(0))
loHttp.SetUrlVar("draft",lcReviewedDraftId)

lnSuccess = loHttp.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",loSchedule,"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 loDraft
    RELEASE loCreated
    RELEASE loReleaseTime
    RELEASE loSchedule
    CANCEL
ENDIF

IF (loResp.StatusCode <> 201) THEN
    ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    RELEASE loHttp
    RELEASE loResp
    RELEASE loDraft
    RELEASE loCreated
    RELEASE loReleaseTime
    RELEASE loSchedule
    CANCEL
ENDIF

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

? loScheduled.StringOf("status") + ": " + loScheduled.StringOf("send_at")

RELEASE loHttp
RELEASE loResp
RELEASE loDraft
RELEASE loCreated
RELEASE loReleaseTime
RELEASE loSchedule
RELEASE loScheduled