SMS Text Messaging Using Twilio-Python

Twilio‘s web service APIs enable us to programatically make phone calls, send and receive text messages over the Internet. Specifically, the Twilio Messaging API is for sending and receiving SMS, MMS messages along with querying the meta-data about the messages such as delivery status, associated media and the like. While we can directly interact with the REST API to initiate connection to the Twilio-subscribed SMSC (Short Message Service Center) to send and receive text messages, Twilio provides a number of helper libraries for many of the popular programming languages which helps to easily integrate the messaging functionality into our applications. In this short how-to (quick quack), we’ll use the Twilio Python helper library to send a simple SMS text message.

Step #1: Create an user/project account in Twilio. Twilio expects your password to be 14-character long with no special character requirement. Use a password manager such as LastPass to generate the password.


Step #2: Install the twilio python package by running,

pip install twilio


easy_install twilio

In case you haven’t installed Python itself, follow this guide to do so. While Twilio supports Python 2.7 and above, it’s recommended to install Python 3 unless your application has strong (fevicol) dependencies with Python 2.7.

Step #3: Write a test script for sending an SMS message.

Before sending a text message we need to get a virtual phone number from Twilio. Go to phone numbers getting started page inside your Twilio account and click on the Get your first Twilio phone number button.


Twilio gives you a new phone number  (US-origin), make a note of it as we’ll use this number as our from_ number in the script below. The to number can be our local mobile number.

Write the following script in your favorite editor and save as

from import Client

### Authenticate the client ###
# AccountSID and AuthToken from your
account_sid = "ACa666793e2793a080e7adbb78b89e6c82"
auth_token = "2397d4d8497f52baff80097c5a0eb123"
client = Client(account_sid, auth_token)

### Create and send a message ###
message = client.messages.create(
          from_ = "+18566175170",
          to = "+919480062633",
          body = "Hello Coding Bazaar!")


Note that the phone numbers should be in E.164 format:

[+] [country code] [phone number including area code]

Step #4: Run the script and verify the SMS in your smartphone’s inbox.



Refer the Twilio’s documentation to learn more about the API and use cases.

Programming Paraphernalia