A Quick Look at Twilio’s New Helper Library for Salesforce

I’ve long been a Twilio fan – their API does a fine job abstracting away the details of telephony, so I, as a developer, can focus on writing great SMS and voice-oriented apps. Now, with the new Twilio Helper Library for Salesforce, it’s easier than ever to use that API from Force.com.

The official announcement has links to the library code on GitHubfull documentation, and more, but, when I got wind of the new library, I thought it might be fun to dive right in and write a simple SMS app on Force.com, allowing you to send messages from a Visualforce page:

First, let’s look at the code for the page:

<apex:page controller="TwilioPageController">
    <apex:pageBlock title="Twilio API Sample">
      <apex:pageBlockSection title="Outbound SMS" columns="1">
        <apex:pageBlockSectionItem >
          <apex:outputLabel for="phoneNumber">Phone Number:</apex:outputLabel>
          <apex:inputText id="phoneNumber" value="{!phoneNumber}"/>
        <apex:pageBlockSectionItem >
          <apex:outputLabel for="message">Message:</apex:outputLabel>
          <apex:inputText id="message" value="{!message}"/>
        <apex:commandButton value="Send SMS" action="{!sendSMS}"/>
        <apex:outputText id="status" value="{!status}"/><br/>

No surprises there – just a simple form for sending an SMS via the page controller. Let’s look at that controller:

public class TwilioPageController {
    public String status { get; set; }
    public String message { get; set; }
    public String phoneNumber { get; set; }

    TwilioAccount account = TwilioAPI.getDefaultAccount();
    String myPhoneNumber = 

    public PageReference sendSMS() {
        Map<String, String> params = new Map<String, String>{
            'From' => myPhoneNumber,
            'To' => phoneNumber,
            'Body' => message

        try {
            TwilioSms sms = account.getSmsMessages().create(params);
            status = 'Sent SMS. SID: '+sms.getSid();
        } catch (Exception e) {
            status = 'Exception: '+e.getMessage();

        phoneNumber = null;
        message = null;

        return null;

The action here is in the sendSMS() method – we create a Map containing the message parameters, send it via account.getSmsMessages().create(), and display confirmation that the message was sent. That’s it: message sent, job done.

Of course, this is only the merest scratch of the surface of the API – go check out the docs and see what you can do with Twilio on Force.com.