Outbound requests to the Programmable Messaging API will receive an immediate response from Twilio. However, the num_segment
property value in Twilio's response will vary based on which parameters are present in your request.
Outbound Messages using the From parameter
When you make a REST API request to send a message using the From
parameter, Twilio analyzes that message immediately, and returns a segment count in our response to your application. Here's an example:
{
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"date_created": "Thu, 05 Sep 2019 00:45:02 +0000",
"date_updated": "Thu, 05 Sep 2019 00:45:02 +0000",
"date_sent": null,
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"to": "+13105555555",
"from": "+12125551234",
"messaging_service_sid": null,
"body": "Hello from my Twilio line!",
"status": "queued",
"num_segments": "1",
"num_media": "0",
"direction": "outbound-api",
"api_version": "2010-04-01",
"price": null,
"price_unit": "USD",
"error_code": null,
"error_message": null,
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
"subresource_uris": {
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
}
}
Outbound Messages using Messaging Services
When you make a request to send a message using a Messaging Service, Twilio does not return an accurate segment count in the initial response to your application. Twilio will validate your request immediately, but then returns a response with num_segment: 0
. Here's an example:
{
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"date_created": "Thu, 05 Sep 2019 00:55:39 +0000",
"date_updated": "Thu, 05 Sep 2019 00:55:39 +0000",
"date_sent": null,
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"to": "+13105555555",
"from": null,
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"body": "Hello from My Messaging Service!",
"status": "accepted",
"num_segments": "0",
"num_media": "0",
"direction": "outbound-api",
"api_version": "2010-04-01",
"price": null,
"price_unit": null,
"error_code": null,
"error_message": null,
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
"subresource_uris": {
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
}
}
This occurs because Messaging Services analyze your message further after processing the API request, in order to select the best phone number or Alpha Sender ID to use for that message. A valid segment count is not available at the time of the initial response. Instead of waiting until the message has been fully processed, Twilio sends the initial API response immediately, helping you to send messages at a higher rate.
How can I get num_segments when using Messaging Services?
You can retrieve the number of segments for an SMS message after the message has been sent, by making a GET request to the Message API resource for that message.
Please note, after sending a message it takes a few seconds for Twilio to populate all of the message details to the Message resource. If you choose to implement GET requests to retrieve the number of segments for your messages, we recommend you implement a delay of 10-30 seconds between sending the message and doing the GET request.