Send SMS and MMS messages
This tutorial shows you how to use Twilio Programmable Messaging to programmatically send SMS and MMS messages from your web application or with cURL. The steps send POST requests to the Message resource and the Media resource in the Twilio REST API.
To send messages with the Twilio command line interface (CLI), see Get a phone number and send your first SMS.
To send messages without writing code, use Twilio Studio, our low-code application builder.
Select your programming language and complete the prerequisites:
- Install Python.
- Install the Twilio Python SDK. To install using pip, run:
pip install twilio
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Install Node.js.
- Install the Twilio Node.js SDK:
npm install twilio
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Install PHP.
- Install dependencies with Composer:
- Install Composer.
- Install the Twilio PHP SDK:
1composer require twilio/sdk2composer install
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Install Java Standard Edition (SE) Development Kit.
- Download the fat JAR file for the Twilio Java SDK that includes all dependencies:
- Navigate to the Maven repository.
- Click the most recent version number.
- In the Files row, click View All.
- Click the file ending in
jar-with-dependencies.jar.
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
- Install Ruby.
- Install the Twilio Ruby SDK:
gem install twilio-ruby
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
-
Most macOS and Linux machines come with cURL preinstalled.
- Sign up for Twilio and get a phone number:
-
To acquire a phone number, go to Products & services > Numbers & senders and click Set up a new phone number.
-
From the home page of the Console, copy your Account SID and paste it in a temporary local file for use later in this tutorial.
-
Create an API key, then copy the SID and secret into the same file.
-
Complete any applicable verification or registration requirements.
- Toll-free numbers: Complete toll-free verification to send SMS messages to recipients in the US and Canada.
- US 10DLC numbers: Register for A2P 10DLC to send SMS messages to recipients in the US. To learn more, see What is A2P 10DLC?.
- UK long code numbers: Submit a Regulatory Compliance (RC) bundle to send SMS messages to recipients in the UK. To learn more, see Know Your Customer (KYC) in the United Kingdom.
Follow these steps to send an SMS message from your Twilio phone number.
-
Create and open a new file called
send_message.pyanywhere on your machine and paste in the following code:1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID at twilio.com/console6# Provision API Keys at twilio.com/console/runtime/api-keys7# and set the environment variables. See http://twil.io/secure8# For local testing, you can use your Account SID and Auth token9api_key = os.environ["TWILIO_API_KEY"]10api_secret = os.environ["TWILIO_API_SECRET"]11account_sid = os.environ["TWILIO_ACCOUNT_SID"]12client = Client(api_key, api_secret, account_sid)1314message = client.messages.create(15body="Join Earth's mightiest heroes. Like Kevin Bacon.",16from_="+15017122661",17to="+15558675310",18)1920print(message.body)To learn all of the API response values that you can return with
print(), see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.(for example:print(message.status)returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with Git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
send_message.pyfile, replace the value fortowith the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Add a statement below
clientto define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.numbers = ["+15558675310", "+12345678901", "+10987654321"] -
Replace the
message =andprint()statements with the following code that iterates through each phone number in thenumberslist and returns the message sent to each number. Twilio makes one API call for each number.1for number in numbers:2message = client.messages.create(3body="This is the ship that made the Kessel Run in fourteen parsecs?",4from_="+15017122661",5to=number,6)7print(f"Sent to {number}: {message.body}")
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for
fromwith your Twilio phone number in E.164 format. -
Save your changes and run this command from your terminal in the directory that contains
send_message.py:python send_message.pyAfter a few moments, you should receive an SMS from your Twilio number.
-
Create and open a new file called
send_message.jsanywhere on your machine and paste in the following code:1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function createMessage() {14const message = await client.messages.create({15body: "This is the ship that made the Kessel Run in fourteen parsecs?",16from: "+15017122661",17to: "+15558675310",18});1920console.log(message.body);21}2223createMessage();To learn all of the API response values that you can return with
console.log(), see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.(for example:console.log(message.status)returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
send_message.jsfile, replace the value fortowith the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Add a
conststatement after the existingconststatements to define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.const numbers = ["+15558675310", "+12345678901", "+10987654321"]; -
Replace the
async function createMessage()block with the following code that iterates through each phone number in thenumberslist and returns the message sent to each number. Twilio makes one API call for each number.1async function createMessage() {2for (const number of numbers) {3const message = await client.messages.create({4body: "This is the ship that made the Kessel Run in fourteen parsecs?",5from: "+15017122661",6to: number,7});89console.log(`Sent to ${number}: ${message.body}`);10}11}
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for
fromwith your Twilio phone number in E.164 format. -
Save your changes and run this command from your terminal in the directory that contains
send_message.js:node send_message.jsAfter a few moments, you receive an SMS from your Twilio number.
-
Create and open a new file called
send_message.phpin the project directory and paste in the following code:1<?php23// Update the path below to your autoload.php,4// see https://getcomposer.org/doc/01-basic-usage.md5require_once "/path/to/vendor/autoload.php";67use Twilio\Rest\Client;89// Find your Account SID at twilio.com/console10// Provision API Keys at twilio.com/console/runtime/api-keys11// and set the environment variables. See http://twil.io/secure12// For local testing, you can use your Account SID and Auth token13$apiKey = $_ENV["TWILIO_API_KEY"];14$apiSecret = $_ENV["TWILIO_API_SECRET"];15$sid = $_ENV["TWILIO_ACCOUNT_SID"];16$twilio = new Client($apiKey, $apiSecret, $sid);1718$message = $twilio->messages->create(19"+15558675310", // To20[21"body" =>22"This is the ship that made the Kessel Run in fourteen parsecs?",23"from" => "+15017122661",24],25);2627print $message->body;To learn all of the API response values that you can return with
print, see the response for Send an SMS Message in the API documentation. Precede the response value with$message->(for example:print $message->status;returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
send_message.phpfile, replace the value fortowith the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Add a statement after the
$twilioto define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.$numbers = ["+15558675310", "+12345678901", "+10987654321"]; -
Replace the
$message =andprintstatements with the following code that iterates through each phone number in thenumberslist and returns the message sent to each number. Twilio makes one API call for each number.1foreach ($numbers as $to) {2$message = $twilio->messages->create(3$to, // To4[5"body" =>6"This is the ship that made the Kessel Run in fourteen parsecs?",7"from" => "+15017122661",8]9);1011print "Sent to $to: " . $message->body . "\n";12}
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for
fromwith your Twilio phone number in E.164 format. -
Update line 5 of
send_message.phptorequire __DIR__ . '/vendor/autoload.php'; -
Save your changes and run this command from your terminal in the directory that contains
send_message.php:php send_message.phpAfter a few moments, you receive an SMS from your Twilio number.
-
Set the environment variables for your Account SID, API key SID, and API key secret.
-
Start Visual Studio so it inherits those variables.
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secret4start devenvTo learn more, see Store your Twilio credentials safely.
-
Create and set up a new project in Visual Studio:
- In Visual Studio, click Create a new project.
- Click Console App (.NET Framework).
- Use the NuGet Package Manager to install the Twilio REST API SDK.
-
Open the file in your new Visual Studio project called
Program.csand paste in the following code, replacing the existing template code:1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;78class Program {9public static async Task Main(string[] args) {10// Find your Account SID at twilio.com/console11// Provision API Keys at twilio.com/console/runtime/api-keys12// and set the environment variables. See http://twil.io/secure13// For local testing, you can use your Account SID and Auth token14string apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");15string apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");16string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");1718TwilioClient.Init(apiKey, apiSecret, accountSid);1920var message = await MessageResource.CreateAsync(21body: "Join Earth's mightiest heroes. Like Kevin Bacon.",22from: new Twilio.Types.PhoneNumber("+15017122661"),23to: new Twilio.Types.PhoneNumber("+15558675310"));2425Console.WriteLine(message.Body);26}27}To learn all of the API response values that you can return with
Console.WriteLine(), see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.and capitalize the first letter of the response value (for example:Console.WriteLine(message.Status)returns thestatusvalue). -
In the
Program.csfile, replace the value forto: new Twilio.Types.PhoneNumberwith the recipient phone number in E.164 format.To send a message to multiple recipients, replace the
class Programstatement with the following code that iterates through each phone number in thenumberslist and returns the message sent to each number. Replace the phone numbers with your recipients' phone numbers in E.164 format. Twilio makes one API call for each number.1class Program {2public static async Task Main(string[] args) {3// Find your Account SID: https://www.twilio.com/console4// Create API keys: https://www.twilio.com/docs/iam/api-keys/keys-in-console5// and set the environment variables. See https://twil.io/secure6string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");7var apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");8var apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");910TwilioClient.Init(apiKey, apiSecret, accountSid);1112string[] numbers = { "+15558675310", "+12345678901", "+10987654321" };1314foreach (var number in numbers) {15var message = await MessageResource.CreateAsync(16body: "Join Earth's mightiest heroes. Like Kevin Bacon.",17from: new Twilio.Types.PhoneNumber("+15017122661"),18to: new Twilio.Types.PhoneNumber(number));1920Console.WriteLine(message.Body);21}22}23}(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
Replace the value for
from: new Twilio.Types.PhoneNumberwith your Twilio phone number in E.164 format. -
Save your changes and run your project in Visual Studio.
After a few moments, you receive an SMS from your Twilio number.
-
Run the following commands to create a new .NET project and install the Twilio NuGet package:
1mkdir TwilioSend2cd TwilioSend3dotnet new console4dotnet add package Twilio -
Open the file in your new project called
Program.csand paste in the following code, replacing the existing template code:1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;78class Program {9public static async Task Main(string[] args) {10// Find your Account SID at twilio.com/console11// Provision API Keys at twilio.com/console/runtime/api-keys12// and set the environment variables. See http://twil.io/secure13// For local testing, you can use your Account SID and Auth token14string apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");15string apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");16string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");1718TwilioClient.Init(apiKey, apiSecret, accountSid);1920var message = await MessageResource.CreateAsync(21body: "Join Earth's mightiest heroes. Like Kevin Bacon.",22from: new Twilio.Types.PhoneNumber("+15017122661"),23to: new Twilio.Types.PhoneNumber("+15558675310"));2425Console.WriteLine(message.Body);26}27}To learn all of the API response values that you can return with
Console.WriteLine(), see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.and capitalize the first letter of the response value (for example:Console.WriteLine(message.Status)returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
Program.csfile, replace the value forto: new Twilio.Types.PhoneNumberwith the recipient phone number in E.164 format.To send a message to multiple recipients, replace the
class Programstatement with the following code that iterates through each phone number in thenumberslist and returns the message sent to each number. Replace the phone numbers with your recipients' phone numbers in E.164 format. Twilio makes one API call for each number.1class Program {2public static async Task Main(string[] args) {3// Find your Account SID: https://www.twilio.com/console4// Create API keys: https://www.twilio.com/docs/iam/api-keys/keys-in-console5// and set the environment variables. See https://twil.io/secure6string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");7var apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");8var apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");910TwilioClient.Init(apiKey, apiSecret, accountSid);1112string[] numbers = { "+15558675310", "+12345678901", "+10987654321" };1314foreach (var number in numbers) {15var message = await MessageResource.CreateAsync(16body: "Join Earth's mightiest heroes. Like Kevin Bacon.",17from: new Twilio.Types.PhoneNumber("+15017122661"),18to: new Twilio.Types.PhoneNumber(number));1920Console.WriteLine(message.Body);21}22}23}(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
Replace the value for
from: new Twilio.Types.PhoneNumberwith your Twilio phone number in E.164 format. -
Save your changes and run this command in the directory that contains
Program.cs:dotnet runAfter a few moments, you receive an SMS from your Twilio number.
-
Create and open a new file called
Example.javain the same directory as the fat jar file and paste in the following code:1// Install the Java helper library from twilio.com/docs/java/install23import com.twilio.type.PhoneNumber;4import com.twilio.Twilio;5import com.twilio.rest.api.v2010.account.Message;67public class Example {8// Find your Account SID at twilio.com/console9// Provision API Keys at twilio.com/console/runtime/api-keys10// and set the environment variables. See http://twil.io/secure11// For local testing, you can use your Account SID and Auth token12public static final String API_KEY = System.getenv("TWILIO_API_KEY");13public static final String API_SECRET = System.getenv("TWILIO_API_SECRET");14public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");1516public static void main(String[] args) {17Twilio.init(API_KEY, API_SECRET, ACCOUNT_SID);18Message message = Message19.creator(new com.twilio.type.PhoneNumber("+15558675310"),20new com.twilio.type.PhoneNumber("+15017122661"),21"This is the ship that made the Kessel Run in fourteen parsecs?")22.create();2324System.out.println(message.getBody());25}26}To learn all of the API response values that you can return with
System.out.println();, see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.get, capitalize the response value, and add parentheses after the response value (for example:System.out.println(message.getStatus());returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
Example.javafile, replace the value for the first phone number with the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Replace the contents of the
public static void main(String[] args)block with the following code that iterates through each phone number in anumberslist and returns the message sent to each number. Twilio makes one API call for each number.1Twilio.init(API_KEY, API_SECRET, ACCOUNT_SID);23String[] numbers = {"+15558675310", "+12345678901", "+10987654321"};45for (String to : numbers) {6Message message = Message7.creator(new PhoneNumber(to),8new PhoneNumber("+15017122661"),9"This is the ship that made the Kessel Run in fourteen parsecs?")10.create();1112System.out.println(message.getBody());13} -
Replace the phone numbers in the
numberslist with your recipients' phone numbers in E.164 format.
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for the second
new PhoneNumber()with your Twilio phone number in E.164 format. -
Save your changes and compile the Java from your terminal in the directory that contains
Example.java. Replace10.9.0with the version of your fat jar file.javac -cp twilio-10.9.0-jar-with-dependencies.jar Example.java -
Run the Java program. Replace
10.9.0with the version of your fat JAR file. On Linux or macOS, run:java -cp .:twilio-10.9.0-jar-with-dependencies.jar ExampleOn Windows, run:
java -cp ".;twilio-10.9.0-jar-with-dependencies.jar" ExampleAfter a few moments, you receive an SMS from your Twilio number.
-
Create and set up your Go project.
-
Create a new Go project by running the following command:
go mod init twilio-example -
Install the Twilio Go SDK:
go get github.com/twilio/twilio-go
-
-
Create and open a new file called
send_message.goin your Go project directory and paste in the following code:1// Download the helper library from https://www.twilio.com/docs/go/install2package main34import (5"fmt"6"github.com/twilio/twilio-go"7api "github.com/twilio/twilio-go/rest/api/v2010"8"os"9)1011func main() {12// Find your Account SID at twilio.com/console13// Provision API Keys at twilio.com/console/runtime/api-keys14// and set the environment variables. See http://twil.io/secure15// For local testing, you can use your Account SID and Auth token16accountSid := os.Getenv("TWILIO_ACCOUNT_SID")17apiKey := os.Getenv("TWILIO_API_KEY")18apiSecret := os.Getenv("TWILIO_API_SECRET")19client := twilio.NewRestClientWithParams(twilio.ClientParams{20Username: apiKey,21Password: apiSecret,22AccountSid: accountSid,23})2425params := &api.CreateMessageParams{}26params.SetBody("Join Earth's mightiest heroes. Like Kevin Bacon.")27params.SetFrom("+15017122661")28params.SetTo("+15558675310")2930resp, err := client.Api.CreateMessage(params)31if err != nil {32fmt.Println(err.Error())33os.Exit(1)34} else {35if resp.Body != nil {36fmt.Println(*resp.Body)37} else {38fmt.Println(resp.Body)39}40}41}To learn all of the API response values that you can return with
fmt.Println(), see the response for Send an SMS Message in the API documentation. Precede the response value with*resp.and capitalize the first letter of the response value (for example:fmt.Println(*resp.Status)returns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
send_message.gofile, replace the value fortowith the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Add a statement below
clientto define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.numbers := []string{"+15558675310", "+12345678901", "+10987654321"} -
Replace the
paramsstatements with the following code that iterates through each phone number in thenumberslist. Twilio makes one API call for each number.1for _, number := range numbers {2params := &api.CreateMessageParams{}3params.SetBody("Join Earth's mightiest heroes. Like Kevin Bacon.")4params.SetFrom("+15017122661")5params.SetTo(number) -
Add an additional closing bracket
}to the end of the file.
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for
params.SetFromwith your Twilio phone number in E.164 format. -
Save your changes and run this command in the directory that contains
send_message.go:go run send_message.goAfter a few moments, you receive an SMS from your Twilio number.
-
Create and open a new file called
send_message.rbanywhere on your machine and paste in the following code:1# Download the helper library from https://www.twilio.com/docs/ruby/install2require 'twilio-ruby'34# Find your Account SID at twilio.com/console5# Provision API Keys at twilio.com/console/runtime/api-keys6# and set the environment variables. See http://twil.io/secure7# For local testing, you can use your Account SID and Auth token8api_key = ENV['TWILIO_API_KEY']9api_secret = ENV['TWILIO_API_SECRET']10account_sid = ENV['TWILIO_ACCOUNT_SID']11@client = Twilio::REST::Client.new(api_key, api_secret, account_sid)1213message = @client14.api15.v201016.messages17.create(18body: 'Join Earth\'s mightiest heroes. Like Kevin Bacon.',19from: '+15017122661',20to: '+15558675310'21)2223puts message.bodyTo learn all of the API response values that you can return with
puts, see the response for Send an SMS Message in the API documentation. Precede the response value withmessage.(for example:puts message.statusreturns thestatusvalue). -
Set the environment variables for your Account SID, API key SID, and API key secret.
On Mac or Linux:
-
Run the following commands to add your credentials as environment variables in a
twilio.envfile and source them. ReplaceACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env2echo "export TWILIO_API_KEY='SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" >> twilio.env3echo "export TWILIO_API_SECRET='your_api_secret'" >> twilio.env4source ./twilio.env -
If you're committing code with git, run the following command to add the
twilio.envfile to.gitignoreto avoid uploading your credentials in plain text:echo "twilio.env" >> .gitignore
On Windows command line (cmd.exe), run the following commands. Replace
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your Account SID,SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXwith your API key SID, andyour_api_secretwith your API key secret.1set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2set TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX3set TWILIO_API_SECRET=your_api_secretTo learn more, see Store your Twilio credentials safely.
-
-
In the
send_message.rbfile, replace the value fortowith the recipient phone number in E.164 format.To send a message to multiple recipients:
-
Add a statement below
@clientto define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.numbers = ['+15558675310', '+12345678901', '+10987654321'] -
Replace the
message =andputsstatements with the following code that iterates through each phone number in thenumberslist. Twilio makes one API call for each number.1numbers.each do |number|2message = @client3.api4.v20105.messages6.create(7body: 'Join Earth\'s mightiest heroes. Like Kevin Bacon.',8from: '+15017122661',9to: number10)11puts message.body12end
(information)Limits on message cadence
Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues.
-
-
Replace the value for
fromwith your Twilio phone number in E.164 format. -
Save your changes and run this command from your terminal in the directory that contains
send_message.rb:ruby send_message.rbAfter a few moments, you receive an SMS from your Twilio number.
-
Set the environment variables for your Account SID, API key SID, API key secret, your Twilio phone number in E.164 format, and the recipient's phone number in E.164 format.
On macOS or Linux, run the following commands. Replace
xxxxxxxxxwith your values.1export TWILIO_ACCOUNT_SID=xxxxxxxxx2export TWILIO_API_KEY=xxxxxxxxx3export TWILIO_API_SECRET=xxxxxxxxx4export TWILIO_NUMBER=xxxxxxxxx5export TO_NUMBER=xxxxxxxxxOn Windows command line (cmd.exe), run the following commands. Replace
xxxxxxxxxwith your values.1set TWILIO_ACCOUNT_SID=xxxxxxxxx2set TWILIO_API_KEY=xxxxxxxxx3set TWILIO_API_SECRET=xxxxxxxxx4set TWILIO_NUMBER=xxxxxxxxx5set TO_NUMBER=xxxxxxxxxTo learn more, see Store your Twilio credentials safely.
-
Run the cURL command from your terminal.
On macOS or Linux, run the following command. Replace the
Bodyvalue with the message you want to send.1curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \2--data-urlencode "Body=Hi there, this is a test message from cURL" \3--data-urlencode "From=+15017122661" \4--data-urlencode "To=+15558675310" \5-u $TWILIO_API_KEY:$TWILIO_API_SECRETOn Windows, run the following command. Replace the
Bodyvalue with the message you want to send.1curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \2--data-urlencode "Body=Hi there, this is a test message from cURL" \3--data-urlencode "From=+15017122661" \4--data-urlencode "To=+15558675310" \5-u $TWILIO_API_KEY:$TWILIO_API_SECRETAfter a few moments, you receive an SMS from your Twilio number.
Info
To learn which countries support MMS messages, see Sending and receiving MMS messages.
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the media_url parameter:
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID at twilio.com/console6# Provision API Keys at twilio.com/console/runtime/api-keys7# and set the environment variables. See http://twil.io/secure8# For local testing, you can use your Account SID and Auth token9api_key = os.environ["TWILIO_API_KEY"]10api_secret = os.environ["TWILIO_API_SECRET"]11account_sid = os.environ["TWILIO_ACCOUNT_SID"]12client = Client(api_key, api_secret, account_sid)1314message = client.messages.create(15body="This is the ship that made the Kessel Run in fourteen parsecs?",16from_="+15017122661",17media_url=[18"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"19],20to="+15558675310",21)2223print(message.body)
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the mediaUrl parameter:
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function createMessage() {14const message = await client.messages.create({15body: "This is the ship that made the Kessel Run in fourteen parsecs?",16from: "+15017122661",17mediaUrl: [18"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg",19],20to: "+15558675310",21});2223console.log(message.body);24}2526createMessage();
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the mediaUrl parameter:
1<?php23// Update the path below to your autoload.php,4// see https://getcomposer.org/doc/01-basic-usage.md5require_once "/path/to/vendor/autoload.php";67use Twilio\Rest\Client;89// Find your Account SID at twilio.com/console10// Provision API Keys at twilio.com/console/runtime/api-keys11// and set the environment variables. See http://twil.io/secure12// For local testing, you can use your Account SID and Auth token13$apiKey = $_ENV["TWILIO_API_KEY"];14$apiSecret = $_ENV["TWILIO_API_SECRET"];15$sid = $_ENV["TWILIO_ACCOUNT_SID"];16$twilio = new Client($apiKey, $apiSecret, $sid);1718$message = $twilio->messages->create(19"+15558675310", // To20[21"body" =>22"This is the ship that made the Kessel Run in fourteen parsecs?",23"from" => "+15017122661",24"mediaUrl" => [25"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg",26],27],28);2930print $message->body;
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the mediaUrl parameter:
1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;7using System.Collections.Generic;89class Program {10public static async Task Main(string[] args) {11// Find your Account SID at twilio.com/console12// Provision API Keys at twilio.com/console/runtime/api-keys13// and set the environment variables. See http://twil.io/secure14// For local testing, you can use your Account SID and Auth token15string apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");16string apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");17string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");1819TwilioClient.Init(apiKey, apiSecret, accountSid);2021var message = await MessageResource.CreateAsync(22body: "This is the ship that made the Kessel Run in fourteen parsecs?",23from: new Twilio.Types.PhoneNumber("+15017122661"),24mediaUrl: new List<Uri> { new Uri(25"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg") },26to: new Twilio.Types.PhoneNumber("+15558675310"));2728Console.WriteLine(message.Body);29}30}
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the mediaUrl parameter:
1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;7using System.Collections.Generic;89class Program {10public static async Task Main(string[] args) {11// Find your Account SID at twilio.com/console12// Provision API Keys at twilio.com/console/runtime/api-keys13// and set the environment variables. See http://twil.io/secure14// For local testing, you can use your Account SID and Auth token15string apiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");16string apiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");17string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");1819TwilioClient.Init(apiKey, apiSecret, accountSid);2021var message = await MessageResource.CreateAsync(22body: "This is the ship that made the Kessel Run in fourteen parsecs?",23from: new Twilio.Types.PhoneNumber("+15017122661"),24mediaUrl: new List<Uri> { new Uri(25"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg") },26to: new Twilio.Types.PhoneNumber("+15558675310"));2728Console.WriteLine(message.Body);29}30}
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Add the required import statements for java.net.URI and java.util.Arrays and add the .setMediaUrl() statement:
1// Install the Java helper library from twilio.com/docs/java/install23import com.twilio.type.PhoneNumber;4import java.net.URI;5import java.util.Arrays;6import com.twilio.Twilio;7import com.twilio.rest.api.v2010.account.Message;89public class Example {10// Find your Account SID at twilio.com/console11// Provision API Keys at twilio.com/console/runtime/api-keys12// and set the environment variables. See http://twil.io/secure13// For local testing, you can use your Account SID and Auth token14public static final String API_KEY = System.getenv("TWILIO_API_KEY");15public static final String API_SECRET = System.getenv("TWILIO_API_SECRET");16public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");1718public static void main(String[] args) {19Twilio.init(API_KEY, API_SECRET, ACCOUNT_SID);20Message message = Message21.creator(new com.twilio.type.PhoneNumber("+15558675310"),22new com.twilio.type.PhoneNumber("+15017122661"),23"This is the ship that made the Kessel Run in fourteen parsecs?")24.setMediaUrl(Arrays.asList(25URI.create("https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg")))26.create();2728System.out.println(message.getBody());29}30}
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Add the params.SetMediaUrl() statement:
1// Download the helper library from https://www.twilio.com/docs/go/install2package main34import (5"fmt"6"github.com/twilio/twilio-go"7api "github.com/twilio/twilio-go/rest/api/v2010"8"os"9)1011func main() {12// Find your Account SID at twilio.com/console13// Provision API Keys at twilio.com/console/runtime/api-keys14// and set the environment variables. See http://twil.io/secure15// For local testing, you can use your Account SID and Auth token16accountSid := os.Getenv("TWILIO_ACCOUNT_SID")17apiKey := os.Getenv("TWILIO_API_KEY")18apiSecret := os.Getenv("TWILIO_API_SECRET")19client := twilio.NewRestClientWithParams(twilio.ClientParams{20Username: apiKey,21Password: apiSecret,22AccountSid: accountSid,23})2425params := &api.CreateMessageParams{}26params.SetBody("This is the ship that made the Kessel Run in fourteen parsecs?")27params.SetFrom("+15017122661")28params.SetMediaUrl([]string{29"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg",30})31params.SetTo("+15558675310")3233resp, err := client.Api.CreateMessage(params)34if err != nil {35fmt.Println(err.Error())36os.Exit(1)37} else {38if resp.Body != nil {39fmt.Println(*resp.Body)40} else {41fmt.Println(resp.Body)42}43}44}
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the media_url parameter:
1# Download the helper library from https://www.twilio.com/docs/ruby/install2require 'twilio-ruby'34# Find your Account SID at twilio.com/console5# Provision API Keys at twilio.com/console/runtime/api-keys6# and set the environment variables. See http://twil.io/secure7# For local testing, you can use your Account SID and Auth token8api_key = ENV['TWILIO_API_KEY']9api_secret = ENV['TWILIO_API_SECRET']10account_sid = ENV['TWILIO_ACCOUNT_SID']11@client = Twilio::REST::Client.new(api_key, api_secret, account_sid)1213message = @client14.api15.v201016.messages17.create(18body: 'This is the ship that made the Kessel Run in fourteen parsecs?',19from: '+15017122661',20media_url: [21'https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg'22],23to: '+15558675310'24)2526puts message.body
To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.
The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.
When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource. Once you create a resource, you can access it at any time by using the API.
Set the MediaUrl parameter:
On macOS or Linux:
1curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \2--data-urlencode "Body=Hi there, this is a test message from cURL" \3--data-urlencode "MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg" \4--data-urlencode "From=+15017122661" \5--data-urlencode "To=+15558675310" \6-u $TWILIO_API_KEY:$TWILIO_API_SECRET
On Windows:
1curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \2--data-urlencode "Body=Hi there, this is a test message from cURL" \3--data-urlencode "MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg" \4--data-urlencode "From=+15017122661" \5--data-urlencode "To=+15558675310" \6-u $TWILIO_API_KEY:$TWILIO_API_SECRET
- View more Messaging tutorials
- Sending high-volume messages? Learn more about Messaging services
- Browse the following developer resources: