JavaScript Requires Chilkat v11.0.0+
JavaScript
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
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
var http = new CkHttp();
http.RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY";
var resp = new CkHttpResponse();
var jobId = "";
if (jobId == "") {
var contacts = new CkJsonObject();
contacts.UpdateString("list_ids[0]","YOUR_LIST_UUID");
contacts.UpdateString("contacts[0].email","avery@example.com");
contacts.UpdateString("contacts[0].first_name","Avery");
contacts.UpdateString("contacts[1].email","robin@example.com");
contacts.UpdateString("contacts[1].first_name","Robin");
success = http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",contacts,"application/json",resp);
if (success !== true) {
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
}
else {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode !== 202) {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
return;
}
var queued = new CkJsonObject();
success = queued.Load(resp.BodyStr);
if (success !== true) {
console.log("Unexpected response: expected JSON.");
return;
}
if (queued.HasMember("job_id") !== true) {
console.log("No job ID returned.");
return;
}
jobId = queued.StringOf("job_id");
console.log("Save this import job ID: " + jobId);
}
http.SetUrlVar("job",jobId);
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp);
if (success !== true) {
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
}
else {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode !== 200) {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
return;
}
var job = new CkJsonObject();
success = job.Load(resp.BodyStr);
if (success !== true) {
console.log("Unexpected response: expected JSON.");
return;
}
var state = job.StringOf("status");
if (state == "pending") {
console.log("Still pending. Check again later with jobId = " + jobId);
return;
}
console.log("Job status: " + state);
console.log("Created: " + job.IntOf("results.created_count") + ", updated: " + job.IntOf("results.updated_count") + ", errors: " + job.IntOf("results.errored_count"));
if ((state == "errored") || (state == "failed")) {
console.log("Review the job before retrying. If present, results.errors_url identifies the private error report.");
}