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

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.set_auth_token("SENDGRID_API_KEY");
let resp = chilkat::HttpResponse::new();

// Take the address from the authenticated account and the group ID from configuration.
let address = "reader@example.com".to_string();
let _ = http.set_url_var("group", "12345");

if http.http_no_body("GET", "https://api.sendgrid.com/v3/asm/groups/{$group}", &resp).is_err() {
    if resp.status_code() == 0 {
        println!("{}", http.last_error_text());
    } else {
        println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    }

    return;
}

if resp.status_code() != 200 {
    println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    return;
}

let group_info = chilkat::JsonObject::new();
if group_info.load(&resp.body_str()).is_err() {
    println!("Unexpected response: expected JSON.");
    return;
}

if !group_info.string_of_equals("name", "Product updates", true) {
    println!("Unexpected unsubscribe group; no preference was changed.");
    return;
}

let addresses = chilkat::JsonObject::new();
let _ = addresses.update_string("recipient_emails[0]", &address);

if http.http_json("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions", &addresses, "application/json", &resp).is_err() {
    if resp.status_code() == 0 {
        println!("{}", http.last_error_text());
    } else {
        println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    }

    return;
}

if resp.status_code() != 201 {
    println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    return;
}

if http.http_json("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search", &addresses, "application/json", &resp).is_err() {
    if resp.status_code() == 0 {
        println!("{}", http.last_error_text());
    } else {
        println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    }

    return;
}

if resp.status_code() != 200 {
    println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    return;
}

let matches = chilkat::JsonArray::new();
if matches.load(&resp.body_str()).is_err() {
    println!("Unexpected response: expected JSON.");
    return;
}

if matches.size() == 1 {
    println!("Confirmed opt-out: {}", matches.string_at(0).unwrap_or_default());
} else {
    println!("Opt-out not confirmed. Recheck this group.");
}