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

Ruby
require 'chilkat'

success = false

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

body = Chilkat::CkJsonObject.new()
body.UpdateString("messages[0].channel","sms")
body.UpdateString("messages[0].to","PHONE_NUMBER")
body.UpdateString("messages[0].content","Test message: your order is ready.")
#  For a two-way sender, supply the number provisioned on the integration.
sender = ""
if (sender != "")
    body.UpdateString("messages[0].from",sender)
end

resp = Chilkat::CkHttpResponse.new()
success = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp)
if (resp.get_StatusCode() == 0)
    print "The send outcome is unknown. Check the message report before resending." + "\n";
    exit
end

reply = Chilkat::CkJsonObject.new()
parsed = reply.Load(resp.bodyStr())
if ((success != true) or ((resp.get_StatusCode() != 202) and (resp.get_StatusCode() != 207)))
    print "Clickatell returned HTTP " + resp.get_StatusCode().to_s() + "\n";
    if (parsed == true)
        if (reply.HasMember("error.description") == true)
            print reply.stringOf("error.description") + "\n";
        end

    end

    exit
end

if (parsed != true)
    print "Expected a JSON response." + "\n";
    exit
end

#  A request-level error applies to the submission as a whole.
if (reply.HasMember("error.code") == true)
    if (reply.IsNullOf("error.code") != true)
        print "Submission error " + reply.stringOf("error.code") + "  " + reply.stringOf("error.description") + "\n";
        exit
    end

end

count = reply.SizeOfArray("messages")
if (count <= 0)
    print "No recipient results returned. Check the message report before resending." + "\n";
    exit
end

i = 0
while (i < count)
    reply.put_I(i)
    resultNumber = i + 1
    #  JsonTypeOf returns 5 for a JSON boolean.
    if (reply.JsonTypeOf("messages[i].accepted") != 5)
        print "Result " + resultNumber.to_s() + " has no acceptance flag." + "\n";
    else
        if (reply.BoolOf("messages[i].accepted") == true)
            messageId = reply.stringOf("messages[i].apiMessageId")
            if ((reply.JsonTypeOf("messages[i].apiMessageId") != 1) or (messageId == ""))
                print "Result " + resultNumber.to_s() + " was accepted without a tracking ID." + "\n";
            else
                print "Result " + resultNumber.to_s() + " accepted. Message ID: " + messageId + "\n";
            end

        else
            print "Result " + resultNumber.to_s() + " rejected: " + reply.stringOf("messages[i].error.code") + "  " + reply.stringOf("messages[i].error.description") + "\n";
        end

    end

    i = i + 1
end