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

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkStringBuilderW url;
    HCkHttpRequestW req;
    const wchar_t *verificationSid;
    const wchar_t *enteredCode;
    HCkHttpResponseW resp;
    HCkJsonObjectW result;
    BOOL parsed;
    const wchar_t *status;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Obtain these credentials from the application's private configuration.
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putBasicAuth(http,TRUE);
    CkHttpW_putLogin(http,L"YOUR_API_KEY_SID");
    CkHttpW_putPassword(http,L"YOUR_API_KEY_SECRET");

    //  Both requests use the same Verify Service SID (VA...).
    url = CkStringBuilderW_Create();
    CkStringBuilderW_Append(url,L"https://verify.twilio.com/v2/Services/");
    CkStringBuilderW_Append(url,L"YOUR_VERIFY_SERVICE_SID");
    CkStringBuilderW_Append(url,L"/VerificationCheck");

    req = CkHttpRequestW_Create();
    CkHttpRequestW_putHttpVerb(req,L"POST");
    CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded");
    //  Load the saved SID for this user's pending attempt. Take only the code from user input.
    verificationSid = L"SAVED_VERIFICATION_SID";
    enteredCode = L"CODE_ENTERED_BY_USER";
    CkHttpRequestW_AddParam(req,L"VerificationSid",verificationSid);
    CkHttpRequestW_AddParam(req,L"Code",enteredCode);

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpReq(http,CkStringBuilderW_getAsString(url),req,resp);
    if (CkHttpResponseW_getStatusCode(resp) == 0) {
        wprintf(L"No response from Twilio. Check connectivity and TLS settings.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    if (CkHttpResponseW_getStatusCode(resp) == 404) {
        wprintf(L"No active verification found. Check the service and verification SIDs.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    result = CkJsonObjectW_Create();
    parsed = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
        wprintf(L"Twilio returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        if ((parsed == TRUE) && (CkJsonObjectW_JsonTypeOf(result,L"code") == 2)) {
            wprintf(L"Twilio error code: %d\n",CkJsonObjectW_IntOf(result,L"code"));
        }

        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(result);
        return;
    }

    if ((parsed != TRUE) || (CkJsonObjectW_JsonTypeOf(result,L"status") != 1)) {
        wprintf(L"Verification status missing from the response.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(result);
        return;
    }

    status = CkJsonObjectW_stringOf(result,L"status");
    if (status == L"approved") {
        //  Record success for the user and phone number associated with verificationSid.
        wprintf(L"Phone number verified.\n");
    }
    else {
        if (status == L"pending") {
            wprintf(L"Code not accepted. Check the digits and enter the code again.\n");
        }
        else {
            wprintf(L"Phone number unverified. Verification status: %s\n",status);
        }

    }



    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(url);
    CkHttpRequestW_Dispose(req);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(result);

    }