DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoUrl
Variant vReq
Handle hoReq
Variant vResp
Handle hoResp
Handle hoResult
Boolean iParsed
String sStatus
String sTemp1
Integer iTemp1
Integer iTemp2
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComBasicAuth Of hoHttp To True
Set ComLogin Of hoHttp To "YOUR_API_KEY_SID"
Set ComPassword Of hoHttp To "YOUR_API_KEY_SECRET"
// Both requests use the same Verify Service SID (VA...).
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComAppend Of hoUrl "https://verify.twilio.com/v2/Services/" To iSuccess
Get ComAppend Of hoUrl "YOUR_VERIFY_SERVICE_SID" To iSuccess
Get ComAppend Of hoUrl "/Verifications" To iSuccess
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
// Supply the number the user asked to verify. AddParam encodes the leading plus sign.
Send ComAddParam To hoReq "To" "PHONE_NUMBER_IN_E164_FORMAT"
Send ComAddParam To hoReq "Channel" "sms"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sTemp1 vReq vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No response from Twilio. Check connectivity and TLS settings."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iParsed
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Twilio returned HTTP " iTemp1
Get ComJsonTypeOf Of hoResult "code" To iTemp1
If ((iParsed = True) And (iTemp1 = 2)) Begin
Get ComIntOf Of hoResult "code" To iTemp1
Showln "Twilio error code: " iTemp1
End
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "status" To iTemp1
If ((iParsed <> True) Or (iTemp1 <> 1)) Begin
Showln "Verification status missing from the response."
Procedure_Return
End
Get ComStringOf Of hoResult "status" To sStatus
If (sStatus <> "pending") Begin
Showln "Verification status: " sStatus
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "sid" To iTemp1
Get ComStringOf Of hoResult "sid" To sTemp1
If ((iTemp1 <> 1) Or (sTemp1 = "")) Begin
Showln "The pending response did not include a verification SID."
Procedure_Return
End
// Save this SID with the pending attempt before prompting the user for the code.
Get ComStringOf Of hoResult "sid" To sTemp1
Showln "Pending verification SID: " sTemp1
End_Procedure