Skip to contentSkip to navigationSkip to topbar
On this page

Conversations API Quickstart


With Twilio's Conversations API, you can build virtual spaces ("conversations") for customers to communicate across multiple channels.

Instead of building separate solutions for online chat versus SMS engagement, you now have one API to create customer engagement across all of them.

This Quickstart will cover:

  • Signing up for Twilio and provisioning your first SMS-enabled phone number
  • Creating your first Conversation with the Conversations API
  • Connecting an SMS Participant to a Conversation
  • Configuring a demo Conversations application
  • Adding a chat Participant to a Conversation to talk with the SMS Participant

You will need:

(information)

Info

Twilio Conversations is built on top of several Twilio products. It may also be useful to pull up a document or sticky note to keep track of the various values that you'll need throughout this Quickstart.


Sign up for Twilio and provision your first SMS-enabled number

sign-up-for-twilio-and-provision-your-first-sms-enabled-number page anchor
(information)

Info

If you've already signed up for Twilio and have an SMS-enabled phone number, you can skip ahead to installing the Twilio CLI.

Before you create your first Conversation, you'll need to sign up for a Twilio account(link takes you to an external page) or sign into your existing account and purchase an SMS-capable phone number(link takes you to an external page).

You can sign up for a free Twilio trial account here(link takes you to an external page).

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked a series of questions to customize your experience.
  • Once you finish the onboarding flow, you'll arrive at your project dashboard in the Twilio Console(link takes you to an external page). This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number(link takes you to an external page) page, check the SMS box and click Search.

Buy a twilio phone number.

You'll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click Buy to add it to your account.

Select an SMS-enabled phone number.

Next, we'll install the Twilio CLI and log in to it.


First, install twilio-cli if you haven't done so already:

macOSWindowsLinux

The suggested way to install twilio-cli on macOS is to use Homebrew(link takes you to an external page). If you don't already have it installed, visit the Homebrew site(link takes you to an external page) for installation instructions and then return here.

Once you have installed Homebrew, run the following command to install twilio-cli:

brew tap twilio/brew && brew install twilio
(information)

Info

For other installation methods, see the Twilio CLI Quickstart.

Log in to twilio-cli

log-in-to-twilio-cli page anchor

To access your Twilio account, you must provide your Twilio credentials to twilio-cli. This can be done by running this command:

twilio login

You will be prompted for:

This will create an API Key for you that will be stored securely for future use.

Now that you have a Twilio account, the Twilio CLI, and a programmable phone number, you can start writing some code.


Create your first Conversation

create-your-first-conversation page anchor

It's time to make our first Conversation! In the sample code, replace the Account SID and Auth Token with the values from your Twilio Console:

Create your first ConversationLink to code sample: Create your first Conversation
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createConversation() {
11
const conversation = await client.conversations.v1.conversations.create({
12
friendlyName: "My First Conversation",
13
});
14
15
console.log(conversation.sid);
16
}
17
18
createConversation();

Output

1
{
2
"sid": "CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"chat_service_sid": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"friendly_name": "My First Conversation",
7
"unique_name": "unique_name",
8
"attributes": "{ \"topic\": \"feedback\" }",
9
"date_created": "2015-12-16T22:18:37Z",
10
"date_updated": "2015-12-16T22:18:38Z",
11
"state": "inactive",
12
"timers": {
13
"date_inactive": "2015-12-16T22:19:38Z",
14
"date_closed": "2015-12-16T22:28:38Z"
15
},
16
"bindings": {},
17
"url": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"links": {
19
"participants": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants",
20
"messages": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages",
21
"webhooks": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Webhooks"
22
}
23
}
(information)

Info

It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out How to Set Environment Variables(link takes you to an external page) for more information.

Copy down the Conversation SID (It starts with CHXXX). Now, let's use it to fetch that Conversation we just created.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function fetchConversation() {
11
const conversation = await client.conversations.v1
12
.conversations("CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
13
.fetch();
14
15
console.log(conversation.chatServiceSid);
16
}
17
18
fetchConversation();

Output

1
{
2
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"chat_service_sid": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"friendly_name": "My First Conversation",
7
"unique_name": "first_conversation",
8
"attributes": "{ \"topic\": \"feedback\" }",
9
"date_created": "2015-12-16T22:18:37Z",
10
"date_updated": "2015-12-16T22:18:38Z",
11
"state": "active",
12
"timers": {
13
"date_inactive": "2015-12-16T22:19:38Z",
14
"date_closed": "2015-12-16T22:28:38Z"
15
},
16
"bindings": {},
17
"url": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"links": {
19
"participants": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants",
20
"messages": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages",
21
"webhooks": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Webhooks"
22
}
23
}

Copy down the Conversation Service SID (It starts with ISXXX), and make sure you've copied the Conversation SID (It starts with CHXXX) as well. We'll be using these values in the next few steps when we add participants to the Conversation you just created.


Add an SMS participant to a Conversation

add-an-sms-participant-to-a-conversation page anchor

You've created a Conversation, which you can think of as a virtual space for users to join from the channel of their choice.

Next, you'll add your first Participant: someone connecting to the Conversation with an SMS-enabled phone number. (Hint: Use the number that you purchased above.)

For the following code sample, replace the placeholder values for:

  • CHXXX...: use the Conversation SID you just copied
  • <Your Personal Mobile Number>: your own mobile number, in E.164 format
  • <Your Purchased Twilio Phone Number>: the Twilio number you purchased in step 1, in E.164 format
  • TWILIO_ACCOUNT_SID: Your Twilio Account SID
  • TWILIO_AUTH_TOKEN: Your Twilio Auth Token
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createConversationParticipant() {
11
const participant = await client.conversations.v1
12
.conversations("CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
13
.participants.create({
14
"messagingBinding.address": "<Your Personal Mobile Number>",
15
"messagingBinding.proxyAddress": "<Your Purchased Twilio Phone Number>",
16
});
17
18
console.log(participant.sid);
19
}
20
21
createConversationParticipant();

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"conversation_sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
4
"sid": "MBaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"identity": null,
6
"attributes": "{ \"role\": \"driver\" }",
7
"messaging_binding": {
8
"type": "sms",
9
"address": "+15558675310",
10
"proxy_address": "+15017122661"
11
},
12
"role_sid": "RLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13
"date_created": "2015-12-16T22:18:37Z",
14
"date_updated": "2015-12-16T22:18:38Z",
15
"url": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/MBaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
16
"last_read_message_index": null,
17
"last_read_timestamp": null
18
}

Now you have one SMS Participant in the Conversation that you created!


Configure the Conversations demo application using CodeSandbox.io

configure-the-conversations-demo-application-using-codesandboxio page anchor

Great, you've got a Conversation with an SMS Participant. Just one problem: it's quiet because it's hard to have a conversation with just one person.

It's time to add a second chat Participant to talk with your SMS Participant (which we just created).

Sign into CodeSandbox and fork our demo application

sign-into-codesandbox-and-fork-our-demo-application page anchor

For this Quickstart, we'll be using a basic chat-interface application to join our Conversation. We need to take a quick detour to set up the sample Conversations application.

For your convenience, we've created a demo application that provides a basic JavaScript-based chat interface in which you can send and receive messages in your new Conversation.

Sign in to Codesandbox.io(link takes you to an external page) and fork the demo app(link takes you to an external page) into your own Sandbox. CodeSandbox is a cloud-based online editor that we can use to host, update, and edit our sample chat application.

Conversations Demo App on CodeSandbox.io with arrow pointing to 'Fork' button.

Now that you have forked the demo application, you have your own sandbox and online editor to adapt the code. Your changes will be instantly reflected in the deployed application.

Use twilio-cli to install the Twilio token plugin and generate your token

use-twilio-cli-to-install-the-twilio-token-plugin-and-generate-your-token page anchor

In order for your Conversations demo application to work, we need to authenticate our chat user by retrieving a short-lived token attached to your API Key. We'll use twilio-cli(link takes you to an external page) to generate a token that you can use in your application.

Run the following command to add the Twilio token plugin(link takes you to an external page) that handles token generation:

twilio plugins:install @twilio-labs/plugin-token

You can create a token with this command, replacing the arguments with your own values:

twilio token:chat --identity testPineapple --chat-service-sid ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --profile project-danger
  • For the value you pass to identity, use the username that your chat Participant will use to log into the chat demo application. Use any identity that you like. May we suggest testPineapple?
  • For your chat-service-sid, use the unique Conversation Service SID starting with ISXXX that you copied after creating your Conversation.
  • For the profile, enter what you used as the shorthand identifier for your profile when setting up the Twilio CLI. In this instance, we used project-danger.

Copy the token returned from the previous command and paste it into the ConversationsApp.js placeholder field in the getToken function.

conversations-paste-token.

Reload your Conversations demo application, which now includes a token for your chosen chat identity (i.e., the one you just attached to the token we created). Log in with that identity in the web interface.

conversations-login-reload-800.

Once you see "You are connected." You know that you have logged into the Conversations demo application:

conversations-demo-no-conversations-800.

Phew! Your Conversations demo application is all set up and ready to go. We're on to the last part: adding this chat Participant to your Conversation.


Add a chat participant to a Conversation

add-a-chat-participant-to-a-conversation page anchor

Let's add a chat participant to our Conversation so it isn't so lonely in there. The following code sample adds a chat Participant to the Conversation. You will need to replace the following information:

  • Conversation SID: the same CHXXX SID that you used previously
  • Identity: the identity that you just created in the Conversations demo application (For this example, we'll use testPineapple)
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createConversationParticipant() {
11
const participant = await client.conversations.v1
12
.conversations("CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
13
.participants.create({ identity: "testPineapple" });
14
15
console.log(participant.sid);
16
}
17
18
createConversationParticipant();

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"conversation_sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
4
"sid": "MBaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"identity": "testPineapple",
6
"attributes": "{ \"role\": \"driver\" }",
7
"messaging_binding": {
8
"type": "sms",
9
"address": "+15558675310",
10
"proxy_address": "+15017122661"
11
},
12
"role_sid": "RLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13
"date_created": "2015-12-16T22:18:37Z",
14
"date_updated": "2015-12-16T22:18:38Z",
15
"url": "https://conversations.twilio.com/v1/Conversations/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/MBaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
16
"last_read_message_index": null,
17
"last_read_timestamp": null
18
}

After you run this code, you should see a link with your Conversation's "friendly name" pop up in the Conversations Demo Application. That means you're connected and can start chatting!

conversations-demo-convo-added-800.

Talk amongst "yourself"

talk-amongst-yourself page anchor

New Conversation? Check. Working demo application? Check. Two Participants? Check.

It's time to start talking. On your mobile phone, send a text message to the Twilio number you used to set up your Conversation. Your Conversations demo application should receive the same message almost immediately!

conversations-demo-full-conversation-800.

If you reply in the demo application browser, you'll receive the message as a text on your phone. Notice how all of this routing between the two channels (SMS and chat) is done automatically on your behalf. Three REST requests in, and you have a working use-case, congratulations!


From here, you can add more participants to your Conversation via chat, SMS, or even WhatsApp. New participants start receiving new messages automatically, and deleting those same participants removes them from the Conversation.

Subject to regional SMS limitations, you can have any number of SMS participants ("user address") for each Twilio number ("proxy address"), or you can have a separate proxy address for each of your users. However, a mobile phone number can be in only one conversation with any given Twilio number—you cannot have multiple conversations containing the same Twilio number and mobile phone number pair.

Want to know more about the Twilio Conversations API?

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.