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

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loUrl
LOCAL loReq
LOCAL lcVerificationSid
LOCAL lcEnteredCode
LOCAL loResp
LOCAL loResult
LOCAL lnParsed
LOCAL lcStatus

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Obtain these credentials from the application's private configuration.
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.BasicAuth = 1
loHttp.Login = "YOUR_API_KEY_SID"
loHttp.Password = "YOUR_API_KEY_SECRET"

*  Both requests use the same Verify Service SID (VA...).
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://verify.twilio.com/v2/Services/")
loUrl.Append("YOUR_VERIFY_SERVICE_SID")
loUrl.Append("/VerificationCheck")

loReq = CreateObject('Chilkat.HttpRequest')
loReq.HttpVerb = "POST"
loReq.ContentType = "application/x-www-form-urlencoded"
*  Load the saved SID for this user's pending attempt. Take only the code from user input.
lcVerificationSid = "SAVED_VERIFICATION_SID"
lcEnteredCode = "CODE_ENTERED_BY_USER"
loReq.AddParam("VerificationSid",lcVerificationSid)
loReq.AddParam("Code",lcEnteredCode)

loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpReq(loUrl.GetAsString(),loReq,loResp)
IF (loResp.StatusCode = 0) THEN
    ? "No response from Twilio. Check connectivity and TLS settings."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loResp
    CANCEL
ENDIF

IF (loResp.StatusCode = 404) THEN
    ? "No active verification found. Check the service and verification SIDs."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loResp
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsed = loResult.Load(loResp.BodyStr)
IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    ? "Twilio returned HTTP " + STR(loResp.StatusCode)
    IF ((lnParsed = 1) AND (loResult.JsonTypeOf("code") = 2)) THEN
        ? "Twilio error code: " + STR(loResult.IntOf("code"))
    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF ((lnParsed <> 1) OR (loResult.JsonTypeOf("status") <> 1)) THEN
    ? "Verification status missing from the response."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

lcStatus = loResult.StringOf("status")
IF (lcStatus = "approved") THEN
    *  Record success for the user and phone number associated with verificationSid.
    ? "Phone number verified."
ELSE
    IF (lcStatus = "pending") THEN
        ? "Code not accepted. Check the digits and enter the code again."
    ELSE
        ? "Phone number unverified. Verification status: " + lcStatus
    ENDIF

ENDIF

RELEASE loHttp
RELEASE loUrl
RELEASE loReq
RELEASE loResp
RELEASE loResult