Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
resp: THttpResponse;
jobId: string;
contacts: TJsonObject;
queued: TJsonObject;
job: TJsonObject;
state: string;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
http := THttp.Create;
http.RequireSslCertVerify := True;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken := 'SENDGRID_API_KEY';
resp := THttpResponse.Create;
jobId := '';
if (jobId = '') then
begin
contacts := TJsonObject.Create;
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) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn(http.LastErrorText);
end
else
begin
WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 202) then
begin
WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
Exit;
end;
queued := TJsonObject.Create;
success := queued.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Unexpected response: expected JSON.');
Exit;
end;
if (queued.HasMember('job_id') <> True) then
begin
WriteLn('No job ID returned.');
Exit;
end;
jobId := queued.StringOf('job_id');
WriteLn('Save this import job ID: ' + jobId);
end;
http.SetUrlVar('job',jobId);
success := http.HttpNoBody('GET','https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}',resp);
if (success <> True) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn(http.LastErrorText);
end
else
begin
WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 200) then
begin
WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
Exit;
end;
job := TJsonObject.Create;
success := job.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Unexpected response: expected JSON.');
Exit;
end;
state := job.StringOf('status');
if (state = 'pending') then
begin
WriteLn('Still pending. Check again later with jobId = ' + jobId);
Exit;
end;
WriteLn('Job status: ' + state);
WriteLn('Created: ' + job.IntOf('results.created_count') + ', updated: ' + job.IntOf('results.updated_count') + ', errors: ' + job.IntOf('results.errored_count'));
if ((state = 'errored') or (state = 'failed')) then
begin
WriteLn('Review the job before retrying. If present, results.errors_url identifies the private error report.');
end;
http.Free;
resp.Free;
contacts.Free;
queued.Free;
job.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.