How to send messages using Python

In this tutorial, we will learn to send message using Python. We can use this approach in building new Python applications.

Tools we will be needing :

  1. Python – Version 2 or 3.
  2. Virtual Environment  – To create isolated Python environments
  3. Twilio Trial Account.
  4. Twilio Python helper library.

Twilio API :

We will be using Twilio Python helper library to send messages over the phone. To use Twilio library we will need to create twilio account. Signup for a free account

Twilio   Try Twilio Free.png

Once you had signed up on twilio, you will receive verification code on your registered phone number, input this code on twilio verification page and your account is set to use twilio services.

In Trial Mode, Twilio can be only used to send messages to a validated phone number. You will have to upgrade your account in-order to send messages to any phone number.

Now login into twilio and grab twilio account test credentials by click over here.

#Note : Copy your TEST ACCOUNT SID and TEST AUTHTOKEN, we will be using this in python script.

Install Python Dependency

As mentioned earlier that we will be using twilio helper library for our Python application. The library can be install using pip command.

We will start the basic Python Project by first creating virtual environment. Virtual environment are used to isolate different Python environments.

virtualenv sendMsg

Now activate this environment using,

source sendMsg/bin/activate

As we are ready with our virtual environment, go ahead and install twilio library.

pip install twilio

Sending Message from Python Script

Create a new python file i.e and enter the following code in the file.

# import the Twilio client from the dependency
from import Client

# put your twilio credentials here
account_sid = "{{your_account_sid}}" # get it from
auth_token = "{{your_authentication_code}}"

client = Client(account_sid, auth_token)

media_url = ""

response = client.messages.create(
    to= "{{enter_your_twillio_account_phone_number}}", #
    from_= "+15005550006", #+15005550006 will be used for trial account.
    body= "Hello, how you doing ?",
    media_url= media_url # if you need to attach multimedia to your message, else remove this parameter.

Now run the python script using


In a few seconds you should be able to see a message appear on your phone – note that messages can sometime take a little longer because we are using test credentials and it might in queued stage. Always check API response to know exact status.

That is everything needed to send messages to phone. Awesome results using few lines of Python code. This code can be added to any Python program to add message sending capabilities.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s