Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

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 ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
jobId: WideString;
contacts: TChilkatJsonObject;
queued: TChilkatJsonObject;
job: TChilkatJsonObject;
state: WideString;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
//  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken := 'SENDGRID_API_KEY';
resp := TChilkatHttpResponse.Create(Self);

jobId := '';
if (jobId = '') then
  begin
    contacts := TChilkatJsonObject.Create(Self);
    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.ControlInterface,'application/json',resp.ControlInterface);
    if (success <> 1) then
      begin
        if (resp.StatusCode = 0) then
          begin
            Memo1.Lines.Add(http.LastErrorText);
          end
        else
          begin
            Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
          end;
        Exit;
      end;
    if (resp.StatusCode <> 202) then
      begin
        Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
        Exit;
      end;

    queued := TChilkatJsonObject.Create(Self);
    success := queued.Load(resp.BodyStr);
    if (success <> 1) then
      begin
        Memo1.Lines.Add('Unexpected response: expected JSON.');
        Exit;
      end;
    if (queued.HasMember('job_id') <> 1) then
      begin
        Memo1.Lines.Add('No job ID returned.');
        Exit;
      end;
    jobId := queued.StringOf('job_id');
    Memo1.Lines.Add('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.ControlInterface);
if (success <> 1) then
  begin
    if (resp.StatusCode = 0) then
      begin
        Memo1.Lines.Add(http.LastErrorText);
      end
    else
      begin
        Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
      end;
    Exit;
  end;
if (resp.StatusCode <> 200) then
  begin
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
    Exit;
  end;

job := TChilkatJsonObject.Create(Self);
success := job.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Unexpected response: expected JSON.');
    Exit;
  end;
state := job.StringOf('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(job.IntOf('results.created_count')) + ', updated: ' + IntToStr(job.IntOf('results.updated_count')) + ', errors: ' + IntToStr(job.IntOf('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;