This tutorial walks through sending your first SMS with the Twilio Python SDK, from installation and credential setup through dispatching a real message to a phone number. It is written for Python developers who have created a Twilio account and want to add programmable SMS to a script or application. By the end, your terminal will print a message SID confirming the message was queued for delivery.
Prerequisites and Setup
You need Python 3.7 or higher, a Twilio account with a purchased or verified phone number, and your Account SID and Auth Token from the Twilio Console. Install the SDK and a dotenv helper with pip install twilio python-dotenv so credentials stay out of source control. Create a .env file at your project root with three variables: your SID, token, and sending number. Add .env to .gitignore before committing any code.
pip install twilio python-dotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
The Complete Code
The SDK's Client class wraps every Twilio REST resource, and client.messages.create() is the method you call to send an SMS. Pass body for message text, from_ for your Twilio number, and to for the recipient in E.164 format. The method returns a MessageInstance with a sid string that uniquely identifies the message. Printing both sid and status immediately confirms the message was accepted and queued.
import os
from twilio.rest import Client
from dotenv import load_dotenv
load_dotenv()
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)
message = client.messages.create(
body="Hello from Twilio and Python!",
from_=os.environ["TWILIO_PHONE_NUMBER"],
to="+15551234567"
)
print(f"Message SID: {message.sid}")
print(f"Status: {message.status}")
Testing and Debugging
Run python send_sms.py and verify that a SID starting with SM prints to the terminal alongside a status of queued. If you see TwilioRestException with error code 21608, the destination number has not been verified under Verified Caller IDs in the Twilio Console, which trial accounts require. Error code 21211 means the to number is not in valid E.164 format, so confirm it includes a leading plus sign and country code. Navigate to Monitor in the Twilio Console to see the full delivery chain if the script completes but the message never arrives on the handset.
Taking It Further
Wrap client.messages.create() in a try/except block catching twilio.base.exceptions.TwilioRestException to log the error code and message in production. Add a status_callback URL so Twilio POSTs delivery receipts to your server without you needing to poll. Swap the from_ number for a messaging_service_sid to route messages through a Messaging Service pool that handles number selection and carrier registration automatically. You can also pass validity_period as an integer number of seconds to control how long Twilio retries an undelivered message before marking it failed.
Reach out to Telphi Consulting to discuss building production-grade SMS workflows tailored to your application's requirements.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.