Twilio

raw JSON →
9.10.2 verified Tue May 12 auth: yes python install: verified quickstart: stale

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.

pip install twilio
error ModuleNotFoundError: No module named 'twilio'
cause The 'twilio' library is not installed in the active Python environment where your script is being executed.
fix
Run pip install twilio in your terminal to install the library.
error AttributeError: module 'twilio.rest' has no attribute 'TwilioRestClient'
cause The `TwilioRestClient` class was removed in `twilio-python` version 6.0.0. The new client class is simply `Client`.
fix
Update your import statement to from twilio.rest import Client and instantiate it with client = Client(account_sid, auth_token).
error Error 20003: Authenticate
cause The Account SID or Auth Token provided in your client initialization are incorrect, missing, or do not match your Twilio account credentials, preventing authentication with the Twilio API.
fix
Double-check your account_sid and auth_token for correctness and ensure they are loaded securely, ideally from environment variables.
error TypeError: create() missing 1 required positional argument: 'to'
cause The `client.messages.create()` method requires the `to`, `from_` (or `messaging_service_sid`), and `body` arguments, but one or more were omitted.
fix
Provide all required arguments to the create() method. Example: client.messages.create(to='+1234567890', from_='+1123456789', body='Hello from Twilio!')
breaking TwilioRestClient removed in v6.0.0. All pre-v6 tutorials and LLM-generated code using 'from twilio.rest import TwilioRestClient' fail with ImportError.
fix from twilio.rest import Client (not TwilioRestClient). Client constructor is the same.
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.
fix Replace .ssml_emphasis() with .emphasis() on TwiML Say objects.
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.
fix Change client.chat.v2.services('IS...').channels('CH...').messages.create('body') to .messages.create(body='body')
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.
fix Update any direct module path imports to use available_phone_number_country.
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.
fix client.messages.create(to='+1...', from_='+1...', body='...')
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.
fix Auth token: Client(account_sid, auth_token). API key: Client(api_key, api_secret, account_sid).
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.60s 70.5M
3.10 slim (glibc) - - 0.43s 73M
3.11 alpine (musl) - - 0.78s 79.6M
3.11 slim (glibc) - - 0.67s 82M
3.12 alpine (musl) - - 0.69s 70.6M
3.12 slim (glibc) - - 0.69s 73M
3.13 alpine (musl) - - 0.69s 69.1M
3.13 slim (glibc) - - 0.69s 71M
3.9 alpine (musl) - - 0.59s 70.2M
3.9 slim (glibc) - - 0.50s 73M

Note from_= (with trailing underscore) — 'from' is a Python reserved word.

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))