Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Obtain these credentials from the application's private configuration.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.basic_auth = true
http.login = "YOUR_API_KEY_SID"
http.password = "YOUR_API_KEY_SECRET"
-- Both requests use the same Verify Service SID (VA...).
local url = chilkat.StringBuilder.new()
url:append("https://verify.twilio.com/v2/Services/")
url:append("YOUR_VERIFY_SERVICE_SID")
url:append("/Verifications")
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.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")
local resp = chilkat.HttpResponse.new()
local success = http:http_req(url:get_as_string(), req, resp)
if resp.status_code == 0 then
print("No response from Twilio. Check connectivity and TLS settings.")
return
end
local result = chilkat.JsonObject.new()
local parsed = result:load(resp.body_str)
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
print("Twilio returned HTTP " .. resp.status_code)
if (parsed) and (result:json_type_of("code") == 2) then
print("Twilio error code: " .. result:int_of("code"))
end
return
end
if (not parsed) or (result:json_type_of("status") ~= 1) then
print("Verification status missing from the response.")
return
end
local status = result:string_of("status")
if status ~= "pending" then
print("Verification status: " .. status)
return
end
if (result:json_type_of("sid") ~= 1) or (result:string_of("sid") == "") then
print("The pending response did not include a verification SID.")
return
end
-- Save this SID with the pending attempt before prompting the user for the code.
print("Pending verification SID: " .. result:string_of("sid"))