JavaScript Requires Chilkat v11.0.0+
JavaScript
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
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
var http = new CkHttp();
http.RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY";
var resp = new CkHttpResponse();
// Take the address from the authenticated account and the group ID from configuration.
var address = "reader@example.com";
http.SetUrlVar("group","12345");
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",resp);
if (success !== true) {
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
}
else {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode !== 200) {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
return;
}
var groupInfo = new CkJsonObject();
success = groupInfo.Load(resp.BodyStr);
if (success !== true) {
console.log("Unexpected response: expected JSON.");
return;
}
if (groupInfo.StringOfEquals("name","Product updates",true) !== true) {
console.log("Unexpected unsubscribe group; no preference was changed.");
return;
}
var addresses = new CkJsonObject();
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) {
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
}
else {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode !== 201) {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
return;
}
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"application/json",resp);
if (success !== true) {
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
}
else {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode !== 200) {
console.log("HTTP " + resp.StatusCode + ": " + resp.BodyStr);
return;
}
var matches = new CkJsonArray();
success = matches.Load(resp.BodyStr);
if (success !== true) {
console.log("Unexpected response: expected JSON.");
return;
}
if (matches.Size == 1) {
console.log("Confirmed opt-out: " + matches.StringAt(0));
}
else {
console.log("Opt-out not confirmed. Recheck this group.");
}