Skip to contentSkip to navigationSkip to topbar
Rate this page:
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:


_10
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:


_10
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 Conversation

create-your-first-conversation-1 page anchor

Create a Conversation with a FriendlyName using the REST API

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.conversations.v1.conversations
_10
.create({friendlyName: 'My First Conversation'})
_10
.then(conversation => console.log(conversation.sid));

Output

_25
{
_25
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"friendly_name": "My First Conversation",
_25
"unique_name": "unique_name",
_25
"attributes": {
_25
"topic": "feedback"
_25
},
_25
"date_created": "2015-12-16T22:18:37Z",
_25
"date_updated": "2015-12-16T22:18:38Z",
_25
"state": "inactive",
_25
"timers": {
_25
"date_inactive": "2015-12-16T22:19:38Z",
_25
"date_closed": "2015-12-16T22:28:38Z"
_25
},
_25
"bindings": {},
_25
"url": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"links": {
_25
"participants": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants",
_25
"messages": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages",
_25
"webhooks": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Webhooks"
_25
}
_25
}

(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.

Fetch your new Conversation

fetch-your-new-conversation page anchor

Retrieve your new Conversation to find the Chat Service SID

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.conversations.v1.conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_10
.fetch()
_10
.then(conversation => console.log(conversation.chatServiceSid));

Output

_25
{
_25
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"friendly_name": "My First Conversation",
_25
"unique_name": "first_conversation",
_25
"attributes": {
_25
"topic": "feedback"
_25
},
_25
"date_created": "2015-12-16T22:18:37Z",
_25
"date_updated": "2015-12-16T22:18:38Z",
_25
"state": "active",
_25
"timers": {
_25
"date_inactive": "2015-12-16T22:19:38Z",
_25
"date_closed": "2015-12-16T22:28:38Z"
_25
},
_25
"bindings": {},
_25
"url": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_25
"links": {
_25
"participants": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants",
_25
"messages": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages",
_25
"webhooks": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Webhooks"
_25
}
_25
}

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

Add a Conversation Participant (SMS)

add-a-conversation-participant-sms page anchor

Create an SMS-based Conversation Participant with the REST API

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = require('twilio')(accountSid, authToken);
_14
_14
client.conversations.v1.conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14
.participants
_14
.create({
_14
'messagingBinding.address': '<Your Personal Mobile Number>',
_14
'messagingBinding.proxyAddress': '<Your Purchased Twilio Phone Number>'
_14
})
_14
.then(participant => console.log(participant.sid));

Output

_20
{
_20
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"conversation_sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"sid": "MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"identity": null,
_20
"attributes": {
_20
"role": "driver"
_20
},
_20
"messaging_binding": {
_20
"type": "sms",
_20
"address": "+15558675310",
_20
"proxy_address": "+15017122661"
_20
},
_20
"role_sid": "RLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"date_created": "2015-12-16T22:18:37Z",
_20
"date_updated": "2015-12-16T22:18:38Z",
_20
"url": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"last_read_message_index": null,
_20
"last_read_timestamp": null
_20
}

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:


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

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


_10
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 )

Add a Conversation Participant (Chat)

add-a-conversation-participant-chat page anchor

Create a chat-based Conversation Participant with the REST API

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.conversations.v1.conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.participants
_11
.create({identity: 'testPineapple'})
_11
.then(participant => console.log(participant.sid));

Output

_20
{
_20
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"conversation_sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"sid": "MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"identity": "testPineapple",
_20
"attributes": {
_20
"role": "driver"
_20
},
_20
"messaging_binding": {
_20
"type": "sms",
_20
"address": "+15558675310",
_20
"proxy_address": "+15017122661"
_20
},
_20
"role_sid": "RLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"date_created": "2015-12-16T22:18:37Z",
_20
"date_updated": "2015-12-16T22:18:38Z",
_20
"url": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_20
"last_read_message_index": null,
_20
"last_read_timestamp": null
_20
}

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?


Rate this page: