Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
let http = CkoHttp()!
http.requireSslCertVerify = true
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = "SENDGRID_API_KEY"
let resp = CkoHttpResponse()!
// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
var reviewedDraftId: String? = ""
if reviewedDraftId == "" {
let draft = CkoJsonObject()!
draft.updateString(jsonPath: "name", value: "November newsletter")
draft.updateString(jsonPath: "send_to.list_ids[0]", value: "YOUR_LIST_UUID")
draft.updateInt(jsonPath: "email_config.sender_id", value: 12345)
draft.updateInt(jsonPath: "email_config.suppression_group_id", value: 67890)
draft.updateString(jsonPath: "email_config.design_id", value: "YOUR_NEWSLETTER_DESIGN_UUID")
success = http.httpJson(verb: "POST", url: "https://api.sendgrid.com/v3/marketing/singlesends", json: draft, contentType: "application/json", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 201 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let created = CkoJsonObject()!
success = created.load(json: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
var draftId: String? = created.string(of: "id")
if created.hasMember(jsonPath: "id") != true {
print("Missing id in response.")
return
}
print("Draft created: \(draftId!). Review it in Marketing Campaigns before scheduling.")
return
}
// The example selects tomorrow at the current UTC time. Use your approved send time.
let releaseTime = CkoDateTime()!
releaseTime.setFromCurrentSystemTime()
releaseTime.addDays(numDays: 1)
let schedule = CkoJsonObject()!
schedule.updateString(jsonPath: "send_at", value: releaseTime.get(asTimestamp: false))
http.setUrlVar(name: "draft", value: reviewedDraftId)
success = http.httpJson(verb: "PUT", url: "https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule", json: schedule, contentType: "application/json", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 201 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let scheduled = CkoJsonObject()!
success = scheduled.load(json: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
print("\(scheduled.string(of: "status")!): \(scheduled.string(of: "send_at")!)")
}