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

Clickatell HTTP API: Send an SMS with query parameters

See more Clickatell Examples

Use an SMS integration configured for HTTP in the Clickatell Portal. The endpoint takes apiKey, to, and content as query parameters. Pass raw values to Chilkat so it can encode spaces, ampersands, and other reserved characters. Supply a country code and phone number as digits. Set from when your two-way integration requires a sender.

The URL contains the API key, recipient, and message text. Keep full request URLs out of logs and browser history. Check the per-message result after HTTP 202. If the connection is interrupted, check the Portal message report before resending. For the header-authenticated JSON workflow, see Send an SMS with One API.

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)

#  Use the key issued for this HTTP integration. Chilkat encodes each query value.
query = Chilkat::CkHttpRequest.new()
query.put_HttpVerb("GET")
query.AddHeader("Accept","application/json")
query.AddParam("apiKey","HTTP_API_KEY")
query.AddParam("to","PHONE_NUMBER")
query.AddParam("content","Test message: pickup between 2 & 4.")
sender = ""
if (sender != "")
    query.AddParam("from",sender)
end

resp = Chilkat::CkHttpResponse.new()
success = http.HttpReq("https://platform.clickatell.com/messages/http/send",query,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)))
    print "Clickatell returned HTTP " + resp.get_StatusCode().to_s() + "\n";
    if (parsed == true)
        if (reply.HasMember("errorDescription") == true)
            print reply.stringOf("errorDescription") + "\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("errorCode") == true)
    if (reply.IsNullOf("errorCode") != true)
        print "Submission error " + reply.stringOf("errorCode") + "  " + reply.stringOf("errorDescription") + "\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].errorCode") + "  " + reply.stringOf("messages[i].errorDescription") + "\n";
        end

    end

    i = i + 1
end