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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true

-- Use the key issued for this HTTP integration. Chilkat encodes each query value.
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_header("Accept", "application/json")
query:add_param("apiKey", "HTTP_API_KEY")
query:add_param("to", "PHONE_NUMBER")
query:add_param("content", "Test message: pickup between 2 & 4.")
local sender = ""
if sender ~= "" then
    query:add_param("from", sender)
end

local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://platform.clickatell.com/messages/http/send", query, resp)
if resp.status_code == 0 then
    print("The send outcome is unknown. Check the message report before resending.")
    return
end

local reply = chilkat.JsonObject.new()
local parsed = reply:load(resp.body_str)
if (not success) or ((resp.status_code ~= 202)) then
    print("Clickatell returned HTTP " .. resp.status_code)
    if parsed then
        if reply:has_member("errorDescription") then
            print(reply:string_of("errorDescription"))
        end
    end

    return
end

if not parsed then
    print("Expected a JSON response.")
    return
end

-- A request-level error applies to the submission as a whole.
if reply:has_member("errorCode") then
    if not reply:is_null_of("errorCode") then
        print("Submission error " .. reply:string_of("errorCode") .. "  " .. reply:string_of("errorDescription"))
        return
    end
end

local count = reply:size_of_array("messages")
if count <= 0 then
    print("No recipient results returned. Check the message report before resending.")
    return
end

local i = 0
while i < count do
    reply.i = i
    local resultNumber = i + 1
    -- JsonTypeOf returns 5 for a JSON boolean.
    if reply:json_type_of("messages[i].accepted") ~= 5 then
        print("Result " .. resultNumber .. " has no acceptance flag.")
    elseif reply:bool_of("messages[i].accepted") then
        local messageId = reply:string_of("messages[i].apiMessageId")
        if (reply:json_type_of("messages[i].apiMessageId") ~= 1) or (messageId == "") then
            print("Result " .. resultNumber .. " was accepted without a tracking ID.")
        else
            print("Result " .. resultNumber .. " accepted. Message ID: " .. messageId)
        end
    else
        print("Result " .. resultNumber .. " rejected: " .. reply:string_of("messages[i].errorCode") .. "  " .. reply:string_of("messages[i].errorDescription"))
    end

    i = i + 1
end