Contents

[twilio] Twilio python client code example.

0
Contents

Here’s a code example using the twilio Python package to send an SMS message using the Twilio API

from twilio.rest import Client

# Twilio account SID and auth token
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'

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

# Sending an SMS message
message = client.messages.create(
    body='Hello from Twilio!',
    from_='your_twilio_phone_number',
    to='recipient_phone_number'
)

# Print the message SID
print(f"Message sent! SID: {message.sid}")

Make sure to replace 'your_account_sid', 'your_auth_token', 'your_twilio_phone_number', and 'recipient_phone_number' with your actual Twilio account SID, auth token, Twilio phone number, and recipient’s phone number, respectively.

In this example, we import the Client class from the twilio.rest module. We then create an instance of the Client class by passing our Twilio account SID and auth token. With the Client instance, we can use the messages.create() method to send an SMS message. We specify the message content in the body parameter, set the sender’s phone number in the from_ parameter (the underscore is used because from is a reserved keyword in Python), and provide the recipient’s phone number in the to parameter.

After sending the message, we print the message.sid, which represents the unique identifier of the sent message.

Remember to install the twilio package using pip before running this code:

pip install twilio

This code demonstrates a basic usage of the twilio Python package to send an SMS message, but you can explore more advanced features and capabilities of the package by referring to the Twilio documentation and the twilio package’s documentation.

About twilio

Twilio - A Python module for communicating with the Twilio API and generating TwiML..