What is Twilio
Twilio is a useful and simple way to personalise communications from cloud based programs. The Twilio platform allows software developers to programmatically make and receive phone calls and send and receive text messages using its web service APIs. Go to the Twilio website and register as a user. You will need a credit card (it is not expensive to do testing) to get a number to use for this recipe.
Getting your Twilio Account number and authentication keys
To be able to use Twilio in a program, you need 3 things as a minimum. A phone number, account number and authentication key. This allows the program to gain access to the Twilio API and in the case of this example, send an SMS.
Setting up your Twilio account;
Register for a Twilio account BEFORE you integrate it with Bluemix. On registering,
1. look for a phone number to use
2. get the credentials to use your phone number
3. ensure you have the correct services you wish to use. * note the Voice SMS and MMS boxes in the picture below.
Connecting Twilio to your application:
Connecting Twilio to your Bluemix application is simple, this allows the Twilio credentials to call the API to be linked into your application. Doing this makes it secure and you dont have to hard wire the credentials in the code. If you need to update your credentials, update Twilio in Bluemix.
You will need to include Twilio in the requirements file of your Bluemix application. This ensures that the required version of Twilio API’s are installed for you to use with your program.
In the requiremnts.txt file just add this line,
To connect Twilio, first click on the Twilio icon in Bluemix. It should look something like this.
The next tep is to add the program (app) and the space you wish to use. Finally, from Twilio, you need to enter your Account SID as well as the AuthToken. These are needed to complete the billing as well as the authority to use the Twilio service and phone number attached.
A short and simple bit of Python Code to call Twilio and send an
The following small peice of code is all you need to call Twilio in Python. This code has the credentials hard wired.
from twilio.rest import TwilioRestClient
account = “——————————————“
token = “——————————-“
client = TwilioRestClient(account, token)
message = client.sms.messages.create(to=”+your desitination number goes here”,
body=”Hello From Python example code”)
If you are running this code on your server and not in Bluemix to test it, dont forget you first need to install TwilioPython module. To install, in your command line enter;
SUDO pip install twilio
Accessing the Twilio Credentials from Bluemix
As previously mentioned, your Twilio Credentials will be stored in Bluemix and below is some small Python Code that you can include in your Python Program to extract it. Credentials are stored as a JSON file, under the key ‘user-provided’. The first  array entry is the Twilioinfo.
The Twilioinfo is also a JSON construct. It has a key called “credentials” and 2 values, authtoken and acountSID. The following code is all you will need to access your Twilio credentials in Python.
if ‘VCAP_SERVICES’ in os.environ:
twilioinfo = json.loads(os.environ[‘VCAP_SERVICES’])[‘user-provided’]
twiliocred = twilioinfo[“credentials”]
twilioauthtoken = twiliocred[‘authToken’]
twilioaccountsid = twiliocred[‘accountSID’]
To run your small program – you will need some code that looks something like this.
tophonenumber = “+9876554321” #<- change this
client = TwilioRestClient(twilioaccountsid, twilioauthtoken)
message = client.sms.messages.create(to=tophonenumber,
from_=”+101010101010101010101″, #<- change this
On completion of this, you would have connected Twilio – Python – Bluemix and been able to start incorporating Mobile phone services into your IoT or other applications.