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

Send an SMS with Twilio

See more Twilio Examples

Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.

For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.

Twilio Messages API reference

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    const char *accountSid;
    HCkStringBuilder url;
    HCkHttpRequest req;
    HCkHttpResponse resp;
    HCkJsonObject reply;
    BOOL parsed;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    CkHttp_putBasicAuth(http,TRUE);
    //  Use the test Account SID and test Auth Token from the Twilio Console.
    accountSid = "YOUR_TEST_ACCOUNT_SID";
    CkHttp_putLogin(http,accountSid);
    CkHttp_putPassword(http,"YOUR_TEST_AUTH_TOKEN");
    url = CkStringBuilder_Create();
    CkStringBuilder_Append(url,"https://api.twilio.com/2010-04-01/Accounts/");
    CkStringBuilder_Append(url,accountSid);
    CkStringBuilder_Append(url,"/Messages.json");

    //  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
    req = CkHttpRequest_Create();
    CkHttpRequest_putHttpVerb(req,"POST");
    CkHttpRequest_putContentType(req,"application/x-www-form-urlencoded");
    CkHttpRequest_AddParam(req,"From","+15005550006");
    CkHttpRequest_AddParam(req,"To","+12025550123");
    CkHttpRequest_AddParam(req,"Body","Hello from Twilio!");

    resp = CkHttpResponse_Create();
    reply = CkJsonObject_Create();
    success = CkHttp_HttpReq(http,CkStringBuilder_getAsString(url),req,resp);
    if (CkHttpResponse_getStatusCode(resp) == 0) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    parsed = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
    if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 201)) {
        printf("HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        if ((parsed == TRUE) && (CkJsonObject_HasMember(reply,"code") == TRUE)) {
            printf("Twilio error %d: %s\n",CkJsonObject_IntOf(reply,"code"),CkJsonObject_stringOf(reply,"message"));
        }

        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    if (parsed != TRUE) {
        printf("Expected a JSON response.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(reply,"sid") != 1) || (CkJsonObject_JsonTypeOf(reply,"status") != 1)) {
        printf("The create response is missing the message SID or status.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    printf("Message SID: %s\n",CkJsonObject_stringOf(reply,"sid"));
    printf("Initial status: %s\n",CkJsonObject_stringOf(reply,"status"));


    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(url);
    CkHttpRequest_Dispose(req);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(reply);

    }