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.- Trial accounts must first verify a phone number before you can send messages there. This limitation is removed once an account is upgraded with a balance. For full details on Trial account limitations, please see our article How Does Twilio's Free Trial Work?
-
Sender: One of the following to indicate the sending party -
- The
From
parameter consisting of a valid Twilio phone number (using E.164 formatting), short code, or Alphanumeric Sender ID (when available) for sending this message. - The
MessagingServiceSid
parameter consisting of a valid Messaging Service SID (MGxxxxx) for sending from a Messaging Service.
- The
-
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
- The
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).