Sending SMS Using Python and Twilio: A Step-by-Step Guide

mohammad ishaq
3 min readAug 9, 2023

Introduction: In today’s digitally connected world, communication is essential, and sending SMS messages programmatically has become a powerful tool for businesses and developers. Twilio, a cloud communications platform, offers a convenient way to send SMS messages using Python. In this tutorial, we’ll walk through the process of sending SMS messages using the Twilio API and Python.

Prerequisites: Before you begin, make sure you have the following:

  • A Twilio account SID and Auth Token (You can sign up for a Twilio account if you don’t have one).
  • Python installed on your machine.
  • The Twilio Python library installed (twilio package).

Step 1: Setting Up Your Twilio Account:

  1. Sign in to your Twilio account or create a new one if you don’t have an account.
  2. Once logged in, navigate to the Dashboard and locate your Account SID and Auth Token. These will be used to authenticate your API requests.

Step 2: Installing the Twilio Python Library: Open your terminal or command prompt and install the Twilio Python library using the following command:bashCopy code

pip install twilio

Step 3: Writing the Python Script: Copy and paste the provided Python code into a Python script file (e.g., send_sms.py).

Step 4: Customizing the Code:

  1. Replace 'AC4defd2204a859d272eb14cf1ca2a3182' with your Twilio Account SID.
  2. Replace 'f1877f808ee8f03fe5628bfc5470189a' with your Twilio Auth Token.
  3. Replace '+13184077785' with your Twilio phone number.
  4. Replace '+916375997845' with the destination phone number.
  5. Modify the message_body variable to contain your desired message.

Step 5: Sending the SMS: Run the Python script using the following command in your terminal:bashCopy code

python send_sms.py

Conclusion: In this tutorial, you’ve learned how to send SMS messages using Python and the Twilio API. By following these steps, you can easily integrate SMS functionality into your applications, notify users, or automate communication processes. Twilio’s user-friendly API and the power of Python make it a seamless experience to send SMS messages programmatically.

Remember that this example focuses on sending a single SMS message, but Twilio offers many more features and capabilities, such as handling incoming messages, media attachments, and more. Explore the Twilio documentation to learn about additional functionalities you can integrate into your projects. Happy coding!

coding:

from twilio.rest import Client

# Your Twilio account SID and Auth Token
account_sid = ‘AC4defd2204a859d272eb14cf1ca2a3182’
auth_token = ‘f1877f808ee8f03fe5628bfc5470189a’

# Create a Twilio client
client = Client(account_sid, auth_token)

# Your Twilio phone number and the destination phone number
twilio_phone_number = ‘+13184088055’
destination_phone_number = ‘+91876564545’ # Use the format: ‘+1234567890’

# The message you want to send
message_body = ‘hello this messege is sent using python’

try:
# Send the message
message = client.messages.create(
body=message_body,
from_=twilio_phone_number,
to=destination_phone_number
)
print(f”SMS sent successfully. Message ID: {message.sid}”)
except Exception as e:
print(f”Error sending SMS: {e}”)

output:

--

--