Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Obtain these credentials from the application's private configuration.
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_BasicAuth(true)
http.put_Login("YOUR_API_KEY_SID")
http.put_Password("YOUR_API_KEY_SECRET")
# Both requests use the same Verify Service SID (VA...).
url = Chilkat::CkStringBuilder.new()
url.Append("https://verify.twilio.com/v2/Services/")
url.Append("YOUR_VERIFY_SERVICE_SID")
url.Append("/Verifications")
req = Chilkat::CkHttpRequest.new()
req.put_HttpVerb("POST")
req.put_ContentType("application/x-www-form-urlencoded")
# Supply the number the user asked to verify. AddParam encodes the leading plus sign.
req.AddParam("To","PHONE_NUMBER_IN_E164_FORMAT")
req.AddParam("Channel","sms")
resp = Chilkat::CkHttpResponse.new()
success = http.HttpReq(url.getAsString(),req,resp)
if (resp.get_StatusCode() == 0)
print "No response from Twilio. Check connectivity and TLS settings." + "\n";
exit
end
result = Chilkat::CkJsonObject.new()
parsed = result.Load(resp.bodyStr())
if ((success != true) or (resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
print "Twilio returned HTTP " + resp.get_StatusCode().to_s() + "\n";
if ((parsed == true) and (result.JsonTypeOf("code") == 2))
print "Twilio error code: " + result.IntOf("code").to_s() + "\n";
end
exit
end
if ((parsed != true) or (result.JsonTypeOf("status") != 1))
print "Verification status missing from the response." + "\n";
exit
end
status = result.stringOf("status")
if (status != "pending")
print "Verification status: " + status + "\n";
exit
end
if ((result.JsonTypeOf("sid") != 1) or (result.stringOf("sid") == ""))
print "The pending response did not include a verification SID." + "\n";
exit
end
# Save this SID with the pending attempt before prompting the user for the code.
print "Pending verification SID: " + result.stringOf("sid") + "\n";