I’m excited to announce the availability of the ColdFusion Helper Library for Twilio. This helper provides an easy to use and natural way for CFML developers to make calls to the Twilio REST API as well as create TwiML responses. Another cool feature of the helper is an API explorer, which allows you to browse the entire Twilio REST API and make calls simply by filling in form fields. This is a great way to learn about the Twilio API and to make sample calls and view their responses. This helper library also contains functionality to validate Twilio requests, helping to secure your app and saving you implementation time. You can also use ColdFusion to generate Capability Tokens for Twilio Client. It’s a great day to be a ColdFusion developer!
The ColdFusion Helper can be downloaded here. After unzipping the archive, you’ll notice the included Application.cfc performs a couple of helpful tasks that would otherwise be repetitive. Application.cfc will include the file ‘TwilioSettings.cfm’ in each request. This file contains your Twilio account credentials, so you will want to edit it and fill in your own Account Sid and Auth Token. These values can be found under ‘Developer Tools’ on the Dashboard page once logging into your Twilio account.
Contents of TwilioSettings.cfm:
You’ll also notice that the Application.cfc file creates an instance of the TwilioLib cfc and places it in the request scope so your Twilio helper object will be instantiated, ready and waiting for you when you need it. You can feel free to incorporate these items into your own Application.cfc (or Application.cfm), or if you’d prefer you can selectively perform these tasks on the pages you want to use Twilio helper.
Making REST Calls
Placing a REST call with the ColdFusion helper library is done using the TwilioLib.newRequest function. This function takes 3 parameters: the path to the resource, the HTTP method and lastly a Struct with the desired REST parameters. Below is example code for making a call using the Twilio REST API.
The ColdFusion helper has great support for creating the XML responses we fondly call TwiML. Using the helper library you have the benefit of validation of required attributes and nesting rules, as well as cfc hint text which reveals the possible attributes and help text for each TwiML tag. TwiML is built by calling TwilioLib.newResponse, which returns an instance of the TwiML cfc. The TwiML cfc contain a function for each TwiML tag, and calling a function appends TwiML to the response. Here’s a pro tip – TwiML method calls return the TwiML object – so you can chain calls together to save time! Here is a sample TwiML response:
ColdFusion and Twilio Client
You can use ColdFusion with Twilio Client to place and receive browser based calls. In order to generate Capability Tokens needed for Twilio Client, we can use the capability object. Below you will find sample code for creating the Capability Token:
More ColdFusion Examples
You can find more Twilio ColdFusion code examples in our HowTos section.
One of the most exciting features of the helper library is the included API explorer, called Test Console. Browsing to the file at console/index.cfm you will find a menu that lists all Twilio REST resources. Clicking on a particular resource will drop down a list of all available methods. Clicking on a particular method will show a new tab with form fields for each REST parameter. You can fill out the form fields and submit the form to make actual calls against the Twilio API. In the screenshot I’ve used Test Console to send an SMS message through Twillio REST API. The resulting XML response is displayed along with the ColdFusion code that was executed. The Test Console will actually write Twilio ColdFusion code for you, how sweet is that!
Our thanks to the author
Many thanks go out to Jason Fill, the author and Twilio developer who wrote the ColdFusion Helper Library. From the start I was impressed with the thought Jason put into his coding. He created a really well thought out object design, and I always find a good OO design implemented in cfc impressive. I also appreciate the coding techniques used, such as allowing for cfc method chaining and calls out to the Java crypto library for request validation. The API explorer is the icing on the cake – it’s neat in that it exposes so much functionality and is written in a small amount of code. CF Magic indeed! Thanks Jason, for providing this great open source tool to the Twilio and ColdFusion communities.
Photo credit: Mingo.nl