Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Send an SMS with Twilio

See more Twilio Examples

Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.

For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.

Twilio Messages API reference

Chilkat Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_basic_auth(true);
// Use the test Account SID and test Auth Token from the Twilio Console.
let account_sid = "YOUR_TEST_ACCOUNT_SID".to_string();
http.set_login(&account_sid);
http.set_password("YOUR_TEST_AUTH_TOKEN");
let url = chilkat::StringBuilder::new();
let _ = url.append("https://api.twilio.com/2010-04-01/Accounts/");
let _ = url.append(&account_sid);
let _ = url.append("/Messages.json");

// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("From", "+15005550006");
req.add_param("To", "+12025550123");
req.add_param("Body", "Hello from Twilio!");

let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
success = http.http_req(&url.get_as_string().unwrap_or_default(), &req, &resp).is_ok();
if resp.status_code() == 0 {
    println!("{}", http.last_error_text());
    return;
}

let parsed = reply.load(&resp.body_str()).is_ok();
if (!success) || (resp.status_code() != 201) {
    println!("HTTP {}", resp.status_code());
    if (parsed) && (reply.has_member("code")) {
        println!("Twilio error {}: {}", reply.int_of("code"), reply.string_of("message").unwrap_or_default());
    }

    return;
}

if !parsed {
    println!("Expected a JSON response.");
    return;
}

if (reply.json_type_of("sid") != 1) || (reply.json_type_of("status") != 1) {
    println!("The create response is missing the message SID or status.");
    return;
}

println!("Message SID: {}", reply.string_of("sid").unwrap_or_default());
println!("Initial status: {}", reply.string_of("status").unwrap_or_default());