Swift Requires Chilkat v11.0.0+
Swift
SendGrid: Add or update list contacts and check the import job
See more SendGrid Examples
Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.
Add or update contacts ยท Import job status
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()!
var jobId: String? = ""
if jobId == "" {
let contacts = CkoJsonObject()!
contacts.updateString(jsonPath: "list_ids[0]", value: "YOUR_LIST_UUID")
contacts.updateString(jsonPath: "contacts[0].email", value: "avery@example.com")
contacts.updateString(jsonPath: "contacts[0].first_name", value: "Avery")
contacts.updateString(jsonPath: "contacts[1].email", value: "robin@example.com")
contacts.updateString(jsonPath: "contacts[1].first_name", value: "Robin")
success = http.httpJson(verb: "PUT", url: "https://api.sendgrid.com/v3/marketing/contacts", json: contacts, 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 != 202 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let queued = CkoJsonObject()!
success = queued.load(json: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
if queued.hasMember(jsonPath: "job_id") != true {
print("No job ID returned.")
return
}
jobId = queued.string(of: "job_id")
print("Save this import job ID: \(jobId!)")
}
http.setUrlVar(name: "job", value: jobId)
success = http.httpNoBody(verb: "GET", url: "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}", 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 != 200 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let job = CkoJsonObject()!
success = job.load(json: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
var state: String? = job.string(of: "status")
if state == "pending" {
print("Still pending. Check again later with jobId = \(jobId!)")
return
}
print("Job status: \(state!)")
print("Created: \(job.int(of: "results.created_count").intValue), updated: \(job.int(of: "results.updated_count").intValue), errors: \(job.int(of: "results.errored_count").intValue)")
if (state == "errored") || (state == "failed") {
print("Review the job before retrying. If present, results.errors_url identifies the private error report.")
}
}