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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.auth_token = "SENDGRID_API_KEY"
local resp = chilkat.HttpResponse.new()

-- Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
local reviewedDraftId = ""
if reviewedDraftId == "" then
    local draft = chilkat.JsonObject.new()
    draft:update_string("name", "November newsletter")
    draft:update_string("send_to.list_ids[0]", "YOUR_LIST_UUID")
    draft:update_int("email_config.sender_id", 12345)
    draft:update_int("email_config.suppression_group_id", 67890)
    draft:update_string("email_config.design_id", "YOUR_NEWSLETTER_DESIGN_UUID")
    success = http:http_json("POST", "https://api.sendgrid.com/v3/marketing/singlesends", draft, "application/json", resp)
    if not success then
        if resp.status_code == 0 then
            print(http.last_error_text)
        else
            print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
        end

        return
    end

    if resp.status_code ~= 201 then
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
        return
    end

    local created = chilkat.JsonObject.new()
    success = created:load(resp.body_str)
    if not success then
        print("Unexpected response: expected JSON.")
        return
    end

    local draftId = created:string_of("id")
    if not created:has_member("id") then
        print("Missing id in response.")
        return
    end

    print("Draft created: " .. draftId .. ". Review it in Marketing Campaigns before scheduling.")
    return
end

-- The example selects tomorrow at the current UTC time. Use your approved send time.
local releaseTime = chilkat.DateTime.new()
releaseTime:set_from_current_system_time()
releaseTime:add_days(1)
local schedule = chilkat.JsonObject.new()
schedule:update_string("send_at", releaseTime:get_as_timestamp(false))
http:set_url_var("draft", reviewedDraftId)

success = http:http_json("PUT", "https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule", schedule, "application/json", resp)
if not success then
    if resp.status_code == 0 then
        print(http.last_error_text)
    else
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    end

    return
end

if resp.status_code ~= 201 then
    print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    return
end

local scheduled = chilkat.JsonObject.new()
success = scheduled:load(resp.body_str)
if not success then
    print("Unexpected response: expected JSON.")
    return
end

print(scheduled:string_of("status") .. ": " .. scheduled:string_of("send_at"))