Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Http http = new Http();
http.setRequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.setAuthToken("SENDGRID_API_KEY");
HttpResponse resp = new HttpResponse();
String jobId = "";
if ("".equals(jobId)) {
JsonObject contacts = new JsonObject();
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");
try {
http.httpJson("PUT", "https://api.sendgrid.com/v3/marketing/contacts", contacts, "application/json", resp);
} catch (ChilkatException e) {
if (resp.getStatusCode() == 0) {
System.out.println(e.getLastErrorText());
} else {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
}
return;
}
if (resp.getStatusCode() != 202) {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
return;
}
JsonObject queued = new JsonObject();
try {
queued.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
if (!queued.hasMember("job_id")) {
System.out.println("No job ID returned.");
return;
}
jobId = queued.stringOf("job_id");
System.out.println("Save this import job ID: " + jobId);
}
http.setUrlVar("job", jobId);
try {
http.httpNoBody("GET", "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}", resp);
} catch (ChilkatException e) {
if (resp.getStatusCode() == 0) {
System.out.println(e.getLastErrorText());
} else {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
}
return;
}
if (resp.getStatusCode() != 200) {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
return;
}
JsonObject job = new JsonObject();
try {
job.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
String state = job.stringOf("status");
if ("pending".equals(state)) {
System.out.println("Still pending. Check again later with jobId = " + jobId);
return;
}
System.out.println("Job status: " + state);
System.out.println("Created: " + job.intOf("results.created_count") + ", updated: " + job.intOf("results.updated_count") + ", errors: " + job.intOf("results.errored_count"));
if (("errored".equals(state)) || ("failed".equals(state))) {
System.out.println("Review the job before retrying. If present, results.errors_url identifies the private error report.");
}
}
}