Scriptable RESTMessage Library to call Twilio from ServiceNow

19 Apr 2014

Using Scriptable RESTMessage Library to consume APIs from ServiceNow

Consuming RESTful APIs from within ServiceNow can be accomplished in multiple ways. Previously the easiest and most common way to do this was to use the [RESTMessage Module] 1. REST Message provides you with an easy to use UI interface by which you can build up a HTTP Request that will be used to interact with a RESTful API. In addition to the UI interface REST Message will also generate a script stub that you can use to call the REST Message record/function you have specified pragmatically. This is a really nice feature but if you are used to writing code in other environments to make HTTP Requests and consume APIs having to create a REST Message(s) for each can be a bit of a hassle and feel awkward. Thankfully there is now an easier way to accomplish this using the [Scriptable RESTMessage Library] 2 recently created by [John Andersen] 3. Scriptable RESTMessage Library is provided as a single script include that extends the existing REST Message functionality and provides a simple way to make HTTP Requests and consume APIs directly via code.

To illustrate this lets make a simple request to the GitHub API to get public info regarding a user.

var r = new RESTMessageScripted("get", "https://api.github.com/users/arbonboy");
r.addHeader("Connection", "close");
r.addHeader("Accept", "application/json; charset=utf-8");
var response = r.execute();
gs.log(response.getBody());

To run this script you must first add the Scriptable RESTMessage script include to your ServiceNow instance. Once you have this script include you can run the sample script above from a background script or any other script block.

Run Script

My helpful screenshot 1

Result

My helpful screenshot 2

The previous sample showed how to use the Scriptable RESTMessage Library to make a GET request but often times when consuming an API we need to make requests using other HTTP methods and in the case of PUT or POST include a message body. Let's see a sample that makes a request including a message body. For this example let's send a SMS message using the [Twilio API] 4.

To send an SMS Message using Twilio's API you must have an account (free development accounts can signed up for) and provide your credentials via Basic Auth for each request as well as use your accounts phone number as the 'From' number in SMS Messages. That being said here is simple example using Scriptable RESTMessage to send an SMS Message. Note that I have blanked out my Twilio credentials from the request, just update those with yours and you should be good to go.

var url = 'https://api.twilio.com/2010-04-01/Accounts/ACa154401a0292776522d6a3f884b7b579/Messages.json';
var method = 'post';
var r = new RESTMessageScripted(method, url);
var body = 'Test+SMS+from+ServiceNow+Background+Script&From=%2B13126983252&To=%2B13125501242';

r.addHeader('Authorization', 'Basic {insert your Twilio Credentials Here');
r.addHeader('Host', 'api.twilio.com');
r.addHeader('Cache-Control', 'no-cache');
r.addHeader('Connection', 'close');
r.addHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
r.setContent('Body='+ body);
var response = r.execute();
gs.log(response.getBody(), 'rest_test');