Send SMS Text Notifications Via Twilio API On the Cheap
Sending SMS text notifications is a great way to stay engaged with your customers. There are many services out there that allow you to integrate your app with SMS functionality, but I have become quite smitten with Twilio. The main reason I prefer them is because of their trust status and deliverability of SMS messages.
The process of getting set up is quite straight-forward. You'll need to create an account, purchase a phone number ($1/mo for local and $2/mo for toll-free), and create a messaging service. You'll need to verify your business before you can send SMS messages, but it's easy and instant in most cases.
There are two levels of trust. The starter use case is free to register and allows up to 3000 message segments per day and up to 1 message segment per second. For a $50 fee you can upgrade to much higher quotas, but further verification is required.
The messaging rates as of the writing of this article are $0.0075/message segment, which is not bad--just 3/4 of a penny. Click Here for complete pricing info.
Step 1: Register Your Twilio Service
Ok, let's get started! Assuming you have registered an account and have selected a phone number, let's create a messaging service.
In the search bar on the top right of the console, type messaging and select Messaging Services from the search results. Then click Create Messaging Service.
Name your messaging service (I chose Notifications) and then make sure Notify my users is selected under Select what you want to use Messaging for.
Next, click Add Senders to the sender pool, select Phone Number as Sender Type and click Continue.
Now select your phone number and click Add Phone Numbers. Then click Step 3: Setup Integration.
On the next screen, you can set up webhooks and callback URLs for inbound messages, but we're not going to do that now (tutorial forthcoming). Go ahead and leave these settings as-is and click Step 4: Add compliance info. Then click Register Business Profile.
Under What type of profile do you need? select Starter customer profile and click Continue.
Fill out your company information (be sure to use your company's legal business name) and then click Save and continue.
When your profile is complete, click Submit for Review. You should now see a screen confirming your messaging quota limits as shown. Click Continue twice to proceed.
Now select your service Notifications under Select a Messaging Service to link to your campaign use case and click Continue.
If everything looks good, click Register campaign use case. If all goes well, you should see the confetti screen. If so, you're good to go!
Now before we can get integrated, you'll need three things: your Account SID, Auth Token and Messaging Service SID. Navigate back to the main screen of your messaging service to get the SID:
To get your Account SID and Auth Token, click your company name in the top left corner to go back to your main dashboard screen and copy them from there:
Step 2: Setup Your SDK Environment
Ok, now let's get integrated! I'll cover two development environments here, Node.JS and PHP.
First install the Twilio SDK.
# Node.JS
npm install twilio
# PHP
composer require twilio/sdk
Now, let's send a test SMS to ourself:
/* Node.JS */
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Your Account SID from www.twilio.com/console
const authToken = 'your_auth_token'; // Your Auth Token from www.twilio.com/console
const twilio = require('twilio');
const client = new twilio(accountSid, authToken);
client.messages
.create({
messagingServiceSid: "YOUR_MESSAGING_SERVICE_ID",
body: 'Hello from Node',
to: '+12345678901', // Text this number
from: '+12345678901', // From a valid Twilio number
})
.then((message) => console.log(message.sid));
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+15558675309',
[
'messagingServiceSid' => 'YOUR_MESSAGING_SERVICE_ID',
// A Twilio phone number you purchased at twilio.com/console
'from' => '+15017250604',
// the body of the text message you'd like to send
'body' => 'Hey Jenny! Good luck on the bar exam!'
]
);
Hopefully your phone dinged! If so, you're good to go. There are infinite possibilities for what you can do with Twilio's amazing programmable messaging and voice services. Be sure to check Our Blog. for more awesome tutorials!
Happy coding!