Twilio

9.10.2 · active · verified Sun Mar 01

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

Install

Imports

Quickstart

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

view raw JSON →