Use Python Code to send Text message/SMS by using the third party API Twilio

Kishita Agarwal
2 min readAug 21, 2023

--

Let’s break down the provided code step by step:

  1. Installing Twilio Library:
pip install twilio

This line indicates that the code requires the Twilio library to be installed. It’s installed using the pip package manager, which is a standard tool for installing Python packages.

2. Importing Twilio Modules:

import twilio from twilio.rest import Client

These lines import the necessary modules from the Twilio package to interact with the Twilio API and send SMS messages.

3. Setting Twilio Account Information:

account_sid = "enter_your_account_sid_by_twilio" 
auth_token = "enter_your_account_token_by_twilio"

These lines store your Twilio account’s SID (account identifier) and authentication token. These credentials are required to authenticate your API requests with Twilio.

4. Creating Twilio Client:

cli = Client(account_sid, auth_token)

This line creates a Twilio Client object using the provided account_sid and auth_token. This client object is used to interact with the Twilio API.

5. Sending SMS:

cli.messages.create(to="+91**********",
from_="your_twilio_number",
body="Hello!!!!")

This line uses the cli (Twilio client) object to send an SMS message. The create method is called on the messages attribute of the client object. The method requires three arguments:

  • to: The recipient's phone number (replace +91********** with the actual recipient's phone number).
  • from_: Your Twilio phone number (replace "your_twilio_number" with your Twilio phone number).
  • body: The message content, in this case, "Hello!!!!".

6. Printing Confirmation:

print("send")

This line prints “send” to the console as a confirmation that the SMS has been sent.

In summary, the code sets up the Twilio API with your account SID and authentication token, creates a Twilio client, and then sends an SMS message using the Twilio client to the specified recipient’s phone number. The recipient will see “Hello!!!!” as the content of the SMS message. Make sure to replace the placeholders with your actual Twilio account information and phone numbers.

--

--