Sample code for 30+ languages & platforms
Lazarus Pascal 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 Lazarus Pascal Downloads

Lazarus Pascal
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.JsonArray,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  address: string;
  groupInfo: TJsonObject;
  addresses: TJsonObject;
  matches: TJsonArray;

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;

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

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

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

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

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


  http.Free;
  resp.Free;
  groupInfo.Free;
  addresses.Free;
  matches.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.