Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.set_auth_token("SENDGRID_API_KEY");
let resp = chilkat::HttpResponse::new();
let mut job_id = String::new();
if job_id == "" {
let contacts = chilkat::JsonObject::new();
let _ = contacts.update_string("list_ids[0]", "YOUR_LIST_UUID");
let _ = contacts.update_string("contacts[0].email", "avery@example.com");
let _ = contacts.update_string("contacts[0].first_name", "Avery");
let _ = contacts.update_string("contacts[1].email", "robin@example.com");
let _ = contacts.update_string("contacts[1].first_name", "Robin");
if http.http_json("PUT", "https://api.sendgrid.com/v3/marketing/contacts", &contacts, "application/json", &resp).is_err() {
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
} else {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
}
return;
}
if resp.status_code() != 202 {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
return;
}
let queued = chilkat::JsonObject::new();
if queued.load(&resp.body_str()).is_err() {
println!("Unexpected response: expected JSON.");
return;
}
if !queued.has_member("job_id") {
println!("No job ID returned.");
return;
}
job_id = queued.string_of("job_id").unwrap_or_default();
println!("Save this import job ID: {}", job_id);
}
let _ = http.set_url_var("job", &job_id);
if http.http_no_body("GET", "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}", &resp).is_err() {
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
} else {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
}
return;
}
if resp.status_code() != 200 {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
return;
}
let job = chilkat::JsonObject::new();
if job.load(&resp.body_str()).is_err() {
println!("Unexpected response: expected JSON.");
return;
}
let state = job.string_of("status").unwrap_or_default();
if state == "pending" {
println!("Still pending. Check again later with jobId = {}", job_id);
return;
}
println!("Job status: {}", state);
println!("Created: {}, updated: {}, errors: {}", job.int_of("results.created_count"), job.int_of("results.updated_count"), job.int_of("results.errored_count"));
if (state == "errored") || (state == "failed") {
println!("Review the job before retrying. If present, results.errors_url identifies the private error report.");
}