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

Send an MMS with Twilio using a media URL

See more Twilio Examples

Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.

Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.

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
-- Load the owning Account SID and an API key from application configuration.
local accountSid = "YOUR_ACCOUNT_SID"
http.login = "YOUR_API_KEY_SID"
http.password = "YOUR_API_KEY_SECRET"
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", "YOUR_MMS_ENABLED_NUMBER")
req:add_param("To", "RECIPIENT_E164_NUMBER")
-- Supply a URL for the file itself so Twilio can retrieve the media.
req:add_param("MediaUrl", "https://media.example.com/photo.jpg")
req:add_param("Body", "Photo attached.")

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"))