Rust Requires Chilkat v11.0.0+
Rust
Send an SMS verification code with Twilio Verify
See more Twilio Examples
Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.
Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_basic_auth(true);
http.set_login("YOUR_API_KEY_SID");
http.set_password("YOUR_API_KEY_SECRET");
// Both requests use the same Verify Service SID (VA...).
let url = chilkat::StringBuilder::new();
let _ = url.append("https://verify.twilio.com/v2/Services/");
let _ = url.append("YOUR_VERIFY_SERVICE_SID");
let _ = url.append("/Verifications");
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
// Supply the number the user asked to verify. AddParam encodes the leading plus sign.
req.add_param("To", "PHONE_NUMBER_IN_E164_FORMAT");
req.add_param("Channel", "sms");
let resp = chilkat::HttpResponse::new();
success = http.http_req(&url.get_as_string().unwrap_or_default(), &req, &resp).is_ok();
if resp.status_code() == 0 {
println!("No response from Twilio. Check connectivity and TLS settings.");
return;
}
let result = chilkat::JsonObject::new();
let parsed = result.load(&resp.body_str()).is_ok();
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Twilio returned HTTP {}", resp.status_code());
if (parsed) && (result.json_type_of("code") == 2) {
println!("Twilio error code: {}", result.int_of("code"));
}
return;
}
if (!parsed) || (result.json_type_of("status") != 1) {
println!("Verification status missing from the response.");
return;
}
let status = result.string_of("status").unwrap_or_default();
if status != "pending" {
println!("Verification status: {}", status);
return;
}
if (result.json_type_of("sid") != 1) || (result.string_of("sid").unwrap_or_default() == "") {
println!("The pending response did not include a verification SID.");
return;
}
// Save this SID with the pending attempt before prompting the user for the code.
println!("Pending verification SID: {}", result.string_of("sid").unwrap_or_default());