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

Check an SMS verification code with Twilio Verify

See more Twilio Examples

After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.

Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.

Twilio Verify API reference

Chilkat Rust Downloads

Rust
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("/VerificationCheck");

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
// Load the saved SID for this user's pending attempt. Take only the code from user input.
let verification_sid = "SAVED_VERIFICATION_SID".to_string();
let entered_code = "CODE_ENTERED_BY_USER".to_string();
req.add_param("VerificationSid", &verification_sid);
req.add_param("Code", &entered_code);

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;
}

if resp.status_code() == 404 {
    println!("No active verification found. Check the service and verification SIDs.");
    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 == "approved" {
    // Record success for the user and phone number associated with verificationSid.
    println!("Phone number verified.");
} else {
    if status == "pending" {
        println!("Code not accepted. Check the digits and enter the code again.");
    } else {
        println!("Phone number unverified. Verification status: {}", status);
    }

}