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.
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:
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 lMessaging Service Overview page.
Create and Configure a Messaging Service
You can create a Messaging Service through the Twilio Console or using the REST API with the following code:
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.
When viewing the search results, you can see the which numbers are SMS capable.
With your shiny new Twilio phone number, you can start sending messages to mobile devices.
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.
Use the Phone Number's SID and your Messaging Service's SID to attach your Twilio number to the Messaging Service that you created:
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.
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:
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.