Sending Text Messages (SMS) with Python

There are plenty of examples you see today of applications using text messages. Multi-factor authentication for example. We've used it recently in an application tied to a webhook. When a certain event happens, a text is sent to let a user know.  If you're reading this, It's pretty likely you already know what you're going to do with it. So let's get to work.

We're assuming you're using Python 3

First, let's install the Twilio helper library


pip install twilio

Next, let's create a file called text_example.py. And we'll enter the following:


from twilio.rest import Client

# Fill this with your Account SID you get from twilio.com/console
my_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Fill this with your auth token you get from twilio.com/console
my_token  = "my_token_here"

twilioClient = Client(my_sid, my_token)

message = twilioClient.messages.create(
    to="+15551111212", #The number you're sending to.
    from_="+12088887676", #This is the number you rent from Twilio.
    body="Hey I sent a text message")

print(message.sid)

And then give it a try.


python text_example.py

It's just that simple. And remember, in the words of Spiderman's uncle, "with great power comes great responsibility".

Pingbacks are closed.

Comments are closed.