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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
;  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$oHttp.AuthToken = "SENDGRID_API_KEY"
$oResp = ObjCreate("Chilkat.HttpResponse")

;  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
Local $sReviewedDraftId = ""
If ($sReviewedDraftId = "") Then
    $oDraft = ObjCreate("Chilkat.JsonObject")
    $oDraft.UpdateString("name","November newsletter")
    $oDraft.UpdateString("send_to.list_ids[0]","YOUR_LIST_UUID")
    $oDraft.UpdateInt("email_config.sender_id",12345)
    $oDraft.UpdateInt("email_config.suppression_group_id",67890)
    $oDraft.UpdateString("email_config.design_id","YOUR_NEWSLETTER_DESIGN_UUID")
    $bSuccess = $oHttp.HttpJson("POST","https://api.sendgrid.com/v3/marketing/singlesends",$oDraft,"application/json",$oResp)
    If ($bSuccess <> True) Then
        If ($oResp.StatusCode = 0) Then
            ConsoleWrite($oHttp.LastErrorText & @CRLF)
        Else
            ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
        EndIf

        Exit
    EndIf

    If ($oResp.StatusCode <> 201) Then
        ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
        Exit
    EndIf

    $oCreated = ObjCreate("Chilkat.JsonObject")
    $bSuccess = $oCreated.Load($oResp.BodyStr)
    If ($bSuccess <> True) Then
        ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
        Exit
    EndIf

Local $sDraftId = $oCreated.StringOf("id")
    If ($oCreated.HasMember("id") <> True) Then
        ConsoleWrite("Missing id in response." & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Draft created: " & $sDraftId & ". Review it in Marketing Campaigns before scheduling." & @CRLF)
    Exit
EndIf

;  The example selects tomorrow at the current UTC time. Use your approved send time.
$oReleaseTime = ObjCreate("Chilkat.CkDateTime")
$oReleaseTime.SetFromCurrentSystemTime()
$oReleaseTime.AddDays(1)
$oSchedule = ObjCreate("Chilkat.JsonObject")
$oSchedule.UpdateString("send_at",$oReleaseTime.GetAsTimestamp(False))
$oHttp.SetUrlVar("draft",$sReviewedDraftId)

$bSuccess = $oHttp.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",$oSchedule,"application/json",$oResp)
If ($bSuccess <> True) Then
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Else
        ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
    EndIf

    Exit
EndIf

If ($oResp.StatusCode <> 201) Then
    ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$oScheduled = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oScheduled.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
    Exit
EndIf

ConsoleWrite($oScheduled.StringOf("status") & ": " & $oScheduled.StringOf("send_at") & @CRLF)