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

Send an SMS with Twilio

See more Twilio Examples

Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.

For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.

Twilio Messages 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
http.basic_auth = true
-- Use the test Account SID and test Auth Token from the Twilio Console.
local accountSid = "YOUR_TEST_ACCOUNT_SID"
http.login = accountSid
http.password = "YOUR_TEST_AUTH_TOKEN"
local url = chilkat.StringBuilder.new()
url:append("https://api.twilio.com/2010-04-01/Accounts/")
url:append(accountSid)
url:append("/Messages.json")

-- Add raw field values. Chilkat form-encodes them, including the + in a phone number.
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("From", "+15005550006")
req:add_param("To", "+12025550123")
req:add_param("Body", "Hello from Twilio!")

local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local success = http:http_req(url:get_as_string(), req, resp)
if resp.status_code == 0 then
    print(http.last_error_text)
    return
end

local parsed = reply:load(resp.body_str)
if (not success) or (resp.status_code ~= 201) then
    print("HTTP " .. resp.status_code)
    if (parsed) and (reply:has_member("code")) then
        print("Twilio error " .. reply:int_of("code") .. ": " .. reply:string_of("message"))
    end

    return
end

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

if (reply:json_type_of("sid") ~= 1) or (reply:json_type_of("status") ~= 1) then
    print("The create response is missing the message SID or status.")
    return
end

print("Message SID: " .. reply:string_of("sid"))
print("Initial status: " .. reply:string_of("status"))