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. 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. A default Messaging Service is automatically generated for your Account upon creation.

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?

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

An auto-generated default Messaging Service has been created for you, or you can create a Messaging Service through the Twilio Console or using the REST API with the following code:

Create a Messaging Service

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

_18
// Download the helper library from https://www.twilio.com/docs/node/install
_18
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_18
_18
// Find your Account SID and Auth Token at twilio.com/console
_18
// and set the environment 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 = twilio(accountSid, authToken);
_18
_18
async function createService() {
_18
const service = await client.messaging.v1.services.create({
_18
friendlyName: "My First Messaging Service",
_18
});
_18
_18
console.log(service.sid);
_18
}
_18
_18
createService();

Output

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

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

Sending SMS messages requires an SMS capable phone number. You can search for and purchase available phone numbers 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.
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

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.

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 your new phone number to the Sender Pool for your Messaging Service

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

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment 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 = twilio(accountSid, authToken);
_20
_20
async function createPhoneNumber() {
_20
const phoneNumber = await client.messaging.v1
_20
.services("MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
_20
.phoneNumbers.create({
_20
phoneNumberSid: "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
});
_20
_20
console.log(phoneNumber.sid);
_20
}
_20
_20
createPhoneNumber();

Output

_11
{
_11
"sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_11
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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/MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/PhoneNumbers/PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_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

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

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

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment 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 = twilio(accountSid, authToken);
_20
_20
async function createMessage() {
_20
const message = await client.messages.create({
_20
body: "Hello from your Messaging Service!",
_20
messagingServiceSid: "MG9752274e9e519418a7406176694466fa",
_20
to: "+15553332222",
_20
});
_20
_20
console.log(message.body);
_20
}
_20
_20
createMessage();

Output

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

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!


What's Next?

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: