How to send sms with Twilio API and Python

Author:

Lucas Estefano - Full-Stack developer

Let’s connect:  Linkedin

How to send sms with Twilio API and Python.

Recently, I embarked on a project for a new client in Australia that requires sending SMS to old leads. In the past, while working in Brazil, I had a partnership with Twilio, a renowned company in this field. I decided to check if Twilio operated in Australia and the United States, countries to which I will need to send the SMS. Fortunately, I found out that they do, with a very affordable cost, approximately $0.0079 per SMS for Short codes.

1- Creating your TWILIO account.

 Firstly, you should access the Twilio website and create your account in order to use Twilio’s features.

1.1 Twilio web site:https://www.twilio.com/en-us

1.2- Click on “Start for free” and create your account.

1.3 – During the account creation process, Twilio will ask you about your goal with this account. Since it’s just a test, you can respond as follows:

1.4 – Account successfully created, you will be directed to the Twilio Dashboard.

2– Creating your phone number and obtaining the token to use in the API.

After creating your account, you need a phone number for sending SMS messages. Twilio provides all of this for you, and all this data is available in the Dashboard below:

 

Notice in the top left corner that a credit of $15.50 has been provided, which is the Trial credit for sending SMS messages.

 

2.1 – Click on “Get your phone number” and your number will be created.

 

2.1 – To retrieve the Authentication and Account SID data, simply scroll down the homepage as shown in the following image:

 

With this data in hand, let’s move on to the code!

 

3- Oops, before anything else, if you don’t have Python installed on your computer, go to the official Python website and install it.

3.1 Python web site: https://www.python.org/download

 

Basically, you’ll download Python and click “next, next, and next” to install it. Pay attention during the installation as you need to configure the Python environment variables, which might require a bit of work.

 

Now, it’s code time!!!

 

4- Open your favorite code editor, I personally like VS Code.

4.1You can download VS Code here: https://code.visualstudio.com/

4.2 – Open your VS Code and create a new file, I named it SmsTwilio.py, remember to add .py at the end.

 

 

 

 

 

4.4 – Open your terminal in the same folder as the .py file and execute the command below to install the Twilio library in your project. If you encounter issues with command 1, use command 2:

				
					pip install twilio
python -m pip install twilio
				
			

 

4.5 – Python code on your Vs code file: 

				
					from twilio.rest import Client

account_sid = 'code from Twillio'
auth_token = 'code from Twillio'
client = Client(account_sid, auth_token)
to_number = 'Destination number'

message = client.messages.create(
  from_='+1334xxxxxxxx',
  body="This is my first SMS with Twillio API.",
  to='+6143xxxxx'
)

print(message.sid)

				
			

 

Remember to replace some data on the lines:

Line  3:  Get your account_sid on Twilio Platform.

Line  4:  Get your auth_token on Twilio Platform.

Line  6: Put the destination number.

Line  9: Put your Twilio number from Twilio page.

Line 10: Your email marketing, you can use HTML 5/ CSS3 code here.

Line 11: Destination number

After running your code, you should receive something like this in your terminal:

 

SMS sent successfully!

 

SMS on your phone

That’s it! Following all these steps, you should have successfully sent the email using Twilio’s test API. It’s important to remember that these are the steps for your testing environment. If you want to deploy it to a production environment, you should upgrade your Twilio plan. Thanks, guys! I’ll see you in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *