SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Simple Example for Sending Programmable SMS Text or Picture Messages

Question

Is there an example for sending programmable SMS text or picture messages?

Product

Programmable Messaging 

Answer

To send a message, your application needs to make an HTTP POST request to Twilio’s Messages API resource with three required pieces of information; a recipient, a sender, and some content.

  • Recipient: The To parameter consisting of the desired destination phone number (using E.164 formatting) for receiving this message.
  • Sender: One of the following to indicate the sending party -
  • Content: The parameter(s) that indicate the body for this message -
    • The Body parameter consisting of the message text you want to send.
    • The MediaUrl parameter consisting of the URL of the media you wish to send out with the message.
    • The ContentSid of the Content Template you wish to use

Notice: Media files must be accessible by Twilio's proxy servers. Local files on your desktop are likely unaccessible via these methods, but may be uploaded to Twilio Assets for sending. For more information on supported file types, please see Twilio Programmable SMS Supported File Types and Size Limits for MMS Media Messages.


Sample Code

Here’s an example cURL script:

curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json \
--data-urlencode "To=+13105555555" \
--data-urlencode "From=+12125551234" \
--data-urlencode "MediaUrl=https://demo.twilio.com/owl.png" \
--data-urlencode "Body=Hello from my Twilio line!" \
-u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'

This example sends an outbound message from the sender (212) 555-1234 (+12125551234) to the recipient at (310) 555-5555 (+13105555555), and includes the image owl.png, and the following message:

Hello from my Twilio line!

To make this script work for you, make the following updates, and then paste it into a terminal window:

  • Line 1 update with your Account SID
  • Line 2 update with a valid destination
  • Line 3 update with a valid sender number
  • Line 4 update with a valid image file url
  • Line 5 update with the desired message text
  • Line 6 update with your Account SID and Auth Token

Additional optional parameters can be added in your request for requesting requesting status change updates, adding a price limit, and more. For full details on each of the available options, and code samples from our Helper Libraries, please see Message Parameters (Twilio Docs).

Additional Information

Have more questions? Submit a request
Powered by Zendesk