This article is part of the Twilio Conversations API series
In this article, you will learn how to add a SMS participant to a Conversation to start sending a message. You will need to have a Conversation setup to complete this tutorial. If you don’t have one setup, check out the previous article in this series on creating a Conversation
Assuming you have a Conversation at this point, you can add a SMS participant using the following code. You will need to plug in the following
- Your Twilio Account SID
- Your Twilio Auth token
- A mobile number that you would like to exchange messages with
- 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 phone = "+1XXXXXXXXXX";
let conversation_sid = "CHxxxxxxxxxxxxxxxxxx";
const response = await client.conversations
.conversations(conversation_sid)
.participants.create({
"messagingBinding.address": phone,
});
console.log(response);
})();The messagingBinding.address parameter used indicates to Twilio that you want to associate this participant with this phone number. Run the above code in your terminal
node add_participant.jsIf 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: 'AC',
conversationSid: 'CHxxxxxxxxxxxxxxxxxxx,
sid: 'MBxxxxxxxxxxxxxxxxxx',
identity: null,
attributes: '{}',
messagingBinding: { type: 'sms', address: '+1XXXXXXXXXX' },
roleSid: 'RLxxxxxxxxxxxxxxxxxx',
dateCreated: 2021-06-09T13:18:06.000Z,
dateUpdated: 2021-06-09T13:18:06.000Z,
url: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxx/Participants/MBxxxxxxxxxxxxxxxxxx',
lastReadMessageIndex: null,
lastReadTimestamp: null
}In the returned JSON response, the 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 Twilio knows this phone number is not a Twilio number.
As a last step, you can navigate to the Conversations panel in your Twilio Console and you should see one participant displayed for the Conversation associated with the SID you just used.
Interested in learning more about Twilio Conversations? Check out my upcoming eBook on Twilio Conversations and other articles.