Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

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

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"
-- Load the saved SID for this user's pending attempt. Take only the code from user input.
local verificationSid = "SAVED_VERIFICATION_SID"
local enteredCode = "CODE_ENTERED_BY_USER"
req:add_param("VerificationSid", verificationSid)
req:add_param("Code", enteredCode)

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

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