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

SendGrid: Save and verify a topic opt-out

See more SendGrid Examples

Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.

Add group suppressions ยท Check specific addresses

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
address: WideString;
groupInfo: TChilkatJsonObject;
addresses: TChilkatJsonObject;
matches: TChilkatJsonArray;

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);

//  Take the address from the authenticated account and the group ID from configuration.
address := 'reader@example.com';
http.SetUrlVar('group','12345');

success := http.HttpNoBody('GET','https://api.sendgrid.com/v3/asm/groups/{$group}',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;

groupInfo := TChilkatJsonObject.Create(Self);
success := groupInfo.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Unexpected response: expected JSON.');
    Exit;
  end;
if (groupInfo.StringOfEquals('name','Product updates',1) <> 1) then
  begin
    Memo1.Lines.Add('Unexpected unsubscribe group; no preference was changed.');
    Exit;
  end;
addresses := TChilkatJsonObject.Create(Self);
addresses.UpdateString('recipient_emails[0]',address);

success := http.HttpJson('POST','https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions',addresses.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 <> 201) then
  begin
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
    Exit;
  end;

success := http.HttpJson('POST','https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search',addresses.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 <> 200) then
  begin
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
    Exit;
  end;

matches := TChilkatJsonArray.Create(Self);
success := matches.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Unexpected response: expected JSON.');
    Exit;
  end;
if (matches.Size = 1) then
  begin
    Memo1.Lines.Add('Confirmed opt-out: ' + matches.StringAt(0));
  end
else
  begin
    Memo1.Lines.Add('Opt-out not confirmed. Recheck this group.');
  end;