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:
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.
If you’ve already signed up for Twilio and have an SMS-enabled phone number, you can skip ahead to creating your first Conversation.
You can sign up for a free Twilio trial account here.
- 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. 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 page, check the "SMS" box and click "Search."
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.
Now that you have a Twilio account and a programmable phone number, you can start writing some code.
It's time to make our first Conversation:
Copy down the Conversation SID (It starts with “CHXXX...”). Now, let's use it to fetch that Conversation we just created.
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.
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 up above.)
For the following code sample, replace the placeholder values for:
Now you have one SMS Participant in the Conversation that you created!
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).
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.
- You will need a CodeSandbox account, which uses your Github credentials.
- Go to our
conversations-demoapplication Sandbox and fork it to your own account with the "Fork" button in the upper right corner
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.
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 to generate a token that you can use in your application.
twilio-cli if you haven't done so already:
Once you have installed Homebrew, run the following command to install
brew tap twilio/brew && brew install twilio
The suggested way to install
twilio-cli is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.
Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.
- Add the
scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
- Install the app:
scoop install twilio
twilio-cli can be installed using the Advanced Package Tool (
apt) on most distributions such as Debian, Ubuntu, and Mint.
To do so, run the following commands in your terminal:
wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \ | sudo apt-key add - sudo touch /etc/apt/sources.list.d/twilio.list echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \ | sudo tee /etc/apt/sources.list.d/twilio.list sudo apt update sudo apt install -y twilio
For other installation methods, see the Twilio CLI Quickstart.
To access your Twilio account, you must provide your Twilio credentials to
twilio-cli. This can be done by running this command:
You will be prompted for
- a "Shorthand identifier for your Twilio profile": This can be anything, e.g,
- your Account SID and Auth Token, both of which you can find on the dashboard of your Twilio console.
This will create an API Key for you that will be stored securely for future use.
Next, run the following command to add the twilio token plugin 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"?
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 above. In this instance, we used
Copy the token that is returned from your request and paste it into the
ConversationsApp.js placeholder field in the
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.
Once you see "You are connected." You know that you have logged into the Conversations demo application:
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.
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")
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!
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!
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.
Want to know more about the Twilio Conversations API?
- Check out the Conversations API Reference docs
- Add a WhatsApp participant to a Conversation
- Learn about Webhooks in Conversations
- Create group texting experiences with Conversations