Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Notify: SMS Notifications Quickstart


With this Quickstart, you will be able to send an SMS Notification from scratch (no previous code writing experience needed).

What we will do:

Before we get started, please, read the following note:

(warning)

Warning

Make sure you have consent from users to receive notifications.

It is best practice, and also potentially required by law in certain jurisdictions, for you to have consent from your end users before sending messages to them, and you should respect your end users' choice to not receive messages from you. It is also important to make sure your database is up to date. This is particularly important for number-based communications like SMS because over time phone numbers may be reassigned to different individuals. If your database is out of date, you could inadvertently send a message to someone who did not consent but was reassigned a phone number that was previously subscribed to your service by another person. Check out the Twilio Marketplace for Add-ons from our partners that can help you keep your database up to date.

Twilio recommends that you consult with your legal counsel to make sure that you are complying with all applicable laws in connection with communications you transmit using Twilio.


Purchase a Twilio Phone Number

purchase-a-twilio-phone-number page anchor

The first thing you need in order to send an SMS notification is a Twilio-powered phone number. You can grab one to use in the console here by going to the Numbers tab to add a phone number. Make sure that it has SMS enabled!

Notify SMS Quickstart - buy a Twilio Phone Number.

Now for Notify to use this Phone number, we need to add it to Messaging Service and associate it with Notify Service Instance.


Create a Messaging Service

create-a-messaging-service page anchor

Go to console Programmable SMS -> Messaging Service

Create a new Messaging Service.

Notify SMS Quickstart - create Messaging Service.

Messaging Services allow you to organize your messages and enable specific features for groups of messages. It has a lot of cool features and you can read about them here, but now we just need it to send SMS from your new Twilio Phone Number.

To do that, add your new Twilio Phone Number to the Messaging Service

Notify SMS Quickstart - add Twilio Phone Number to Messaging Service step 2.

Now that we have a Phone Number and a Messaging Service, we must create a Notify Service Instance that we'll use the new Twilio Phone Number to send SMS Notifications.


Set up Notify Service Instance

set-up-notify-service-instance page anchor

In the console, create a Notify Service. Make note of the SID! You will use this later when you start writing code further down.

Notify SMS Quickstart - set up Notify Service Instance.

Now choose your new Messaging Service for this Notify Service Instance

Notify SMS Quickstart - configure Messaging Service in Notify Service Instance.

Next, we will gather account information and start writing a bit of code.


Gather account information

gather-account-information page anchor

Config ValueDescription
Account SIDUsed to authenticate REST API requests - find it in the console here.
Auth TokenUsed to authenticate REST API requests - like the Account SID, find it in the console here.
Service Instance SIDA Notify Service instance where all the data for our application is stored and scoped. We created it in Twilio console in the previous paragraph.

Now we're ready to write some code!


(Optional) Create a binding for SMS

optional-create-a-binding-for-sms page anchor

This step is optional. If you do not want to store the phone numbers of your users in Notify to manage all contact information in one place, you can just skip ahead to Send an SMS Notification.

A Binding is a combination of a user and a device that can receive a notification. You can find out more about bindings and how to create, list, retrieve or delete them, under REST API - > Bindings menu.

In this example, we will create a binding with type 'sms'.

You will need:

  • Your account information (gathered above )
  • User Identity - this is a unique identifier of the user. In this example it's 00000001 , but it's up to you, how you identify your users. The only requirement - it has to be unique and not Personally Identifiable Information.
  • User's phone number (parameter 'Address') - for testing purposes, let's create a binding with your mobile phone number so you can actually receive it. In the example, it's +1651000000000`

Here, we use the REST API to create a Binding

Create a Binding for SMS

create-a-binding-for-sms page anchor
Node.js
Python
C#
Java
PHP
Ruby
curl

_20
// Download the Node helper library from www.twilio.com/docs/libraries/node#installation
_20
// These identifiers are your accountSid and authToken from
_20
// https://www.twilio.com/console
_20
// To set up environmental variables, see http://twil.io/secure
_20
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_20
const authToken = process.env.TWILIO_AUTH_TOKEN;
_20
const client = require('twilio')(accountSid, authToken);
_20
_20
const bindingOpts = {
_20
identity: '00000001', // We recommend using a GUID or other anonymized identifier for Identity.
_20
bindingType: 'sms',
_20
address: '+1651000000000',
_20
};
_20
_20
client.notify
_20
.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_20
.bindings.create(bindingOpts)
_20
.then(binding => console.log(binding.sid))
_20
.catch(error => console.log(error))
_20
.done();

(error)

Danger

Do not use Personally Identifiable Information for Identity

Notify uses Identity as a unique identifier of a user. You should not use directly identifying information (aka personally identifiable information or PII) like a person's name, home address, email or phone number, as Identity because the systems that will process this attribute assume it is not directly identifying information.

Now that we have a binding, all we need to do is send an SMS notification!


Send an SMS Notification

send-an-sms-notification page anchor

In this step, we will send an SMS. If you created a Binding in the previous step we are going to use that if not you can just provide the phone number(s) you want to message directly in the request.

You will need:

  • Your account information (gathered above )
  • User's Identity or phone number, to whom you want to send the SMS (Note, if you are using Identity then a binding between the Identity and the phone number has to be created first. See an example at Create a Binding for SMS .)
  • Body - text of the message to be sent.

Here, we use the REST API to send an SMS Notification using Identity

Send SMS Notification Using Identity

send-sms-notification-using-identity page anchor
Node.js
Python
C#
Java
PHP
Ruby
curl

_18
// Download the Node helper library from www.twilio.com/docs/libraries/node#installation
_18
// These identifiers are your accountSid and authToken from
_18
// https://www.twilio.com/console
_18
// To set up environmental variables, see http://twil.io/secure
_18
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_18
const authToken = process.env.TWILIO_AUTH_TOKEN;
_18
const client = require('twilio')(accountSid, authToken);
_18
_18
const notificationOpts = {
_18
identity: '00000001', // We recommend using a GUID or other anonymized identifier for Identity.
_18
body: 'Knok-Knok! This is your first Notify SMS',
_18
};
_18
_18
client.notify
_18
.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_18
.notifications.create(notificationOpts)
_18
.then(notification => console.log(notification.sid))
_18
.catch(error => console.log(error));

Alternatively, you can just provide the phone number directly. If you want to send an SMS to multiple phone numbers, just add more ToBinding parameters.

Send SMS Notification To a Number

send-sms-notification-to-a-number page anchor
Node.js
Python
C#
Java
PHP
Ruby
curl

_21
// Download the Node helper library from www.twilio.com/docs/libraries/node#installation
_21
// These identifiers are your accountSid and authToken from
_21
// https://www.twilio.com/console
_21
// To set up environmental variables, see http://twil.io/secure
_21
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_21
const authToken = process.env.TWILIO_AUTH_TOKEN;
_21
const client = require('twilio')(accountSid, authToken);
_21
_21
const notificationOpts = {
_21
toBinding: JSON.stringify({
_21
binding_type: 'sms',
_21
address: '+1651000000000',
_21
}),
_21
body: 'Knock-Knock! This is your first Notify SMS',
_21
};
_21
_21
client.notify
_21
.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_21
.notifications.create(notificationOpts)
_21
.then(notification => console.log(notification.sid))
_21
.catch(error => console.log(error));



There's much more you can do with Notify. Try our other Quickstarts to send:

Or learn how to send Notifications to a group of users.


Rate this page: