B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Obtain these credentials from the application's private configuration.
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.BasicAuth = True
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"
' Both requests use the same Verify Service SID (VA...).
Dim url As ChilkatStringBuilder
url.Initialize
url.Append("https://verify.twilio.com/v2/Services/")
url.Append("YOUR_VERIFY_SERVICE_SID")
url.Append("/Verifications")
Dim req As ChilkatHttpRequest
req.Initialize
req.HttpVerb = "POST"
req.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")
Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpReq(url.GetAsString, req, resp)
If resp.StatusCode = 0 Then
Log("No response from Twilio. Check connectivity and TLS settings.")
Return
End If
Dim result As ChilkatJsonObject
result.Initialize
Dim parsed As Boolean = result.Load(resp.BodyStr)
If (success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Log("Twilio returned HTTP " & resp.StatusCode)
If (parsed = True) And (result.JsonTypeOf("code") = 2) Then
Log("Twilio error code: " & result.IntOf("code"))
End If
Return
End If
If (parsed <> True) Or (result.JsonTypeOf("status") <> 1) Then
Log("Verification status missing from the response.")
Return
End If
Dim status As String = result.StringOf("status")
If status <> "pending" Then
Log("Verification status: " & status)
Return
End If
If (result.JsonTypeOf("sid") <> 1) Or (result.StringOf("sid") = "") Then
Log("The pending response did not include a verification SID.")
Return
End If
' Save this SID with the pending attempt before prompting the user for the code.
Log("Pending verification SID: " & result.StringOf("sid"))