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

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.

Twilio Verify API reference

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Obtain these credentials from the application's private configuration.
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_BasicAuth $http 1
CkHttp_put_Login $http "YOUR_API_KEY_SID"
CkHttp_put_Password $http "YOUR_API_KEY_SECRET"

#  Both requests use the same Verify Service SID (VA...).
set url [new_CkStringBuilder]

CkStringBuilder_Append $url "https://verify.twilio.com/v2/Services/"
CkStringBuilder_Append $url "YOUR_VERIFY_SERVICE_SID"
CkStringBuilder_Append $url "/Verifications"

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
#  Supply the number the user asked to verify. AddParam encodes the leading plus sign.
CkHttpRequest_AddParam $req "To" "PHONE_NUMBER_IN_E164_FORMAT"
CkHttpRequest_AddParam $req "Channel" "sms"

set resp [new_CkHttpResponse]

set success [CkHttp_HttpReq $http [CkStringBuilder_getAsString $url] $req $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "No response from Twilio. Check connectivity and TLS settings."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    exit
}

set result [new_CkJsonObject]

set parsed [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $resp] < 200]  ||  [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
    puts "Twilio returned HTTP [CkHttpResponse_get_StatusCode $resp]"
    if {expr [$parsed == 1]  &&  [[CkJsonObject_JsonTypeOf $result "code"] == 2]} then {
        puts "Twilio error code: [CkJsonObject_IntOf $result code]"
    }

    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    exit
}

if {expr [$parsed != 1]  ||  [[CkJsonObject_JsonTypeOf $result "status"] != 1]} then {
    puts "Verification status missing from the response."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    exit
}

set status [CkJsonObject_stringOf $result "status"]
if {$status != "pending"} then {
    puts "Verification status: $status"
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $result "sid"] != 1]  ||  [[CkJsonObject_stringOf $result "sid"] == ""]} then {
    puts "The pending response did not include a verification SID."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    exit
}

#  Save this SID with the pending attempt before prompting the user for the code.
puts "Pending verification SID: [CkJsonObject_stringOf $result sid]"

delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $result