Sending Messages with Copilot

Public Beta
Twilio's Messaging Services API is currently available as a Public Beta product. Some features are not yet implemented and others may be changed before the product is declared as Generally Available.
Public Beta products are not covered by a Twilio SLA.

Sending a single message with Twilio is simple. But once we launch our application in the US and globally, at scale, our application will need to decide which identity (phone number, shortcode, or alpha sender ID) to use to send the message. We may want to spread our messaging traffic over several US phone numbers or prioritize an alpha sender ID when sending messages to countries where alpha senders are commonly used.

Instead of attempting to handle this number selection logic in our application, we can use Twilio’s Copilot. When enabled Twilio's Copilot features will assist our application to improve the performance of our messages. To use Twilio's Copilot features, first we'll need to setup a Messaging Service, where we'll define our Copilot configuration.

In this guide, we'll walk through setting up our Messaging Service, via the REST API. Once configured, we'll use our Messaging Service to send messages instead of specifying the phone number ourselves. Twilio's Copilot will then select the best identity, either a phone number or an alpha sender ID, to send our messages in the US and globally.

Let’s dive into setting up a Messaging Service.

Alternatively, Messaging Services can be configured through the Twilio Console when logged in. This is extremely valuable when we want to make configuration changes to our application in the future without deploying any code.

Create a Messaging Service

Before Copilot can intelligently select the phone numbers to send your messages and applying message content changes, a Service must first be created. The Service resource holds all the configurations we want applied to our messages.

Here is how we’ll create a Messaging Service:

Loading Code Samples...
Language
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.Service;

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

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

        Service service = Service.creator("My First Service").create();
    }
}
curl 'https://messaging.twilio.com/v1/Services' -X POST \
    --data-urlencode 'FriendlyName=My First Service' \
    --data-urlencode 'StatusCallback=http://requestb.in/1234abcd' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

service = client.messaging \
                .services \
                .create(friendly_name="My First Service")

print(service.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1;


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 = "your_auth_token";
      const string serviceFriendlyName = "My First Service";

      TwilioClient.Init(accountSid, authToken);

      var service = ServiceResource.Create(serviceFriendlyName);

      Console.WriteLine(service.Sid);
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$service = $twilio->messaging->v1->services->create("My First Service",
                                                    array('statusCallback' => "http://requestb.in/1234abcd")
                                                   );
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@service = @client.messaging.v1
  .services
  .create(
    friendly_name: 'My First Service',
    status_callback: 'http://requestb.in/1234abcd')

puts @service.sid
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);

const opts = {
  friendlyName: 'My First Service',
  statusCallback: 'http://requestb.in/1234abcd'
};

client.messaging.services.create(opts)
    .then(function(response) {
      console.log(response);
    }).catch(function(error) {
      console.log(error);
    });
{
     "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
     "sid": "MG2172dd2db502e20dd981ef0d67850e1a",
     "date_created": "2017-03-14T20:12:31Z",
     "date_updated": "2017-03-14T20:12:33Z",
     "friendly_name": "My First Service!",
     "inbound_request_url": null,
     "inbound_method": "POST",
     "fallback_url": null,
     "fallback_method": "POST",
     "status_callback": "http://requestb.in/1234abcd",
     "sticky_sender": true,
     "mms_converter": true,
     "links": {
     "phone_numbers": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers",
     "short_codes": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/ShortCodes",
     "alpha_senders": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/AlphaSenders"
                    },
     "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a"
}
Create a Messaging Service

We’ll also want to make sure we set the Status Callback property because any phone number validation errors, message delivery updates, and queuing errors will be webhooked to our application through this URL. This will let us know when our messages are delivered, and if our message fails, the errors we may be experiencing.

Upon creating a Service, some of our Service configuration will be set by default. For more detail about all the properties of a Service and these default settings, take a look at our Service Resource Reference.

Add phone numbers to a Service

Once our Service has been created, we’ll need to add phone numbers, short codes or an Alpha Sender ID to the Service. This provides Copilot with the identities to select when sending our messages.

Loading Code Samples...
Language
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        String phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";
        PhoneNumber phoneNumber = PhoneNumber.creator(pathServiceSid, phoneNumberSid)
                                             .create();
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN557ce644e5ab84fa21cc21112e22c485' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_number = client.messaging \
                     .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                     .phone_numbers \
                     .create(phone_number_sid="PN557ce644e5ab84fa21cc21112e22c485")

print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
      const string phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";

      TwilioClient.Init(accountSid, authToken);

      var phoneNumber = PhoneNumberResource.Create(pathServiceSid, phoneNumberSid);

      Console.WriteLine(phoneNumber.Sid);

    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumber = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers->create("PN557ce644e5ab84fa21cc21112e22c485");
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_number = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers
  .create(phone_number_sid: 'PN557ce644e5ab84fa21cc21112e22c485')

puts @phone_number.sid
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers.create({phoneNumberSid: 'PN557ce644e5ab84fa21cc21112e22c485'})
    .then(function(response) {
      console.log(response);
    }).catch(function(error) {
      console.log(error);
    });
{
    "sid": "PN557ce644e5ab84fa21cc21112e22c485",
    "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
    "date_created": "2017-03-14T20:12:31Z",
    "date_updated": "2017-03-14T20:12:33Z",
    "phone_number": "+15017250604",
    "country_code": "US",
    "capabilities": ["sms", "mms"],
    "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485"
}
Add a Phone Number to a Messaging Service

To add phone numbers to our Service, we first purchased the numbers to our Account. If you haven’t purchased any phone numbers yet, use the Incoming Phone Numbers API first to buy a few phone numbers.

Since we’ll be sending messages to the US and CA, our messages will be throttled at the rate of about 1 message per second. To make sure our Service can handle our volume, we’ll want to add a few numbers. Copilot’s Scaler will help distribute our traffic over all our phone numbers.

We’ll also add a few UK and Australian phone numbers for our users in those countries. Copilot’s Geomatch will select the phone number that is local to my user, if available. So my British and Australian customer will now receive messages from the country that matches their phone number.

Loading Code Samples...
Language
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

import java.util.Arrays;
import java.util.List;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        List<String> phoneNumberSids = Arrays
                .asList("PN2a0747eba6abf96b7e3c3ff0b4530f6e",
                        "PN557ce644e5ab84fa21cc21112e22c485",
                        "PN2a0747eba6abf96b7e3c3ff0b4530f6e");

        for (String phoneNumberSid : phoneNumberSids) {
            PhoneNumber phoneNumber = PhoneNumber
                    .creator(pathServiceSid, phoneNumberSid)
                    .create();

            System.out.println(phoneNumber);
        }
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN2a0747eba6abf96b7e3c3ff0b4530f6e' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
    
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN557ce644e5ab84fa21cc21112e22c485' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
    
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN2a0747eba6abf96b7e3c3ff0b4530f6e' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_numbers_sids = ["PN2a0747eba6abf96b7e3c3ff0b4530f6e",
                      "PN557ce644e5ab84fa21cc21112e22c485",
                      "PN2a0747eba6abf96b7e3c3ff0b4530f6e"]

for phone_numbers_sid in phone_numbers_sids:
    phone_number = client.messaging \
                         .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                         .phone_numbers \
                         .create(phone_number_sid=phone_numbers_sid)
    print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
    const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";

    var phoneNumberSids = new List<string> {
      "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
      "PN557ce644e5ab84fa21cc21112e22c485",
      "PN2a0747eba6abf96b7e3c3ff0b4530f6e"
    };

    TwilioClient.Init(accountSid, authToken);

    foreach(var phoneNumberSid in phoneNumberSids)
    {
        var phoneNumber = PhoneNumberResource.Create(pathServiceSid, phoneNumberSid);
        Console.WriteLine(phoneNumber.Sid);
    }
  }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = array("PN2a0747eba6abf96b7e3c3ff0b4530f6e",
    "PN557ce644e5ab84fa21cc21112e22c485",
    "PN2a0747eba6abf96b7e3c3ff0b4530f6e");

foreach ($phoneNumbers as $number) {
    $phoneNumber = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                           ->phoneNumbers->create($number);
}
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_number_sids = ['PN2a0747eba6abf96b7e3c3ff0b4530f6e',
                      'PN557ce644e5ab84fa21cc21112e22c485',
                      'PN2a0747eba6abf96b7e3c3ff0b4530f6e']

@phone_number_sids.each do |phone_number_sid|
  @phone_number = @client.messaging.v1
    .services('MG2172dd2db502e20dd981ef0d67850e1a')
    .phone_numbers
    .create(phone_number_sid: phone_number_sid)

  puts @phone_number.sid
end
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

const phoneNumbers = ['PN2a0747eba6abf96b7e3c3ff0b4530f6e',
                      'PN557ce644e5ab84fa21cc21112e22c485',
                      'PN2a0747eba6abf96b7e3c3ff0b4530f6e'];

phoneNumbers.forEach((phoneNumber) => {
  service.phoneNumbers.create({phoneNumberSid: phoneNumber})
      .then(function(response) {
        console.log(response);
      }).catch(function(error) {
        console.log(error);
      });
});
{
    "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
    "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
    "date_created": "2017-03-14T20:15:31Z",
    "date_updated": "2017-03-14T20:15:33Z",
    "phone_number": "+441632960556",
    "country_code": "GB",
    "capabilities": ["sms"],
    "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
}
Add multiple phone numbers to Messaging Services

List phone numbers in a Service

Let’s double check and confirm all the phone numbers in our Service.

Loading Code Samples...
Language
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        ResourceSet<PhoneNumber> phoneNumbers = PhoneNumber.reader(pathServiceSid)
                                                           .read();

        for (PhoneNumber phoneNumber : phoneNumbers) {
            System.out.println(phoneNumber);
        }
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_numbers = client.messaging \
                      .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                      .phone_numbers \
                      .list()

for phone_number in phone_numbers:
    print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";

      TwilioClient.Init(accountSid, authToken);

      var phoneNumbers = PhoneNumberResource.Read(pathServiceSid);

      foreach (var phoneNumber in phoneNumbers)
      {
        Console.WriteLine(phoneNumber.Sid);
      }
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers->read();

foreach ($phoneNumbers as $phoneNumber) {
    print($phoneNumber->phoneNumber);
}
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_numbers = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers.list

@phone_numbers.each do |phone_number|
  puts phone_number.phone_number
end
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers.list()
       .then(function(response) {
         console.log(response);
       }).catch(function(error) {
         console.log(error);
       });
{
    "meta": {
        "page": 0,
        "page_size": 50,
        "first_page_url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers?PageSize=50&Page=0",
        "previous_page_url": null,
        "next_page_url": null,
        "key": "phone_numbers",
        "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers?PageSize=50&Page=0"
                    },
    "phone_numbers": [
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN557ce644e5ab84fa21cc21112e22c485",
            "date_created": "2017-03-14T20:12:31Z",
            "date_updated": "2017-03-14T20:12:33Z",
            "phone_number": "+987654321",
            "country_code": "US",
            "capabilities": ["sms", "mms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485"
        },
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
            "date_created": "2017-03-14T20:15:31Z",
            "date_updated": "2017-03-14T20:15:33Z",
            "phone_number": "+441632960556",
            "country_code": "GB",
            "capabilities": ["sms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
        }
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
            "date_created": "2017-03-14T20:15:31Z",
            "date_updated": "2017-03-14T20:15:33Z",
            "phone_number": "+611900654321",
            "country_code": "AU",
            "capabilities": ["sms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
        }
    ]
}
List all Phone Numbers in a Messaging Service

Adding an Alpha Sender ID

Since we’ll be sending messages to countries where branded messages are popular we’ll also want to add an Alpha Sender ID. This allows our users to quickly identify our messages upon receiving them.

Sending messages with Copilot

Now that our Messaging Service is all configured, we ready to start sending messages! At the moment to send messages we'll need to make requests to Twilio's 2010-04-01 API version to send messages.

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
// 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
// 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.")
// 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 twilio.com/docs/php/install
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.'
)
// 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 Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var authToken = "your_auth_token";
var client = require('twilio')(accountSid, authToken);

client.sendMessage({
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.'
}, function(err, message) {
    console.log(message);
});
{
  "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

Receive Message Status Updates

As our message is getting delivered, Twilio will be making webhook requests to our Status Callback URL to let us know the delivery status of our message.

If we happen to send a message to an invalid phone number, we’ll see an error in the webhook.

{
  "sid": "SM18c9444687c240caa8b54dc9a22e14d8",
  "date_created": "Wed, 15 Mar 2017 07:07:03 +0000",
  "date_updated": "Wed, 15 Mar 2017 07:07:03 +0000",
  "date_sent": "Wed, 15 Mar 2017 07:07:13 +0000",
  "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "+441632960675",
  "from": "+441632960556",
  "body": "Phantom Menace was clearly the best of the prequel trilogy.",
  "status": "delivered",
  "messaging_service_sid": "MG9752274e9e519418a7406176694466fa"
  "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/SM18c9444687c240caa8b54dc9a22e14d8.json",
}

Removing a phone number

Let’s say we have a phone number that we no longer need in this Service. We can go ahead and remove the phone number.

Loading Code Samples...
Language
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        String phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";
        PhoneNumber.deleter(pathServiceSid, phoneNumberSid).delete();
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485' -X DELETE \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

did_delete = client.messaging \
                   .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                   .phone_numbers(sid="PN557ce644e5ab84fa21cc21112e22c485") \
                   .delete()

print(did_delete)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
      const string phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";

      TwilioClient.Init(accountSid, authToken);

      var deleted = PhoneNumberResource.Delete(pathServiceSid, phoneNumberSid);

      Console.WriteLine(deleted);
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers("PN557ce644e5ab84fa21cc21112e22c485")->delete();
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@deleted = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers('PN557ce644e5ab84fa21cc21112e22c485')
  .delete

puts @deleted
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers('PN557ce644e5ab84fa21cc21112e22c485')
       .remove()
       .then(function(response) {
         console.log(response);
       }).catch(function(error) {
         console.log(error);
       });
{}
Remove a Phone Number from a Messaging Service

Since the number is still owned by my account, we can now assign it to any other Service.

Now you know all about creating and configuring your Messaging Service and sending messages that are enhanced by Copilot.

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:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.Service;

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

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

        Service service = Service.creator("My First Service").create();
    }
}
curl 'https://messaging.twilio.com/v1/Services' -X POST \
    --data-urlencode 'FriendlyName=My First Service' \
    --data-urlencode 'StatusCallback=http://requestb.in/1234abcd' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

service = client.messaging \
                .services \
                .create(friendly_name="My First Service")

print(service.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1;


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 = "your_auth_token";
      const string serviceFriendlyName = "My First Service";

      TwilioClient.Init(accountSid, authToken);

      var service = ServiceResource.Create(serviceFriendlyName);

      Console.WriteLine(service.Sid);
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$service = $twilio->messaging->v1->services->create("My First Service",
                                                    array('statusCallback' => "http://requestb.in/1234abcd")
                                                   );
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@service = @client.messaging.v1
  .services
  .create(
    friendly_name: 'My First Service',
    status_callback: 'http://requestb.in/1234abcd')

puts @service.sid
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);

const opts = {
  friendlyName: 'My First Service',
  statusCallback: 'http://requestb.in/1234abcd'
};

client.messaging.services.create(opts)
    .then(function(response) {
      console.log(response);
    }).catch(function(error) {
      console.log(error);
    });
{
     "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
     "sid": "MG2172dd2db502e20dd981ef0d67850e1a",
     "date_created": "2017-03-14T20:12:31Z",
     "date_updated": "2017-03-14T20:12:33Z",
     "friendly_name": "My First Service!",
     "inbound_request_url": null,
     "inbound_method": "POST",
     "fallback_url": null,
     "fallback_method": "POST",
     "status_callback": "http://requestb.in/1234abcd",
     "sticky_sender": true,
     "mms_converter": true,
     "links": {
     "phone_numbers": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers",
     "short_codes": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/ShortCodes",
     "alpha_senders": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/AlphaSenders"
                    },
     "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a"
}
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        String phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";
        PhoneNumber phoneNumber = PhoneNumber.creator(pathServiceSid, phoneNumberSid)
                                             .create();
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN557ce644e5ab84fa21cc21112e22c485' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_number = client.messaging \
                     .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                     .phone_numbers \
                     .create(phone_number_sid="PN557ce644e5ab84fa21cc21112e22c485")

print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
      const string phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";

      TwilioClient.Init(accountSid, authToken);

      var phoneNumber = PhoneNumberResource.Create(pathServiceSid, phoneNumberSid);

      Console.WriteLine(phoneNumber.Sid);

    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumber = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers->create("PN557ce644e5ab84fa21cc21112e22c485");
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_number = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers
  .create(phone_number_sid: 'PN557ce644e5ab84fa21cc21112e22c485')

puts @phone_number.sid
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers.create({phoneNumberSid: 'PN557ce644e5ab84fa21cc21112e22c485'})
    .then(function(response) {
      console.log(response);
    }).catch(function(error) {
      console.log(error);
    });
{
    "sid": "PN557ce644e5ab84fa21cc21112e22c485",
    "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
    "date_created": "2017-03-14T20:12:31Z",
    "date_updated": "2017-03-14T20:12:33Z",
    "phone_number": "+15017250604",
    "country_code": "US",
    "capabilities": ["sms", "mms"],
    "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485"
}
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

import java.util.Arrays;
import java.util.List;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        List<String> phoneNumberSids = Arrays
                .asList("PN2a0747eba6abf96b7e3c3ff0b4530f6e",
                        "PN557ce644e5ab84fa21cc21112e22c485",
                        "PN2a0747eba6abf96b7e3c3ff0b4530f6e");

        for (String phoneNumberSid : phoneNumberSids) {
            PhoneNumber phoneNumber = PhoneNumber
                    .creator(pathServiceSid, phoneNumberSid)
                    .create();

            System.out.println(phoneNumber);
        }
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN2a0747eba6abf96b7e3c3ff0b4530f6e' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
    
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN557ce644e5ab84fa21cc21112e22c485' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
    
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' -X POST \
    --data-urlencode 'PhoneNumberSid=PN2a0747eba6abf96b7e3c3ff0b4530f6e' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_numbers_sids = ["PN2a0747eba6abf96b7e3c3ff0b4530f6e",
                      "PN557ce644e5ab84fa21cc21112e22c485",
                      "PN2a0747eba6abf96b7e3c3ff0b4530f6e"]

for phone_numbers_sid in phone_numbers_sids:
    phone_number = client.messaging \
                         .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                         .phone_numbers \
                         .create(phone_number_sid=phone_numbers_sid)
    print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
    const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";

    var phoneNumberSids = new List<string> {
      "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
      "PN557ce644e5ab84fa21cc21112e22c485",
      "PN2a0747eba6abf96b7e3c3ff0b4530f6e"
    };

    TwilioClient.Init(accountSid, authToken);

    foreach(var phoneNumberSid in phoneNumberSids)
    {
        var phoneNumber = PhoneNumberResource.Create(pathServiceSid, phoneNumberSid);
        Console.WriteLine(phoneNumber.Sid);
    }
  }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = array("PN2a0747eba6abf96b7e3c3ff0b4530f6e",
    "PN557ce644e5ab84fa21cc21112e22c485",
    "PN2a0747eba6abf96b7e3c3ff0b4530f6e");

foreach ($phoneNumbers as $number) {
    $phoneNumber = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                           ->phoneNumbers->create($number);
}
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_number_sids = ['PN2a0747eba6abf96b7e3c3ff0b4530f6e',
                      'PN557ce644e5ab84fa21cc21112e22c485',
                      'PN2a0747eba6abf96b7e3c3ff0b4530f6e']

@phone_number_sids.each do |phone_number_sid|
  @phone_number = @client.messaging.v1
    .services('MG2172dd2db502e20dd981ef0d67850e1a')
    .phone_numbers
    .create(phone_number_sid: phone_number_sid)

  puts @phone_number.sid
end
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

const phoneNumbers = ['PN2a0747eba6abf96b7e3c3ff0b4530f6e',
                      'PN557ce644e5ab84fa21cc21112e22c485',
                      'PN2a0747eba6abf96b7e3c3ff0b4530f6e'];

phoneNumbers.forEach((phoneNumber) => {
  service.phoneNumbers.create({phoneNumberSid: phoneNumber})
      .then(function(response) {
        console.log(response);
      }).catch(function(error) {
        console.log(error);
      });
});
{
    "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
    "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
    "date_created": "2017-03-14T20:15:31Z",
    "date_updated": "2017-03-14T20:15:33Z",
    "phone_number": "+441632960556",
    "country_code": "GB",
    "capabilities": ["sms"],
    "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
}
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        ResourceSet<PhoneNumber> phoneNumbers = PhoneNumber.reader(pathServiceSid)
                                                           .read();

        for (PhoneNumber phoneNumber : phoneNumbers) {
            System.out.println(phoneNumber);
        }
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers' \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

phone_numbers = client.messaging \
                      .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                      .phone_numbers \
                      .list()

for phone_number in phone_numbers:
    print(phone_number.sid)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";

      TwilioClient.Init(accountSid, authToken);

      var phoneNumbers = PhoneNumberResource.Read(pathServiceSid);

      foreach (var phoneNumber in phoneNumbers)
      {
        Console.WriteLine(phoneNumber.Sid);
      }
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers->read();

foreach ($phoneNumbers as $phoneNumber) {
    print($phoneNumber->phoneNumber);
}
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@phone_numbers = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers.list

@phone_numbers.each do |phone_number|
  puts phone_number.phone_number
end
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers.list()
       .then(function(response) {
         console.log(response);
       }).catch(function(error) {
         console.log(error);
       });
{
    "meta": {
        "page": 0,
        "page_size": 50,
        "first_page_url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers?PageSize=50&Page=0",
        "previous_page_url": null,
        "next_page_url": null,
        "key": "phone_numbers",
        "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers?PageSize=50&Page=0"
                    },
    "phone_numbers": [
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN557ce644e5ab84fa21cc21112e22c485",
            "date_created": "2017-03-14T20:12:31Z",
            "date_updated": "2017-03-14T20:12:33Z",
            "phone_number": "+987654321",
            "country_code": "US",
            "capabilities": ["sms", "mms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485"
        },
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
            "date_created": "2017-03-14T20:15:31Z",
            "date_updated": "2017-03-14T20:15:33Z",
            "phone_number": "+441632960556",
            "country_code": "GB",
            "capabilities": ["sms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
        }
        {
            "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "service_sid": "MG2172dd2db502e20dd981ef0d67850e1a",
            "sid": "PN2a0747eba6abf96b7e3c3ff0b4530f6e",
            "date_created": "2017-03-14T20:15:31Z",
            "date_updated": "2017-03-14T20:15:33Z",
            "phone_number": "+611900654321",
            "country_code": "AU",
            "capabilities": ["sms"],
            "url": "https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e"
        }
    ]
}
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
// 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
// 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.")
// 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 twilio.com/docs/php/install
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.'
)
// 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 Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var authToken = "your_auth_token";
var client = require('twilio')(accountSid, authToken);

client.sendMessage({
    messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
    to: '+441632960675',
    body: 'Phantom Menace was clearly the best of the prequel trilogy.'
}, function(err, message) {
    console.log(message);
});
{
  "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>
SDK Version:
  • 5.x
Response Format:
  • json
SDK Version:
  • 7.x
SDK Version:
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 6.x
SDK Version:
  • 5.x
Format:
  • JSON
import com.twilio.Twilio;
import com.twilio.rest.messaging.v1.service.PhoneNumber;

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

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

        String pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
        String phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";
        PhoneNumber.deleter(pathServiceSid, phoneNumberSid).delete();
    }
}
curl 'https://messaging.twilio.com/v1/Services/MG2172dd2db502e20dd981ef0d67850e1a/PhoneNumbers/PN557ce644e5ab84fa21cc21112e22c485' -X DELETE \
    -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)

did_delete = client.messaging \
                   .services(sid="MG2172dd2db502e20dd981ef0d67850e1a") \
                   .phone_numbers(sid="PN557ce644e5ab84fa21cc21112e22c485") \
                   .delete()

print(did_delete)
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
using System;
using Twilio;
using Twilio.Rest.Messaging.V1.Service;


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 = "your_auth_token";
      const string pathServiceSid = "MG2172dd2db502e20dd981ef0d67850e1a";
      const string phoneNumberSid = "PN557ce644e5ab84fa21cc21112e22c485";

      TwilioClient.Init(accountSid, authToken);

      var deleted = PhoneNumberResource.Delete(pathServiceSid, phoneNumberSid);

      Console.WriteLine(deleted);
    }
}
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library

use Twilio\Rest\Client;

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio = new Client($account_sid, $auth_token);

$phoneNumbers = $twilio->messaging->v1->services("MG2172dd2db502e20dd981ef0d67850e1a")
                                      ->phoneNumbers("PN557ce644e5ab84fa21cc21112e22c485")->delete();
# Get twilio-ruby from twilio.com/docs/libraries/ruby
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@deleted = @client.messaging.v1
  .services('MG2172dd2db502e20dd981ef0d67850e1a')
  .phone_numbers('PN557ce644e5ab84fa21cc21112e22c485')
  .delete

puts @deleted
// Download the Node helper library from twilio.com/docs/libraries/node
// 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);
const service = client.messaging.services('MG2172dd2db502e20dd981ef0d67850e1a');

service.phoneNumbers('PN557ce644e5ab84fa21cc21112e22c485')
       .remove()
       .then(function(response) {
         console.log(response);
       }).catch(function(error) {
         console.log(error);
       });
{}