How to Send SMS Messages with Copilot

Messaging Copilot improves your customers' SMS message experience with routing and content intelligence features that you can control from the Twilio console. Copilot can localize outgoing phone numbers, distribute bulk text messaging across multiple senders, lock one number to a customer and much more.

This guide will help you understand what Copilot can do for your business and show you how to send SMS messages with Copilot in your application. 

Choose your favorite programming language and let's get started!

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Response Format:
  • json
SDK Version:
  • 6.x
  • 7.x
SDK Version:
  • 2.x
  • 3.x
SDK Version:
  • 4.x
  • 5.x
SDK Version:
  • 5.x
  • 6.x
SDK Version:
  • 4.x
  • 5.x
Format:
  • JSON
  • XML
// Download the Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.sendMessage(
  {
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.',
  },
  (err, message) => {
    console.log(message);
  }
);
// Download the Node helper library from twilio.com/docs/node/install
// These consts are your accountSid and authToken from https://www.twilio.com/console
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.',
  })
  .then(message => console.log(message));
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio;
class Example
{
 static void Main(string[] args)
 {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "[AuthToken]";
    var twilio = new TwilioRestClient(AccountSid, AuthToken);

    var message = twilio.SendMessageWithService(
        "MG9752274e9e519418a7406176694466fa", "+441632960675",
        "Phantom Menace was clearly the best of the prequel trilogy."
    );
    Console.WriteLine(message.Sid);
 }
}
<?php

// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');

// Download the PHP helper library from twilio.com/docs/php/install
// These vars are your accountSid and authToken from twilio.com/user/account
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$client = new Services_Twilio($account_sid, $auth_token);

$client->account->messages->create(array(
    'To' => "+441632960675",
    'MessagingServiceSid' => "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    'Body' => "Phantom Menace was clearly the best of the prequel trilogy.",
));
require 'rubygems' # not necessary with ruby 1.9 but included for completeness
require 'twilio-ruby'

# put your own credentials here
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = '[AuthToken]'

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

@client.account.messages.create({
  :messaging_service_sid => 'MG9752274e9e519418a7406176694466fa',
  :to => '+441632960675',
  :body => 'Phantom Menace was clearly the best of the prequel trilogy.'
})
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

class Example
{
   static void Main(string[] args)
   {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "[AuthToken]";
        TwilioClient.Init(accountSid, authToken);

        var to = new PhoneNumber("+441632960675");
        var message = MessageResource.Create(
            to,
            messagingServiceSid: "MG9752274e9e519418a7406176694466fa",
            body: "Phantom Menace was clearly the best of the prequel trilogy.");

        Console.WriteLine(message.Sid);
   }
}
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Download the PHP helper library from twilio.com/docs/php/install
// These vars are your accountSid and authToken from twilio.com/user/account
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$client = new Client($account_sid, $auth_token);

$client->messages->create(
    "+441632960675",
    array(
        'messagingServiceSid' => "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        'body' => "Phantom Menace was clearly the best of the prequel trilogy."
    )
);
from twilio.rest import TwilioRestClient

# put your own credentials here
ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
AUTH_TOKEN = "[AuthToken]"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

client.messages.create(
    to="+441632960675",
    messaging_service_sid="MG9752274e9e519418a7406176694466fa",
    body="Phantom Menace was clearly the best of the prequel trilogy.",
)
require 'rubygems' # not necessary with ruby 1.9 but included for completeness
require 'twilio-ruby'

# put your own credentials here
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new(account_sid, auth_token)

@client.messages.create(
  messaging_service_sid: 'MG9752274e9e519418a7406176694466fa',
  to: '+441632960675',
  body: 'Phantom Menace was clearly the best of the prequel trilogy.'
)
// You may want to be more specific in your imports
import java.util.*;
import com.twilio.sdk.*;
import com.twilio.sdk.resource.factory.*;
import com.twilio.sdk.resource.instance.*;
import com.twilio.sdk.resource.list.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class TwilioTest {
  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "[AuthToken]";

  public static void main(String[]args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    // Build the parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", "+441632960675"));
    params.add(new BasicNameValuePair("MessagingServiceSid", "MG9752274e9e519418a7406176694466fa"));
    params.add(new BasicNameValuePair("Body",
      "Phantom Menace was clearly the best of the prequel trilogy."));

    MessageFactory messageFactory = client.getAccount().getMessageFactory();
    Message message = messageFactory.create(params);
    System.out.println(message.getSid());
  }
}
from twilio.rest import Client

# put your own credentials here
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "[AuthToken]"

client = Client(account_sid, auth_token)

client.messages.create(
    to="+441632960675",
    messaging_service_sid="MG9752274e9e519418a7406176694466fa",
    body="Phantom Menace was clearly the best of the prequel trilogy."
)
// Install the Java helper library from twilio.com/docs/java/install
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "your_auth_token";

  public static void main(String[] args) {
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    Message message =
        Message.creator(new PhoneNumber("+441632960675"), "MG9752274e9e519418a7406176694466fa",
            "Phantom Menace was clearly the best of the prequel trilogy.").create();

    System.out.println(message.getSid());
  }
}
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json' \
  --data-urlencode 'To=+441632960675'  \
  --data-urlencode 'MessagingServiceSid=MG9752274e9e519418a7406176694466fa'  \
  --data-urlencode 'Body=Phantom Menace was clearly the best of the prequel trilogy.'  \
  -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:auth_token
{
  "sid": "SM18c9444687c240caa8b54dc9a22e14d8",
  "date_created": "Tue, 03 Nov 2015 20:26:16 +0000",
  "date_updated": "Tue, 03 Nov 2015 20:26:16 +0000",
  "date_sent": null,
  "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "+441632960675",
  "from": null,
  "messaging_service_sid": "MG9752274e9e519418a7406176694466fa",
  "body": "Phantom Menace was clearly the best of the prequel trilogy.",
  "status": "accepted",
  "num_segments": "0",
  "num_media": "0",
  "direction": "outbound-api",
  "api_version": "2010-04-01",
  "price": null,
  "price_unit": null,
  "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM18c9444687c240caa8b54dc9a22e14d8.json",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM18c9444687c240caa8b54dc9a22e14d8/Media.json"
  }
}
<?xml version='1.0' encoding='UTF-8'?>
<TwilioResponse>
    <Message>
        <Sid>SM802a848d6e5948aea0a2780200f70825</Sid>
        <DateCreated>Tue, 03 Nov 2015 20:28:44 +0000</DateCreated>
        <DateUpdated>Tue, 03 Nov 2015 20:28:44 +0000</DateUpdated>
        <DateSent/>
        <AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
        <To>+441632960675</To>
        <From/>
        <MessagingServiceSid>MG9752274e9e519418a7406176694466fa</MessagingServiceSid>
        <Body>Phantom Menace was clearly the best of the prequel trilogy.</Body>
        <Status>accepted</Status>
        <NumSegments>0</NumSegments>
        <NumMedia>0</NumMedia>
        <Direction>outbound-api</Direction>
        <ApiVersion>2010-04-01</ApiVersion>
        <Price/>
        <PriceUnit/>
        <Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM802a848d6e5948aea0a2780200f70825.xml</Uri>
        <SubresourceUris>
            <Media>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM802a848d6e5948aea0a2780200f70825/Media.xml</Media>
        </SubresourceUris>
    </Message>
</TwilioResponse>
Send a Message with Copilot
Jarod Reyes
Paul Kamp
Kat King
David Prothero
Mario Celi

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 browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
Response Format:
  • json
SDK Version:
  • 6.x
  • 7.x
SDK Version:
  • 2.x
  • 3.x
SDK Version:
  • 4.x
  • 5.x
SDK Version:
  • 5.x
  • 6.x
SDK Version:
  • 4.x
  • 5.x
Format:
  • JSON
  • XML
// Download the Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.sendMessage(
  {
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.',
  },
  (err, message) => {
    console.log(message);
  }
);
// Download the Node helper library from twilio.com/docs/node/install
// These consts are your accountSid and authToken from https://www.twilio.com/console
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.',
  })
  .then(message => console.log(message));
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio;
class Example
{
 static void Main(string[] args)
 {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "[AuthToken]";
    var twilio = new TwilioRestClient(AccountSid, AuthToken);

    var message = twilio.SendMessageWithService(
        "MG9752274e9e519418a7406176694466fa", "+441632960675",
        "Phantom Menace was clearly the best of the prequel trilogy."
    );
    Console.WriteLine(message.Sid);
 }
}
<?php

// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');

// Download the PHP helper library from twilio.com/docs/php/install
// These vars are your accountSid and authToken from twilio.com/user/account
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$client = new Services_Twilio($account_sid, $auth_token);

$client->account->messages->create(array(
    'To' => "+441632960675",
    'MessagingServiceSid' => "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    'Body' => "Phantom Menace was clearly the best of the prequel trilogy.",
));
require 'rubygems' # not necessary with ruby 1.9 but included for completeness
require 'twilio-ruby'

# put your own credentials here
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = '[AuthToken]'

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

@client.account.messages.create({
  :messaging_service_sid => 'MG9752274e9e519418a7406176694466fa',
  :to => '+441632960675',
  :body => 'Phantom Menace was clearly the best of the prequel trilogy.'
})
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

class Example
{
   static void Main(string[] args)
   {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "[AuthToken]";
        TwilioClient.Init(accountSid, authToken);

        var to = new PhoneNumber("+441632960675");
        var message = MessageResource.Create(
            to,
            messagingServiceSid: "MG9752274e9e519418a7406176694466fa",
            body: "Phantom Menace was clearly the best of the prequel trilogy.");

        Console.WriteLine(message.Sid);
   }
}
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Download the PHP helper library from twilio.com/docs/php/install
// These vars are your accountSid and authToken from twilio.com/user/account
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$client = new Client($account_sid, $auth_token);

$client->messages->create(
    "+441632960675",
    array(
        'messagingServiceSid' => "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        'body' => "Phantom Menace was clearly the best of the prequel trilogy."
    )
);
from twilio.rest import TwilioRestClient

# put your own credentials here
ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
AUTH_TOKEN = "[AuthToken]"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

client.messages.create(
    to="+441632960675",
    messaging_service_sid="MG9752274e9e519418a7406176694466fa",
    body="Phantom Menace was clearly the best of the prequel trilogy.",
)
require 'rubygems' # not necessary with ruby 1.9 but included for completeness
require 'twilio-ruby'

# put your own credentials here
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new(account_sid, auth_token)

@client.messages.create(
  messaging_service_sid: 'MG9752274e9e519418a7406176694466fa',
  to: '+441632960675',
  body: 'Phantom Menace was clearly the best of the prequel trilogy.'
)
// You may want to be more specific in your imports
import java.util.*;
import com.twilio.sdk.*;
import com.twilio.sdk.resource.factory.*;
import com.twilio.sdk.resource.instance.*;
import com.twilio.sdk.resource.list.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class TwilioTest {
  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "[AuthToken]";

  public static void main(String[]args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    // Build the parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", "+441632960675"));
    params.add(new BasicNameValuePair("MessagingServiceSid", "MG9752274e9e519418a7406176694466fa"));
    params.add(new BasicNameValuePair("Body",
      "Phantom Menace was clearly the best of the prequel trilogy."));

    MessageFactory messageFactory = client.getAccount().getMessageFactory();
    Message message = messageFactory.create(params);
    System.out.println(message.getSid());
  }
}
from twilio.rest import Client

# put your own credentials here
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "[AuthToken]"

client = Client(account_sid, auth_token)

client.messages.create(
    to="+441632960675",
    messaging_service_sid="MG9752274e9e519418a7406176694466fa",
    body="Phantom Menace was clearly the best of the prequel trilogy."
)
// Install the Java helper library from twilio.com/docs/java/install
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "your_auth_token";

  public static void main(String[] args) {
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    Message message =
        Message.creator(new PhoneNumber("+441632960675"), "MG9752274e9e519418a7406176694466fa",
            "Phantom Menace was clearly the best of the prequel trilogy.").create();

    System.out.println(message.getSid());
  }
}
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json' \
  --data-urlencode 'To=+441632960675'  \
  --data-urlencode 'MessagingServiceSid=MG9752274e9e519418a7406176694466fa'  \
  --data-urlencode 'Body=Phantom Menace was clearly the best of the prequel trilogy.'  \
  -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:auth_token
{
  "sid": "SM18c9444687c240caa8b54dc9a22e14d8",
  "date_created": "Tue, 03 Nov 2015 20:26:16 +0000",
  "date_updated": "Tue, 03 Nov 2015 20:26:16 +0000",
  "date_sent": null,
  "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "+441632960675",
  "from": null,
  "messaging_service_sid": "MG9752274e9e519418a7406176694466fa",
  "body": "Phantom Menace was clearly the best of the prequel trilogy.",
  "status": "accepted",
  "num_segments": "0",
  "num_media": "0",
  "direction": "outbound-api",
  "api_version": "2010-04-01",
  "price": null,
  "price_unit": null,
  "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM18c9444687c240caa8b54dc9a22e14d8.json",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM18c9444687c240caa8b54dc9a22e14d8/Media.json"
  }
}
<?xml version='1.0' encoding='UTF-8'?>
<TwilioResponse>
    <Message>
        <Sid>SM802a848d6e5948aea0a2780200f70825</Sid>
        <DateCreated>Tue, 03 Nov 2015 20:28:44 +0000</DateCreated>
        <DateUpdated>Tue, 03 Nov 2015 20:28:44 +0000</DateUpdated>
        <DateSent/>
        <AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
        <To>+441632960675</To>
        <From/>
        <MessagingServiceSid>MG9752274e9e519418a7406176694466fa</MessagingServiceSid>
        <Body>Phantom Menace was clearly the best of the prequel trilogy.</Body>
        <Status>accepted</Status>
        <NumSegments>0</NumSegments>
        <NumMedia>0</NumMedia>
        <Direction>outbound-api</Direction>
        <ApiVersion>2010-04-01</ApiVersion>
        <Price/>
        <PriceUnit/>
        <Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM802a848d6e5948aea0a2780200f70825.xml</Uri>
        <SubresourceUris>
            <Media>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM802a848d6e5948aea0a2780200f70825/Media.xml</Media>
        </SubresourceUris>
    </Message>
</TwilioResponse>