Sample code for 30+ languages & platforms
Lua 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 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
-- Supply the key for this One API integration from application configuration.
http:set_request_header("Authorization", "ONE_API_KEY")
http:set_request_header("Accept", "application/json")

local body = chilkat.JsonObject.new()
body:update_string("messages[0].channel", "sms")
body:update_string("messages[0].to", "PHONE_NUMBER")
body:update_string("messages[0].content", "Test message: your order is ready.")
-- For a two-way sender, supply the number provisioned on the integration.
local sender = ""
if sender ~= "" then
    body:update_string("messages[0].from", sender)
end

local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://platform.clickatell.com/v1/message", body, "application/json", 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) and (resp.status_code ~= 207)) then
    print("Clickatell returned HTTP " .. resp.status_code)
    if parsed then
        if reply:has_member("error.description") then
            print(reply:string_of("error.description"))
        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("error.code") then
    if not reply:is_null_of("error.code") then
        print("Submission error " .. reply:string_of("error.code") .. "  " .. reply:string_of("error.description"))
        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].error.code") .. "  " .. reply:string_of("messages[i].error.description"))
    end

    i = i + 1
end