Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL cJobId
LOCAL oContacts
LOCAL oQueued
LOCAL oJob
LOCAL cState
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
oHttp:AuthToken := "SENDGRID_API_KEY"
oResp := CreateObject("Chilkat.HttpResponse")
cJobId := ""
IF (cJobId == "")
oContacts := CreateObject("Chilkat.JsonObject")
oContacts:UpdateString("list_ids[0]", "YOUR_LIST_UUID")
oContacts:UpdateString("contacts[0].email", "avery@example.com")
oContacts:UpdateString("contacts[0].first_name", "Avery")
oContacts:UpdateString("contacts[1].email", "robin@example.com")
oContacts:UpdateString("contacts[1].first_name", "Robin")
nSuccess := oHttp:HttpJson("PUT", "https://api.sendgrid.com/v3/marketing/contacts", oContacts, "application/json", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 202)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
RETURN
ENDIF
oQueued := CreateObject("Chilkat.JsonObject")
nSuccess := oQueued:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Unexpected response: expected JSON."
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
RETURN
ENDIF
IF (oQueued:HasMember("job_id") != 1)
? "No job ID returned."
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
RETURN
ENDIF
cJobId := oQueued:StringOf("job_id")
? "Save this import job ID: " + cJobId
ENDIF
oHttp:SetUrlVar("job", cJobId)
nSuccess := oHttp:HttpNoBody("GET", "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 200)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
RETURN
ENDIF
oJob := CreateObject("Chilkat.JsonObject")
nSuccess := oJob:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Unexpected response: expected JSON."
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
oJob:destroy()
RETURN
ENDIF
cState := oJob:StringOf("status")
IF (cState == "pending")
? "Still pending. Check again later with jobId = " + cJobId
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
oJob:destroy()
RETURN
ENDIF
? "Job status: " + cState
? "Created: " + Str(oJob:IntOf("results.created_count")) + ", updated: " + Str(oJob:IntOf("results.updated_count")) + ", errors: " + Str(oJob:IntOf("results.errored_count"))
IF ((cState == "errored") .OR. (cState == "failed"))
? "Review the job before retrying. If present, results.errors_url identifies the private error report."
ENDIF
oHttp:destroy()
oResp:destroy()
oContacts:destroy()
oQueued:destroy()
oJob:destroy()