Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
jobId: PWideChar;
contacts: HCkJsonObject;
queued: HCkJsonObject;
job: HCkJsonObject;
state: PWideChar;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_putAuthToken(http,'SENDGRID_API_KEY');
resp := CkHttpResponse_Create();
jobId := '';
if (jobId = '') then
begin
contacts := CkJsonObject_Create();
CkJsonObject_UpdateString(contacts,'list_ids[0]','YOUR_LIST_UUID');
CkJsonObject_UpdateString(contacts,'contacts[0].email','avery@example.com');
CkJsonObject_UpdateString(contacts,'contacts[0].first_name','Avery');
CkJsonObject_UpdateString(contacts,'contacts[1].email','robin@example.com');
CkJsonObject_UpdateString(contacts,'contacts[1].first_name','Robin');
success := CkHttp_HttpJson(http,'PUT','https://api.sendgrid.com/v3/marketing/contacts',contacts,'application/json',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 202) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
queued := CkJsonObject_Create();
success := CkJsonObject_Load(queued,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
if (CkJsonObject_HasMember(queued,'job_id') <> True) then
begin
Memo1.Lines.Add('No job ID returned.');
Exit;
end;
jobId := CkJsonObject__stringOf(queued,'job_id');
Memo1.Lines.Add('Save this import job ID: ' + jobId);
end;
CkHttp_SetUrlVar(http,'job',jobId);
success := CkHttp_HttpNoBody(http,'GET','https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 200) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
job := CkJsonObject_Create();
success := CkJsonObject_Load(job,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
state := CkJsonObject__stringOf(job,'status');
if (state = 'pending') then
begin
Memo1.Lines.Add('Still pending. Check again later with jobId = ' + jobId);
Exit;
end;
Memo1.Lines.Add('Job status: ' + state);
Memo1.Lines.Add('Created: ' + IntToStr(CkJsonObject_IntOf(job,'results.created_count')) + ', updated: ' + IntToStr(CkJsonObject_IntOf(job,'results.updated_count'))
+ ', errors: ' + IntToStr(CkJsonObject_IntOf(job,'results.errored_count')));
if ((state = 'errored') or (state = 'failed')) then
begin
Memo1.Lines.Add('Review the job before retrying. If present, results.errors_url identifies the private error report.');
end;
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(contacts);
CkJsonObject_Dispose(queued);
CkJsonObject_Dispose(job);