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

Send Messages with Messaging Services


Using a Messaging Service improves your customers' SMS or WhatsApp messaging experience with routing intelligence and content features that you can control from the Twilio Console(link takes you to an external page). With Messaging Service features, you can localize outgoing phone numbers, distribute bulk text messaging across multiple senders, lock one number to a customer, and much more.

This guide covers what a Messaging Service can do for your business and shows you how to set up a Service to send messages in your application.

To run any of the code samples found here, you can run the code samples in your local development environment, use the Twilio CLI, or use curl commands in your terminal.

For instructions on setting up your local environment select your programming language of choice below:

  • Ruby
  • Python
  • PHP
  • Node.js
  • Java
  • C#
  • Go

Let's get started!


Why use a Messaging Service?

why-use-a-messaging-service page anchor

Sending messages at a high volume and/or at a global scale quickly balloons in complexity. That's why Twilio Programmable Messaging encourages the use of Messaging Services to manage your senders, maintain compliance with local carrier regulations, and create a smooth and consistent messaging experience for your end users.

Some Messaging Service features include:

  • Sticky Sender : Use the same From phone number to message a given customer for a consistent experience
  • Smart Encoding : Save space (and money!) by automatically converting hard-to-catch Unicode characters to UCS-2 compliant characters
  • MMS Converter : Automatically convert links to media files for areas in which MMS is not supported
  • Advanced Opt-Out : Manage your opt-out, opt-in, and help keywords and messages
  • Sender ID pre-registration alert : Get notifications when you are sending messages in countries that require pre-registration of your Alphanumeric Sender ID
  • ...and more!

For more information, check out the Messaging Service Overview page.


Create and Configure a Messaging Service

create-and-configure-a-messaging-service page anchor

You can create a Messaging Service through the Twilio Console(link takes you to an external page) or using the REST API with the following code:

Create a Messaging Service

create-a-messaging-service page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.messaging.v1.services
_10
.create({friendlyName: 'My First Messaging Service'})
_10
.then(service => console.log(service.sid));

Output

_33
{
_33
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_33
"sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_33
"date_created": "2015-07-30T20:12:31Z",
_33
"date_updated": "2015-07-30T20:12:33Z",
_33
"friendly_name": "My First Messaging Service",
_33
"inbound_request_url": "https://www.example.com/",
_33
"inbound_method": "POST",
_33
"fallback_url": "https://www.example.com",
_33
"fallback_method": "GET",
_33
"status_callback": "https://www.example.com",
_33
"sticky_sender": true,
_33
"smart_encoding": false,
_33
"mms_converter": true,
_33
"fallback_to_long_code": true,
_33
"scan_message_content": "inherit",
_33
"area_code_geomatch": true,
_33
"validity_period": 600,
_33
"synchronous_validation": true,
_33
"usecase": "marketing",
_33
"us_app_to_person_registered": false,
_33
"use_inbound_webhook_on_number": true,
_33
"links": {
_33
"phone_numbers": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/PhoneNumbers",
_33
"short_codes": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ShortCodes",
_33
"alpha_senders": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/AlphaSenders",
_33
"messages": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages",
_33
"us_app_to_person": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Compliance/Usa2p",
_33
"us_app_to_person_usecases": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Compliance/Usa2p/Usecases",
_33
"channel_senders": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ChannelSenders"
_33
},
_33
"url": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_33
}

Take note of the Messaging Service SID (a string starting with MGXX) that the code prints to your console: you need this SID in future steps in this guide.


Purchase an SMS capable phone number

purchase-an-sms-capable-phone-number page anchor

Sending SMS messages requires an SMS capable phone number. You can search for and purchase available phone numbers(link takes you to an external page) in the Console. When you search, make sure that the number you choose is SMS capable. Check the appropriate box in the search UI to filter available numbers to those that are SMS capable.

Search for SMS Capable Number.

When viewing the search results, you can see the which numbers are SMS capable.

Buy SMS Capable Number.

With your shiny new Twilio phone number, you can start sending messages to mobile devices.

(warning)

Warning

Effective July 5, 2023, sending messages to the United States while in trial will require a Toll-Free number.
If you would prefer to use a US 10DLC number to message recipients in the United States you must upgrade your account and complete A2P 10DLC registration. More information about A2P 10DLC registration is available here(link takes you to an external page).
A2P 10DLC registration is not required if you are messaging recipients outside of the United States while in trial.

Add a number to the Messaging Service's sender pool

add-a-number-to-the-messaging-services-sender-pool page anchor

Now, associate your Twilio number with the Messaging Service that you created.

You can do this in the Twilio Console in the Senders section under your Messaging Service. Click the Add Senders button, select the Sender Type, and assign the senders to your Messaging Service.

You can also use the Messaging Services REST API to add the Phone Number you purchased to your sender pool. To do this, you will need the Phone Number's unique SID, which starts with PNXXX. You can find this in the Phone Numbers Section of the Twilio Console(link takes you to an external page).

find_phone_number_sid.

Use the Phone Number's SID and your Messaging Service's SID to attach your Twilio number to the Messaging Service that you created:

Add a phone number to the Sender Pool

add-a-phone-number-to-the-sender-pool page anchor

Add your new phone number to the Sender Pool for your Messaging Service

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_13
// Download the helper library from https://www.twilio.com/docs/node/install
_13
// Find your Account SID and Auth Token at twilio.com/console
_13
// and set the environment variables. See http://twil.io/secure
_13
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_13
const authToken = process.env.TWILIO_AUTH_TOKEN;
_13
const client = require('twilio')(accountSid, authToken);
_13
_13
client.messaging.v1.services('MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13
.phoneNumbers
_13
.create({
_13
phoneNumberSid: 'PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
_13
})
_13
.then(phone_number => console.log(phone_number.sid));

Output

_11
{
_11
"sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_11
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_11
"service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_11
"date_created": "2015-07-30T20:12:31Z",
_11
"date_updated": "2015-07-30T20:12:33Z",
_11
"phone_number": "+987654321",
_11
"country_code": "US",
_11
"capabilities": [],
_11
"url": "https://messaging.twilio.com/v1/Services/MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/PhoneNumbers/PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_11
}

(warning)

Warning

You must add at least one phone number or channel address to the Sender Pool for your Messaging Service before it can send messages.


Send a message with a Messaging Service

send-a-message-with-a-messaging-service page anchor

Sending a message with a Messaging Service is a lot like sending a message from a Twilio number, with one key difference. Instead of specifying a From telephone number in your API request, you specify a Messaging Service SID, its unique identifier.

In this example, we'll use the Messaging Service SID (it starts with MGXX) of the Messaging Service that we just created.

Send a Message with a Messaging Service

send-a-message-with-a-messaging-service-1 page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = require('twilio')(accountSid, authToken);
_14
_14
client.messages
_14
.create({
_14
body: 'Hello from your Messaging Service!',
_14
messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
_14
to: '+15553332222'
_14
})
_14
.then(message => console.log(message.sid));

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "Hello from your Messaging Service!",
_24
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "+14155552345",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"messaging_service_sid": "MG9752274e9e519418a7406176694466fa",
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "+15553332222",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

Run the code sample, replacing the Messaging Service SID with the SID you created earlier, and the To number with your own mobile phone number. In a few seconds, you should receive an SMS message!


Sending an SMS or WhatsApp message using a Twilio Messaging Service sets you up for efficient scaling. As your messaging needs grow, your Messaging Service will automatically handle the multiple senders in your pool as well as maintain consistent interactions with your end users around the world.

Ready to build more? Check out the following resources:


Rate this page: