Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oUrl
LOCAL oReq
LOCAL oResp
LOCAL oResult
LOCAL nParsed
LOCAL cStatus
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
oHttp:Login := "YOUR_API_KEY_SID"
oHttp:Password := "YOUR_API_KEY_SECRET"
// Both requests use the same Verify Service SID (VA...).
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://verify.twilio.com/v2/Services/")
oUrl:Append("YOUR_VERIFY_SERVICE_SID")
oUrl:Append("/Verifications")
oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"
// Supply the number the user asked to verify. AddParam encodes the leading plus sign.
oReq:AddParam("To", "PHONE_NUMBER_IN_E164_FORMAT")
oReq:AddParam("Channel", "sms")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq(oUrl:GetAsString(), oReq, oResp)
IF (oResp:StatusCode == 0)
? "No response from Twilio. Check connectivity and TLS settings."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
RETURN
ENDIF
oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. (oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
? "Twilio returned HTTP " + Str(oResp:StatusCode)
IF ((nParsed == 1) .AND. (oResult:JsonTypeOf("code") == 2))
? "Twilio error code: " + Str(oResult:IntOf("code"))
ENDIF
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
IF ((nParsed != 1) .OR. (oResult:JsonTypeOf("status") != 1))
? "Verification status missing from the response."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
cStatus := oResult:StringOf("status")
IF (cStatus != "pending")
? "Verification status: " + cStatus
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
IF ((oResult:JsonTypeOf("sid") != 1) .OR. (oResult:StringOf("sid") == ""))
? "The pending response did not include a verification SID."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
// Save this SID with the pending attempt before prompting the user for the code.
? "Pending verification SID: " + oResult:StringOf("sid")
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()