Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = 'SENDGRID_API_KEY';
final resp = CkHttpResponse();
// Take the address from the authenticated account and the group ID from configuration.
final address = 'reader@example.com';
http.setUrlVar('group', '12345');
try {
http.httpNoBody('GET', 'https://api.sendgrid.com/v3/asm/groups/{\$group}', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 200) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
final groupInfo = CkJsonObject();
try {
groupInfo.load(resp.bodyStr);
} on ChilkatException {
print('Unexpected response: expected JSON.');
return;
}
if (!groupInfo.stringOfEquals('name', 'Product updates', true)) {
print('Unexpected unsubscribe group; no preference was changed.');
return;
}
final addresses = CkJsonObject();
addresses.updateString('recipient_emails[0]', address);
try {
http.httpJson('POST', 'https://api.sendgrid.com/v3/asm/groups/{\$group}/suppressions', addresses, 'application/json', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 201) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
try {
http.httpJson('POST', 'https://api.sendgrid.com/v3/asm/groups/{\$group}/suppressions/search', addresses, 'application/json', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 200) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
final matches = CkJsonArray();
try {
matches.load(resp.bodyStr);
} on ChilkatException {
print('Unexpected response: expected JSON.');
return;
}
if (matches.size == 1) {
print('Confirmed opt-out: ${matches.stringAt(0)}');
} else {
print('Opt-out not confirmed. Recheck this group.');
}
}