Sample code for 30+ languages & platforms
Rust

Twilio Send SMS (using Chilkat HTTP)

See more Twilio Examples

Send an outgoing SMS message.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

// Implements the following CURL command:

// (See information about using test credentials and phone numbers:  https://www.twilio.com/docs/iam/test-credentials)

// curl -X POST https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json \
// --data-urlencode "From=+15005550006" \
// --data-urlencode "Body=body" \
// --data-urlencode "To=+15005551212" \
// -u TWILIO_ACCOUNT_SID:TWILIO_AUTH_TOKEN

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

http.set_login("TWILIO_ACCOUNT_SID");
http.set_password("TWILIO_AUTH_TOKEN");

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("From", "+15005550006");
req.add_param("Body", "body");
req.add_param("To", "+15005551212");

let resp = chilkat::HttpResponse::new();
if http.http_req("https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json", &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

// A 201 status code indicates success.
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
//   "api_version": "2010-04-01",
//   "body": "body",
//   "date_created": "Thu, 30 Jul 2015 20:12:31 +0000",
//   "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",
//   "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",
//   "direction": "outbound-api",
//   "error_code": null,
//   "error_message": null,
//   "from": "+15017122661",
//   "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
//   "num_media": "0",
//   "num_segments": "1",
//   "price": null,
//   "price_unit": null,
//   "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
//   "status": "sent",
//   "subresource_uris": {
//     "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
//   },
//   "to": "+15558675310",
//   "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

let account_sid = j_resp.string_of("account_sid").unwrap_or_default();
let api_version = j_resp.string_of("api_version").unwrap_or_default();
let body = j_resp.string_of("body").unwrap_or_default();
let date_created = j_resp.string_of("date_created").unwrap_or_default();
let date_sent = j_resp.string_of("date_sent").unwrap_or_default();
let date_updated = j_resp.string_of("date_updated").unwrap_or_default();
let direction = j_resp.string_of("direction").unwrap_or_default();
let error_code = j_resp.string_of("error_code").unwrap_or_default();
let error_message = j_resp.string_of("error_message").unwrap_or_default();
let from = j_resp.string_of("from").unwrap_or_default();
let messaging_service_sid = j_resp.string_of("messaging_service_sid").unwrap_or_default();
let num_media = j_resp.string_of("num_media").unwrap_or_default();
let num_segments = j_resp.string_of("num_segments").unwrap_or_default();
let price = j_resp.string_of("price").unwrap_or_default();
let price_unit = j_resp.string_of("price_unit").unwrap_or_default();
let sid = j_resp.string_of("sid").unwrap_or_default();
let status = j_resp.string_of("status").unwrap_or_default();
let subresource_uris_media = j_resp.string_of("subresource_uris.media").unwrap_or_default();
let v_to = j_resp.string_of("to").unwrap_or_default();
let uri = j_resp.string_of("uri").unwrap_or_default();