Platform
Docs
Solutions
ContactLog In

4 Different Ways To Send Text Messages (SMS) with Python

Get started sending notifications with Courier in minutes, not days!

We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.

Sign up

With the advancement of technology and the internet, sending a message to another person has become easier. However, SMS marketing still has the highest reach and a response rate compared to the average email response rate. Therefore, SMS is a powerful tool for customer engagement that allows businesses to send timely, concise, and personalized messages.

This post discusses three different options to send text messages (SMS) with Python. I'll share each method's pros and cons, so you can select the best method that fits your needs.

1. Using Twilio API

Twilio APIs provide seamless communication with the users via voice, SMS, video, or chat. Many companies use it to send SMS notifications such as password resets or alerts.

Key Partners of Twilio SMS API

  • Uber
  • Airbnb
  • eBay
  • Marks & Spenser
  • Coca Cola

Advantages of Using Twilio SMS API

  1. Support of various programming languages.
  2. Only get charged for what you use.
  3. Easy-to-understand documentation is available.
  4. Wide community support.

Disadvantages of Using Twilio SMS API

  1. It contains a developer-first platform and may not be easily navigable for non-developers.
  2. Comparatively expensive.
  3. Not very mobile-friendly.

Tutorial: How to Send SMS Using Twilio SMS API

To use Twilio in your project, you'll need a Twilio Account. If you don't have one already, you can sign up for your free trial account here.

To send SMS, you'll also require an SMS-enabled Twilio phone number which you can search and buy from the Twilio console - Buy a Number page.

Install dependencies

You can install the Twilio library from the console if you don't already have the Python helper library installed, using pip:

1 pip install twilio

Initialize the dependencies

Create a file named send-sms.py and add the code below. You can find the Account SID and Auth Token in your Twilio console and set the environment variables.

1 2 3 4 import os account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN']

Send an SMS

1 2 3 4 5 6 7 8 9 10 11 from twilio.rest import Client client = Client(account_sid, auth_token) message = client.messages \ .create( body='Hello there from Twilio SMS API', from_ = FROM_NUMBER, to = TO_NUMBER ) print(message.sid)

Here, you can replace the FROM_NUMBER from the number you purchased earlier and the TO_NUMBER from the message recipient's number.

Apart from Python, Twilio provides SDKs on C#, Node.js, PHP, Java, Go, and Ruby.

2. Using Vonage API

Vonage provides a simple yet powerful way to deliver voice and SMS messages. They offer a set of APIs for sending SMS notifications, sending SMS messages in bulk, and many more. It is perfect for sales personnel, general brand owners, or comms agents who need to distribute information across multiple channels with their users.

Key partners of Vonage SMS API

  • Dominos
  • Glassdoor
  • Allstate
  • DHL
  • Zipcar

Advantages of Using Vonage SMS API

  1. Comparatively easy to set up.
  2. Both web and mobile-friendly.
  3. Verify API adds a layer of security.

Disadvantages of Using Vonage SMS API

  1. Quite expensive.
  2. Canceling the service is difficult and takes a substantial amount of time.
  3. Poor customer support.

Tutorial: How to Send SMS Using Vonage SMS API

To complete this tutorial, you'll need a Vonage Account. If you don't have one already, you can create a new account and start developing with free credit.

Install dependencies

To install the Vonage Python SDK using pip:

1 pip install vonage

Or to upgrade the installed Vonage Python SDK using pip:

1 pip install vonage --upgrade

Instead, you can clone the repository via the command line:

1 git clone git@github.com:Vonage/vonage-python-sdk.git

Initialize the dependencies

Now, create a file named send-sms.py and include the following code. You can find the API key and API Secret from the Vonage API dashboard.

1 2 3 4 5 6 7 8 import os VONAGE_API_KEY = os.environ['VONAGE_API_KEY'] VONAGE_API_SECRET = os.environ['VONAGE_API_SECRET'] #Create a client instance and then pass the client to the Sms instance client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET) sms = vonage.Sms(client)

Instead of creating a client instance, you can directly pass the key and secret to the constructor.

Send an SMS

Sending an SMS using the Vonage API is very easy, and you can use the following code to do that.

1 2 3 4 5 6 7 8 9 10 11 12 13 response = sms.send_message( { "from": VONAGE_BRAND_NAME, "to": TO_NUMBER, "text": "Hello there from Vonage SMS API", } ) if response["messages"][0]["status"] == "0": print("Message Details: ", response) print("Message sent successfully.") else: print(f"Message failed with error: {response['messages'][0]['error-text']}")

Apart from Python, Vonage SDKs are available in C#, Node.js, PHP, Ruby, Java, and .NET too.

3. Using Plivo API

Plivo is a cloud-based API platform that enables real-time communications to applications. It provides SDKs and RESTful API to integrate SMS, voice, video, push notifications, and many other forms of communication into the mobile, web, and IoT applications.

Key Partners of Vonage SMS API

  • IBM
  • Zomato
  • Accenture
  • WiX.com

Advantages of Using Vonage SMS API

  1. Easy to set up.
  2. User-friendly UI/UX.
  3. Seamless customization.

Disadvantages of Using Vonage SMS API

  1. Documentation is a bit challenging to understand.
  2. Lack of error messages.

Tutorial: How to Send SMS Using a Plivo SMS API

You need a Plivo account and an SMS-enabled phone number to get started.

Install dependencies

You can install the Plivo SDK using pip:

1 pip install plivo

Initialize the dependencies

Create a file named send-sms.py and add the following code to it. The Auth ID and Auth Token can be found in the Account section at the top of the Plivo console.

1 2 3 4 import os auth_id = os.environ['PLIVO_ACCOUNT_ID'] auth_token = os.environ['PLIVO_AUTH_TOKEN']

Send an SMS

1 2 3 4 5 6 7 8 import plivo client = plivo.RestClient('<auth_id>','<auth_token>') message_created = client.messages.create( src = FROM_NUMBER, dst = TO_NUMBER, text='Hello there from Plivo SMS API!' )

Note that if you're using a Plivo trial account, you can only send messages using the numbers that have been verified with Plivo.

Plivo SDKs are available in .NET, Go, Java, Python, PHP, Node.js, and Ruby.

4. Using Multi-Channel Notification API

Apart from the methods mentioned above, you can use a multi-channel notification service like Courier to send SMS, which helps you reach your customers on more than one channel using a uniform API.

With the modern application requirements, using a multi-channel notification service like Courier can streamline your notification flow by creating all the relevant channels with them a single API.

Advantages of Using a Multichannel Notification Service

  1. You can notify users across different notification channels using a single API.
  2. You won't have to manage a large codebase since all channels use the same API.
  3. You can add more notification channels without modifying your code.
  4. As a result, your notification service can be managed by any non-technical user.

Disadvantages of Using a Multichannel Notification Service

  1. Consuming many notification channels can be costly.
  2. The maximum number of notifications you can receive per month limits you.

Tutorial: How to Send SMS Using a Multi-Channel Notification API

For this demonstration, I will be using Courier since it supports some well-known SMS providers like Twillio, Plivo, MessageBird, etc.

Creating a Courier account

You can easily create a free Courier account using the following link - https://app.courier.com/signup.

Creating a New Channel

After logging in to the account, you need to create a new channel. For that, select the Channels tab from the Courier dashboard. You will see a list of SMS providers there, and you can select one from the list. For this example, I have selected MessageBird.

MessageBird Send SMS

Then, you will be asked to enter an Originating Number and an Access Key. After that, click the Install Provider button.

Creating a Notification

After creating the channel, select the Designer tab from the Courier dashboard and click the Create Notification button. There, select SMS as the channel and MessageBird as the provider.

Send SMS Notification

Then, click the Publish Changes button and move to the Preview tab. There, click the Create Test Event button to create a new event.

Send TXT Message Node.js

Install Courier SDK

After creating the event, you can find the auto-generated code to configure Courier with your project in the Send tab.

Then, you need to install Courier SDK using the pip install trycourier command and copy the auto-generated code to your project.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 from trycourier import Courier client = Courier(auth_token="YOUR_AUTH_TOKEN_HERE") resp = client.send_message( message={ "to": { "phone_number": "5558675309", }, "template": "NEDRQK62TWM70MHT84S6FKSXM4VT", "data": { "test": "test", }, } ) print(resp['requestId'])

Note: You can customize the above code based on your requirements. Please refer to Courier documentation for more details.

Conclusion

In this article, I've discussed three different service providers that you can use to send SMS with Python. With basic knowledge of Python, you should now be able to choose the most suitable provider and integrate it into your application to send SMS.

Get started sending notifications with Courier in minutes, not days!

We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.

Sign up

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.