Skip to main content


Latest Version: 1.0.0

The Twilio library wraps Twilio’s API for sending and receiving SMS messages. To get started with this library, you will need a Twilio developer account. Once you’ve created your account, make a note of your Twilio Phone Number, Account SID and Auth Token as these are required to instantiate Twilio objects.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To add this library to your project, add #require "Twilio.class.nut:1.0.0" to the top of your agent code

Class Usage

Constructor: Twilio(accountSID, authToken, twilioNumber)

To create a new Twilio object, pass your Account SID, Auth token and Twilio Phone Number to the constructor:

#require "Twilio.class.nut:1.0.0"

twilio <- Twilio(accountSID, authToken, twilioNumber);

Class Methods

send(numberToSendTo, message[, callback])

The send() method is used to send an SMS message. An optional callback can be passed to the function — if a callback is supplied, the message will be sent asynchronously, and your callback will fire when the HTTP request completes. The callback function must include a single parameter into which will be passed a table containing three fields: statuscode, headers and body. If you don’t pass a callback, it will execute the request synchronously and return the table outlined above.

recipientsNumber <- "15555555555";
textMessage <- "Hello World!";

// Send the message synchronously
local response = twilio.send(recipientsNumber, textMessage);
server.log(response.statuscode + ": " + response.body);

// Send it again, this time asynchronously
twilio.send(recipientsNumber, textMessage, function(response) {
    server.log(response.statuscode + " - " + response.body);

respond(HTTPresponse, message)

You can respond to text messages by using the respond() method, which will generate the necessary headers and XML for Twilio to understand your response. You must pass on an impOS™ HTTPResponse object, which will be used by the library to respond to Twilio. In the following examples, we’re sending back the message we received, placed in single quote marks and preface with “You just said”. The code uses the HTTPResponse object automatically generated by impOS on receiving an HTTP request.

http.onrequest (function(request, response) {
    local path = request.path.tolower();
    if (path == "/twilio" || path == "/twilio/") {
        // Twilio request handler
        try {
            local data = http.urldecode(request.body);
            twilio.respond(response, "You just said '" + data.body + "'");
        } catch(ex) {
            local message = "Uh oh, something went horribly wrong: " + ex;
            twilio.respond(response, message);
    } else {
        // Default request handler
        response.send(200, "OK")

Note Before the above example will work, you need to configure your Twilio Phone Number:

  • Click on the phone number you would like to configure on the Manage Numbers dashboard.

  • Change the Request URL under Messaging to your agent’s URL, then click save. In the example code, we’ve tacked a /twilio to the end of the path so we know when the message is coming from Twilio:{agentID}/twilio

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release


The Twilio library is licensed under the MIT License.