Twilio
Official Python helper library for the Twilio REST API (SMS, voice, WhatsApp, email, video). v9.0 (2024) was a full OpenAPI-generated rewrite. v6.0 removed TwilioRestClient. All pre-v6 import patterns are broken. Actively maintained.
Warnings
- breaking TwilioRestClient removed in v6.0.0. All pre-v6 tutorials and LLM-generated code using 'from twilio.rest import TwilioRestClient' fail with ImportError.
- breaking v9.0 (2024) was a full OpenAPI rewrite. ssml_emphasis() method on TwiML Say renamed to emphasis(). Code using say.ssml_emphasis() raises AttributeError.
- breaking v9.0: Chat API message.create() and similar methods now require body as a keyword argument, not positional. Positional args in these calls raise TypeError.
- breaking twilio.rest.api.v2010.account.available_phone_number renamed to available_phone_number_country in v9.0. Code importing the old module path fails.
- gotcha The 'from' keyword is a Python reserved word. Twilio SMS create() uses from_= (with trailing underscore) as the parameter name. Passing from= causes SyntaxError.
- gotcha Client() with no arguments reads TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN from environment. If using API key auth instead, pass Client(api_key, api_secret, account_sid) — 3-argument form. Mixing up 2-arg and 3-arg forms causes authentication failures.
Install
-
pip install twilio
Imports
- Client
from twilio.rest import Client client = Client('ACXXXXXXXXX', 'auth_token') - TwilioRestException
from twilio.base.exceptions import TwilioRestException
- VoiceResponse (TwiML)
from twilio.twiml.voice_response import VoiceResponse resp = VoiceResponse() resp.say('Hello').emphasis('you')
Quickstart
import os
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
client = Client(
os.environ['TWILIO_ACCOUNT_SID'],
os.environ['TWILIO_AUTH_TOKEN']
)
# Send SMS
try:
message = client.messages.create(
to='+15558675309',
from_='+15017250604',
body='Hello from Python!'
)
print(message.sid)
except TwilioRestException as e:
print(e)
# Generate TwiML
from twilio.twiml.voice_response import VoiceResponse
resp = VoiceResponse()
resp.say('Welcome to Twilio!')
print(str(resp))