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

Clickatell One API: Send an SMS and get its message ID

See more Clickatell Examples

Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.

HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.

Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference

Clickatell API reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    //  Supply the key for this One API integration from application configuration.
    http.SetRequestHeader(L"Authorization",L"ONE_API_KEY");
    http.SetRequestHeader(L"Accept",L"application/json");

    CkJsonObjectW body;
    body.UpdateString(L"messages[0].channel",L"sms");
    body.UpdateString(L"messages[0].to",L"PHONE_NUMBER");
    body.UpdateString(L"messages[0].content",L"Test message: your order is ready.");
    //  For a two-way sender, supply the number provisioned on the integration.
    const wchar_t *sender = L"";
    if (sender != L"") {
        body.UpdateString(L"messages[0].from",sender);
    }

    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://platform.clickatell.com/v1/message",body,L"application/json",resp);
    if (resp.get_StatusCode() == 0) {
        wprintf(L"The send outcome is unknown. Check the message report before resending.\n");
        return;
    }

    CkJsonObjectW reply;
    bool parsed = reply.Load(resp.bodyStr());
    if ((success != true) || ((resp.get_StatusCode() != 202) && (resp.get_StatusCode() != 207))) {
        wprintf(L"Clickatell returned HTTP %d\n",resp.get_StatusCode());
        if (parsed == true) {
            if (reply.HasMember(L"error.description") == true) {
                wprintf(L"%s\n",reply.stringOf(L"error.description"));
            }

        }

        return;
    }

    if (parsed != true) {
        wprintf(L"Expected a JSON response.\n");
        return;
    }

    //  A request-level error applies to the submission as a whole.
    if (reply.HasMember(L"error.code") == true) {
        if (reply.IsNullOf(L"error.code") != true) {
            wprintf(L"Submission error %s  %s\n",reply.stringOf(L"error.code"),reply.stringOf(L"error.description"));
            return;
        }

    }

    int count = reply.SizeOfArray(L"messages");
    if (count <= 0) {
        wprintf(L"No recipient results returned. Check the message report before resending.\n");
        return;
    }

    int i = 0;
    while ((i < count)) {
        reply.put_I(i);
        int resultNumber = i + 1;
        //  JsonTypeOf returns 5 for a JSON boolean.
        if (reply.JsonTypeOf(L"messages[i].accepted") != 5) {
            wprintf(L"Result %d has no acceptance flag.\n",resultNumber);
        }
        else {
            if (reply.BoolOf(L"messages[i].accepted") == true) {
                const wchar_t *messageId = reply.stringOf(L"messages[i].apiMessageId");
                if ((reply.JsonTypeOf(L"messages[i].apiMessageId") != 1) || (messageId == L"")) {
                    wprintf(L"Result %d was accepted without a tracking ID.\n",resultNumber);
                }
                else {
                    wprintf(L"Result %d accepted. Message ID: %s\n",resultNumber,messageId);
                }

            }
            else {
                wprintf(L"Result %d rejected: %s  %s\n",resultNumber,reply.stringOf(L"messages[i].error.code"),reply.stringOf(L"messages[i].error.description"));
            }

        }

        i = i + 1;
    }
    }