Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
address: PWideChar;
groupInfo: HCkJsonObject;
addresses: HCkJsonObject;
matches: HCkJsonArray;
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();
// Take the address from the authenticated account and the group ID from configuration.
address := 'reader@example.com';
CkHttp_SetUrlVar(http,'group','12345');
success := CkHttp_HttpNoBody(http,'GET','https://api.sendgrid.com/v3/asm/groups/{$group}',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;
groupInfo := CkJsonObject_Create();
success := CkJsonObject_Load(groupInfo,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
if (CkJsonObject_StringOfEquals(groupInfo,'name','Product updates',True) <> True) then
begin
Memo1.Lines.Add('Unexpected unsubscribe group; no preference was changed.');
Exit;
end;
addresses := CkJsonObject_Create();
CkJsonObject_UpdateString(addresses,'recipient_emails[0]',address);
success := CkHttp_HttpJson(http,'POST','https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions',addresses,'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) <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
success := CkHttp_HttpJson(http,'POST','https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search',addresses,'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) <> 200) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
matches := CkJsonArray_Create();
success := CkJsonArray_Load(matches,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
if (CkJsonArray_getSize(matches) = 1) then
begin
Memo1.Lines.Add('Confirmed opt-out: ' + CkJsonArray__stringAt(matches,0));
end
else
begin
Memo1.Lines.Add('Opt-out not confirmed. Recheck this group.');
end;
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(groupInfo);
CkJsonObject_Dispose(addresses);
CkJsonArray_Dispose(matches);