Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
# Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY")
resp = Chilkat::CkHttpResponse.new()
jobId = ""
if (jobId == "")
contacts = Chilkat::CkJsonObject.new()
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.get_StatusCode() == 0)
print http.lastErrorText() + "\n";
else
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
end
exit
end
if (resp.get_StatusCode() != 202)
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
exit
end
queued = Chilkat::CkJsonObject.new()
success = queued.Load(resp.bodyStr())
if (success != true)
print "Unexpected response: expected JSON." + "\n";
exit
end
if (queued.HasMember("job_id") != true)
print "No job ID returned." + "\n";
exit
end
jobId = queued.stringOf("job_id")
print "Save this import job ID: " + jobId + "\n";
end
http.SetUrlVar("job",jobId)
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp)
if (success != true)
if (resp.get_StatusCode() == 0)
print http.lastErrorText() + "\n";
else
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
end
exit
end
if (resp.get_StatusCode() != 200)
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
exit
end
job = Chilkat::CkJsonObject.new()
success = job.Load(resp.bodyStr())
if (success != true)
print "Unexpected response: expected JSON." + "\n";
exit
end
state = job.stringOf("status")
if (state == "pending")
print "Still pending. Check again later with jobId = " + jobId + "\n";
exit
end
print "Job status: " + state + "\n";
print "Created: " + job.IntOf("results.created_count").to_s() + ", updated: " + job.IntOf("results.updated_count").to_s() + ", errors: " + job.IntOf("results.errored_count").to_s() + "\n";
if ((state == "errored") or (state == "failed"))
print "Review the job before retrying. If present, results.errors_url identifies the private error report." + "\n";
end