Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.auth_token = "SENDGRID_API_KEY"
local resp = chilkat.HttpResponse.new()
-- Take the address from the authenticated account and the group ID from configuration.
local address = "reader@example.com"
http:set_url_var("group", "12345")
success = http:http_no_body("GET", "https://api.sendgrid.com/v3/asm/groups/{$group}", resp)
if not success then
if resp.status_code == 0 then
print(http.last_error_text)
else
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
end
return
end
if resp.status_code ~= 200 then
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
return
end
local groupInfo = chilkat.JsonObject.new()
success = groupInfo:load(resp.body_str)
if not success then
print("Unexpected response: expected JSON.")
return
end
if not groupInfo:string_of_equals("name", "Product updates", true) then
print("Unexpected unsubscribe group; no preference was changed.")
return
end
local addresses = chilkat.JsonObject.new()
addresses:update_string("recipient_emails[0]", address)
success = http:http_json("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions", addresses, "application/json", resp)
if not success then
if resp.status_code == 0 then
print(http.last_error_text)
else
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
end
return
end
if resp.status_code ~= 201 then
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
return
end
success = http:http_json("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search", addresses, "application/json", resp)
if not success then
if resp.status_code == 0 then
print(http.last_error_text)
else
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
end
return
end
if resp.status_code ~= 200 then
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
return
end
local matches = chilkat.JsonArray.new()
success = matches:load(resp.body_str)
if not success then
print("Unexpected response: expected JSON.")
return
end
if matches.size == 1 then
print("Confirmed opt-out: " .. matches:string_at(0))
else
print("Opt-out not confirmed. Recheck this group.")
end