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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
string ls_ReviewedDraftId
oleobject loo_Draft
oleobject loo_Created
string ls_DraftId
oleobject loo_ReleaseTime
oleobject loo_Schedule
oleobject loo_Scheduled

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
//  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loo_Http.AuthToken = "SENDGRID_API_KEY"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

//  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
ls_ReviewedDraftId = ""
if ls_ReviewedDraftId = "" then
    loo_Draft = create oleobject
    li_rc = loo_Draft.ConnectToNewObject("Chilkat.JsonObject")

    loo_Draft.UpdateString("name","November newsletter")
    loo_Draft.UpdateString("send_to.list_ids[0]","YOUR_LIST_UUID")
    loo_Draft.UpdateInt("email_config.sender_id",12345)
    loo_Draft.UpdateInt("email_config.suppression_group_id",67890)
    loo_Draft.UpdateString("email_config.design_id","YOUR_NEWSLETTER_DESIGN_UUID")
    li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/marketing/singlesends",loo_Draft,"application/json",loo_Resp)
    if li_Success <> 1 then
        if loo_Resp.StatusCode = 0 then
            Write-Debug loo_Http.LastErrorText
        else
            Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
        end if

        destroy loo_Http
        destroy loo_Resp
        destroy loo_Draft
        return
    end if

    if loo_Resp.StatusCode <> 201 then
        Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Draft
        return
    end if

    loo_Created = create oleobject
    li_rc = loo_Created.ConnectToNewObject("Chilkat.JsonObject")

    li_Success = loo_Created.Load(loo_Resp.BodyStr)
    if li_Success <> 1 then
        Write-Debug "Unexpected response: expected JSON."
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Draft
        destroy loo_Created
        return
    end if

    ls_DraftId = loo_Created.StringOf("id")
    if loo_Created.HasMember("id") <> 1 then
        Write-Debug "Missing id in response."
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Draft
        destroy loo_Created
        return
    end if

    Write-Debug "Draft created: " + ls_DraftId + ". Review it in Marketing Campaigns before scheduling."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Draft
    destroy loo_Created
    return
end if

//  The example selects tomorrow at the current UTC time. Use your approved send time.
loo_ReleaseTime = create oleobject
li_rc = loo_ReleaseTime.ConnectToNewObject("Chilkat.CkDateTime")

loo_ReleaseTime.SetFromCurrentSystemTime()
loo_ReleaseTime.AddDays(1)
loo_Schedule = create oleobject
li_rc = loo_Schedule.ConnectToNewObject("Chilkat.JsonObject")

loo_Schedule.UpdateString("send_at",loo_ReleaseTime.GetAsTimestamp(0))
loo_Http.SetUrlVar("draft",ls_ReviewedDraftId)

li_Success = loo_Http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",loo_Schedule,"application/json",loo_Resp)
if li_Success <> 1 then
    if loo_Resp.StatusCode = 0 then
        Write-Debug loo_Http.LastErrorText
    else
        Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
    end if

    destroy loo_Http
    destroy loo_Resp
    destroy loo_Draft
    destroy loo_Created
    destroy loo_ReleaseTime
    destroy loo_Schedule
    return
end if

if loo_Resp.StatusCode <> 201 then
    Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Draft
    destroy loo_Created
    destroy loo_ReleaseTime
    destroy loo_Schedule
    return
end if

loo_Scheduled = create oleobject
li_rc = loo_Scheduled.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Scheduled.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Unexpected response: expected JSON."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Draft
    destroy loo_Created
    destroy loo_ReleaseTime
    destroy loo_Schedule
    destroy loo_Scheduled
    return
end if

Write-Debug loo_Scheduled.StringOf("status") + ": " + loo_Scheduled.StringOf("send_at")


destroy loo_Http
destroy loo_Resp
destroy loo_Draft
destroy loo_Created
destroy loo_ReleaseTime
destroy loo_Schedule
destroy loo_Scheduled