Python has gained a significant share of prominence in the last decade, thanks to large community support and a variety of modules/APIs available. Python is extensively applied in data science, data analysis, data engineering, machine learning and many other fields but python is also a scripting language used to write scripts for task automation. One such task is sending and receiving automated SMS from your python application. Such automated SMS sending feature is commonly used for sending OTP (one-time password) for verification and various other applications.
In this article, we will be discussing the Twilio python API and how to implement it in your python projects for sending SMS using Python. The article covers a step-by-step guide from making the Twilio python account to installing and implementing the API along with the different prices offered by Twilio.
Twilio python API
The Twilio python API offers a vast set of services including SMS, MMS, programmable voice messages, WhatsApp API integration and many more. One of the highlighted features among them all is sending and receiving SMS and MMS messages. Along with the SMS, it also offers features to send and receive the query meta-data about the text messages such as time of delivery, delivery status, and associated media (if any), and it also leverages tools like Copilot to manage your messages globally at scale.
Creating a Twilio python account
First, we need to create an account on the official website of Twilio. It is fairly simple just like any typical signup process. You need to provide your credentials and after an email verification, your account will be created.
After a successful signup process. It will present all the possible plans offered by Twilio python API that can be built with the SMS sending feature such as sending alerts and notifications, promotions etc.
Although it is a paid service, you can try out the Twilio python API with the free trial. When your account will be created, you will be credited with an initial amount of $ 15.5 to get you started. It allows you to freely test the features as per your requirements before making any purchase.
You will be provided with an account SID and auth token. It will be made available on the main console page as shown below. It is the most crucial data item as your account will be verified by them when using the API. You can copy the account SID and auth token from the console, it will be later required during the API implementation process.
Install the Twilio CLI
First, you need to install the Twilio CLI (command line interface) to set up the Twilio SMS API in python. You will be needing Scoop (a command line installer for windows) to install the Twilio CLI on Windows.
After installing scoop, use the following commands to install Twilio CLI
1. To Add the twilio-cli Bucket:
scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
2. To Install the application:
scoop install twilio
Signing in to the Twilio CLI
Run twilio login to connect Twilio CLI to your Twilio account. Now, provide the Account SID and the Auth Token we saved earlier from the Twilio Console to Twilio CLI. Set up your SMS-enabled Twilio phone number and proceed to install Python the Twilio Python Helper Library.
Installing Twilio Library using pip
Assuming that you already have python installed on your device, we will move to install the Twilio helper library on python that is required to implement Twilio python API. It is just like installing any other library using pip.
Enter the following command in the python console,
pip install twilio
Implementing Twilio python API
After installing Twilio, it is time to implement the API in your code.
The code below is the Python implementation for the twilio python API.
1. # importing twilio 2. from twilio.rest import Client 3. # Your Account Sid and Auth Token from twilio.com / console 4. account_sid = 'AC*************************************' 5. auth_token = '****************************************' 6. client = Client(account_sid, auth_token) 7. message = client.messages.create( 8. from_='+15017122661', 9. body ='body', 10. to ='+15558675310' 11. ) 12. print(message.sid) 13.
You need to replace the placeholder values for account_sid and auth_token with the values provided at the Twilio console. You have to mention Twilio which phone number to use to send the SMS. Replace the ‘from_ number’ with the Twilio phone number you would have purchased in the signup process earlier.
Note that the parameter is named `from_`, and not `from`.
Next, replace the number with to with the phone number of the recipient. Both of these parameters must use E.164 formatting which includes a ‘+’ sign followed by the country code and then the phone number, for instance, +923693921049. We also include the body parameter, which contains the content of the SMS to be sent.
If you are wondering how to broadcast a message, you could create an array of recipients and iterate through each phone number in the array.
See the code snippet below where the same message is sent to 6 different receivers,
1. numbers_to_message = ['+15558675310', '+14158141829', '+15017122661', '+15017121265', '+15036523921', '+15017224793'] 2. for number in numbers_to_message: 3. client.messages.create( 4. body='Hello friends! Greetings from my Twilio number!', 5. from_='+15017122662', 6. to=number 7. )
while using the trial account, you will notice that all the messages you send will always begin with the predefined text, “Sent from a Twilio trial account.” It will be removed, once you will upgrade your account.
And that is it! You can now successfully send SMS with the Twilio python API and the Python helper library from your application.
The Twilio python API pricing varies from country to country. We will be mainly discussing the pricing and features offered in the US. The SMS pricing is based on the destination and the type of message you want to send, as well as the carrier to which the SMS is being sent.
The current pricing (January 23) for the local numbers, toll-free numbers and sending short codes is shown below,
Messaging service features
|To Send||To Receive||To send||To receive|
|Local Numbers||$ 0.0079||$ 0.0079||$ 0.0200||$ 0.0100||INCLUDED|
|Toll-Free Numbers||$ 0.0079||$ 0.0079||$ 0.0200||$ 0.0200||INCLUDED|
|Short codes||$ 0.0079||$ 0.0079||$ 0.0200||$ 0.0100||INCLUDED|
The phone number pricings are as follows,
Number to be used
|Clean local numbers||LOCAL PREFIX|
$ 1.15/ month
One (1) SMS per second
|Toll‑free numbers||TOLL‑FREE PREFIX|
$ 2.15 / month
Three (3) SMS per second
|High-volume short code|
$ 1000/ month, billed quarterly
Plus one time setup fee **
100 SMS per second
|MANAGE YOUR OWN LEASE|
$ 500/ month, billed quarterly
Plus, one time setup fee **
|Use the Twilio SMS API with your existing phone numbers||LOCAL PREFIX|
$ 0.50 / month
One (1) SMS per second
$ 0.50/ month
Three (3) SMS per second
You can check out the in-depth prices for different US carriers and volume discounts on sending and receiving a huge number of SMS and short code on the pricing section of the Twilio website.
See Also: Guide To Integration Testing In Python
Wrapping it up
We covered the complete process of setting up the Twilio python API for sending SMS. The pricing it offers can be considered reasonable as compared to other options and the extremely simple process for implementation easily makes it one of the best options for sending SMS using python. We have just covered the SMS sending API but Twilio offers way more APIs for various functionalities.
You can explore more about Twilio and its offerings from its official website of Twilio.