All Articles

Twilio Conversations - Adding a Twilio Phone Number Participant

Published by Tony Vu on Jun 15, 2021

This article is part of the Twilio Conversations API series

In this article, you will learn how to add a Twilio phone number to a Conversation. This will let you programmatically send a text message from a Twilio phone number to other participants in a Conversation.

You will need to have a Conversation setup to complete this tutorial. If you don’t have one setup, check out the first article in this series on creating a Conversation

Assuming you have a Conversation at this point, you can add a Twilio phone number participant using the following code. You will need to plug in the following

  • Your Twilio Account SID
  • Your Twilio Auth token
  • A Twilio phone number
  • A Conversation SID for the Conversation you would like to add this participant to. This is simply an ID that is assigned to a Conversation by Twilio that uniquely identifies that resource.
const accountSid = "ACxxxxxxxxxxxxxxxxxx";
const authToken = "xxxxxxxxxxxxxxxxxx";

const client = require("twilio")(accountSid, authToken);

(async () => {
  const twilio_phone_number = "+1XXXXXXXXXX";
  let conversation_sid = "CHxxxxxxxxxxxxxxxxxx";
  const response = await client.conversations
    .conversations(conversation_sid)
    .participants.create({
      "messagingBinding.projectedAddress": twilio_phone_number,
    });
  console.log(response);
})();

The messagingBinding.projectedAddress parameter is specifically used for a Twilio phone number. This enables you to use the Twilio phone number to send a text message to other participants in the Conversation. Unlike adding a SMS participant to a conversation, you would not use the messagingBinding.address parameter. The messagingBinding.address parameter is only used for mobile phone numbers and not Twilio phone numbers.

node add_participant.js

If the participant was successfully added to the Conversation, you should see a JSON object returned and printed out in the terminal that represents the newly added participant.

{
  accountSid: 'ACxxxxxxxxxxxxxxxxxxxxxxx',
  conversationSid: 'CHxxxxxxxxxxxxxxxxxxxxxxx',
  sid: 'MBxxxxxxxxxxxxxxxxxxxxxxx',
  identity: null,
  attributes: '{}',
  messagingBinding: { projected_address: '+1XXXXXXXXXX', type: 'sms' },
  roleSid: 'Rxxxxxxxxxxxxxxxxxxxxxxx',
  dateCreated: 2021-06-10T15:36:41.000Z,
  dateUpdated: 2021-06-10T15:36:41.000Z,
  url: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxxxxxxx/Participants/MBxxxxxxxxxxxxxxxxxxxxxxx',
  lastReadMessageIndex: null,
  lastReadTimestamp: null
}

In the returned JSON response, the projected)address key of the messagingBinding object indicates the phone number you used for this participant. The type key of this object is automatically set to sms since it’s a phone number.

Interested in learning more about Twilio Conversations? Check out my upcoming eBook on Twilio Conversations and other articles.