Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

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.

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;
    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"/Verifications");

    req = CkHttpRequestW_Create();
    CkHttpRequestW_putHttpVerb(req,L"POST");
    CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded");
    //  Supply the number the user asked to verify. AddParam encodes the leading plus sign.
    CkHttpRequestW_AddParam(req,L"To",L"PHONE_NUMBER_IN_E164_FORMAT");
    CkHttpRequestW_AddParam(req,L"Channel",L"sms");

    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;
    }

    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"pending") {
        wprintf(L"Verification status: %s\n",status);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(result);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(result,L"sid") != 1) || (CkJsonObjectW_stringOf(result,L"sid") == L"")) {
        wprintf(L"The pending response did not include a verification SID.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(result);
        return;
    }

    //  Save this SID with the pending attempt before prompting the user for the code.
    wprintf(L"Pending verification SID: %s\n",CkJsonObjectW_stringOf(result,L"sid"));


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

    }