Rate this page:

Exploring the Conversations Android Quickstart

What does the Conversations Android Quickstart do? How does it work? How would you add something similar to your own project? We'll cover all of these questions and more in this behind the scenes look at the example application code.

If you haven't had a chance to try out the Conversations Android Quickstart app yet, follow the instructions in the README to get it up and running.

You'll also need to supply an access token with a Chat grant for a Conversations Service in the strings.xml resource file before running the application. You can generate an access token with the Twilio Command Line Interface (CLI):

twilio token:chat --identity <The test username> --chat-service-sid <ISXXX...>

One of the easiest ways to install the CLI on macOS is to use Homebrew. If you don't already have it installed, visit the Homebrew site for installation instructions and then return here.

Once Homebrew is installed, simply run the following command to install the CLI:

brew tap twilio/brew && brew install twilio


If you already installed the CLI with brew and want to upgrade to the latest version, run:

brew upgrade twilio

Warning for Node.js developers

If you have installed Node.js version 10.12 or higher on your Mac, you can avoid potential Node.js version conflicts by installing the CLI using npm:

npm install twilio-cli -g

Before You Begin

Before we can install, we need to make sure you have Node.js installed (version 12.21 or above). To see if you have node installed, try running this command:

node -v

If your system reports v12.21 or above, you can skip the next step.

Installing Node.js on Windows

Using the Windows Installer (.msi) is the recommended way to install Node.js on Windows. You can download the installer from the Node.js download page.

Run PowerShell as an administrator, and type the following command:

Set-ExecutionPolicy Bypass -Scope Process

You should see the following output:

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/ Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

Enter Y to confirm the change.

Note you will have to set this execution policy every time you run PowerShell. See notes on Set-ExecutionPolicy to learn more.

Tested Windows Versions

These instructions have been tested on the following Windows versions:

  • Windows 10 Pro
  • Windows Server 2016

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.


If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Before we can install, we need to make sure you have Node.js installed (version 10.12 or above). Even if you already installed Node yourself, the CLI works best when you install it using nvm. Here's how to get nvm installed on most Linux systems:

curl -o- | bash

Please visit the nvm installation instructions for additional options and troubleshooting steps. Once you have nvm installed, run the following to install and use the most recent LTS release of Node.js:

nvm install --lts
nvm use <insert version reported from above>

Installing other Twilio CLI prerequisites for Linux

Depending on your distribution, you will need to run one of the following commands:

  • Debian/Ubuntu: sudo apt-get install libsecret-1-dev
  • Red Hat-based: sudo yum install libsecret-devel
  • Arch Linux: sudo pacman -S libsecret

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.


If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Quickstart Overview

The example application code uses Java as the programming language. We also only have one activity class, named MainActivity. We chose not to use fragments in this quickstart for simplicity, but you can certainly use them with Conversations.

We built a class named QuickstartConversationsManager to handle the interactions with the Conversations SDK.

Within the quickstart application, you will find examples of the following:

When you build an application that uses Conversations, you may be able to use the QuickstartConversationsManager and MainActivity classes as a start for your project. You may also just want to take a look at how the quickstart works, and then build your own solution with the classes in the SDK!

Adding Twilio Conversations to your Application

When you build your solutions with Twilio Conversations, you need a Conversations Android SDK for your mobile app. You can install this library using Gradle.

Conversations SDK

You would typically start by adding the ConversationsClient from the com.twilio.conversations package to your project, and then work with Conversation objects to send and retrieve Message objects for a given conversation. Other important classes are User, Participant, and Media.

While we cover some of the basics of the Conversations SDK in this Quickstart, you can also find reference Javadocs for each class and interface. We also consider some of these topics in more detail in other pages in our docs, which we will link to in each section that has a corresponding guide.

Twilio Helper Library

The Conversations SDK for Android is only one half of the solution. You'll also need to build a server to support your mobile application. Twilio supports six different languages and platforms for you to build with. Java might be the best choice if you are an Android developer looking to try out web application development, but you can use any of these to build your server.

For your chosen language and/or platform, pick the appropriate Twilio Helper Library:

On each of these pages, you will find instructions for setting up the Twilio helper library (also called a "server-side SDK"). We recommend using dependency management for the Twilio libraries, and you'll find directions for the most common build tools for your platform.

If you don't already have a Twilio account, sign up for a Twilio trial account, and then create a new project. You'll also need to create an API Key and API Secret pair to call Twilio's REST API, whether you use one of the Twilio helper libraries, or make the API calls yourself.

Understanding Identity, Access Tokens, and Chat Grants

Each chat user in your Conversations project needs an identity - this could be their user id, their username, or some kind of other identifier. You could certainly have anonymous users in your Conversations - for instance, a web chat popup with a customer service agent on an e-commerce web site - but in that case, you would still want to issue some kind of identifier from your application.

Once you build Twilio Conversations into your project, you should generate an access token with a ChatGrant for end users, along with the identity value.

With the Conversations Android Quickstart, the easiest way to get started is to create an access token from the Twilio Command Line Interface (CLI).

Difference between Access Tokens, Auth Tokens and API Keys

As part of this project, you will see that there are three different ways of providing credentials for Twilio - access tokens, auth tokens, and API keys. What is the difference between all of these different styles?

Access Tokens

Access tokens provide short-lived credentials for a single end user to work with your Twilio service from a Javascript application running in a web browser, or from a native iOS or Android mobile application. Use the Twilio helper libraries in your back end web services to create access tokens for your front end applications to consume. Alternatively, use the Twilio CLI to create access tokens for testing. These access tokens have a built-in expiration, and need to be refreshed from your server if your users have long-running connections. The Conversations client will update your application when access tokens are about to expire, or if they have expired, so that you can refresh the token.

Auth Tokens

Although the names are similar, authentication (or auth) tokens are not the same as access tokens, and cannot be used in the same way. The auth token pairs with your Twilio account identifier (also called the account SID) to provide authentication for the Twilio REST API. Your auth token should be treated with the same care that you would use to secure your Twilio password, and should never be included directly in source code, made available to a client application, or checked into a file in source control.

API Keys and Secrets

Similar to auth tokens, API key/secret pairs secure access to the Twilio REST API for your account. When you create an API key and secret pair from the Twilio console, the secret will only be shown once, and then it won't be recoverable. In your back end application, you would authenticate to Twilio with a combination of your account identifier (also known as the "Account SID"), an API key, and an API secret.

The advantage of API keys over auth tokens is that it is easy to rotate API keys on your server application, especially if you use one API key and secret pair for each application cluster or instance. This way, you can have multiple credentials under your Twilio account, and if you need to swap out a key pair and then deactivate it, you can do it on an application basis, not on an account basis.

Storing Credentials Securely

Whether you use auth tokens or API keys, we suggest that you store those credentials securely, and do not check them into source control. There are many different options for managing secure credentials that depend on how and where you run your development, staging, and production environments.

When you develop locally, look into using a .env file with your project, usually in conjunction with a library named dotenv. For .NET Core, read our article on Setting Twilio Environment Variables in Windows 10 with PowerShell and .NET Core 3.0 to learn a lot more about this topic!

Retrieving a Conversations Access Token

For the Conversations Quickstart, you can simply generate an access token using the Twilio Command Line Interface (CLI), and then paste that into the strings.xml file. While this works for getting the quickstart up and running, you will want to replace this with your own function that retrieves an access token.

You can use OkHttp, Volley or another HTTP library to make an authenticated HTTP request to your server, where the server code would provide an access token with a ChatGrant that sets the identity for the user based on your own authentication mechanism (such as an API key, or your own token).

Ideally, this method would be usable for three different scenarios:

  1. Initializing the Conversations Client when your application loads
  2. Refreshing the access token when the Conversations Client notifies your application that the token is about to expire
  3. Refreshing the access token when the Conversations Client notifies your application that the token did expire

Initializing the Conversations Client

The first step is to get an access token. Once you have an access token (a string value), you can initialize a Twilio Conversations Client. This client is the central class in the Conversations SDK, and you need to keep it around after initialization. The client is designed to be long-lived, and it will fire events off that your project can subscribe to.

You'll need to create your own listener for the Conversations Client that implements the ConversationsClientListener interface. In the quick start, we created a class named QuickstartConversationsManager to encapsulate our usage of the Conversations SDK.


        Client Synchronization State

        After you initialize the Conversations client, the client needs to synchronize with the server. The onConversationSynchronizationChange method on each listener gets called when the synchronization status changes - the completed status is COMPLETED, which means that the Conversations, Participants and Messages collections are ready to use.


              Joining a Conversation

              The Conversation class is the building block of your Conversations application. In the Quickstart, we've set things up so that the user automatically joins one conversation. For instance, this conversation's unique id could be supplied by a back end service to represent a three way conversation between a restaurant, a customer, and a delivery driver.

              Your user may have already joined the conversation, so you should check to see if they have before calling the join() method on the Conversation object.


                    Sending Messages to a Conversation

                    To send a message (with text content) to a conversation that a user has joined, you need to call the sendMessage() method on a Conversation object. To create a message, you can build one up with the Message.Options class.


                          Receiving and Displaying Messages

                          Each Conversation object from the Conversations SDK represents an individual conversation between one or more users. Inside the Conversations Quickstart, we interact with the Conversation in the QuickstartConversationManager class. We use this approach to avoid having an activity or fragment class that does too much. After initializing the Conversations SDK with an access token, waiting for the client to synchronize, and then either creating or joining a conversation, we can start to engage with that conversation by sending or receiving messages. These messages are Message objects from the Conversations SDK.

                          Displaying Existing Messages

                          We retrieve the last messages using the getLastMessages() method on the Conversation class. This returns all of the previous messages (up to a limit, which you can set in code), and you can use that to initialize the display for your class. After loading in any existing messages, the QuickstartConversationsManager notifies its listener (the MainActivity) that there is a new batch of messages to display.


                                Receiving New Messages

                                The QuickstartConversationsManager class implements the ConverstationListener interface. As events occur with our conversation, our manager object will get notified. One of these events is onMessageAdded. This event gets fired from the Twilio Conversations SDK when any user sends a message to the conversation.

                                Our manager appends that message to the messages we already have, and then notifies its delegate that a new message has arrived, and that the view controller should refresh its view of the messages.

                                In the main activity, we simply tell the recycler view that contains the messages to reload its data.

                                      Receiving New Messages

                                      Conclusion/Next Steps

                                      Now that you've seen how the Conversations Android Quickstart implements several key pieces of functionality, you can see how to add the Conversations SDK to your Java or Kotlin Android project. You can re-use the Quickstart Conversations Manager class within your own project, or extend it to fit your needs.

                                      For more information, check out these helpful links:

                                      Rate this page:

                                      Need some help?

                                      We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.


                                            Thank you for your feedback!

                                            We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                            Sending your feedback...
                                            🎉 Thank you for your feedback!
                                            Something went wrong. Please try again.

                                            Thanks for your feedback!

                                            Refer us and get $10 in 3 simple steps!

                                            Step 1

                                            Get link

                                            Get a free personal referral link here

                                            Step 2

                                            Give $10

                                            Your user signs up and upgrade using link

                                            Step 3

                                            Get $10

                                            1,250 free SMSes
                                            OR 1,000 free voice mins
                                            OR 12,000 chats
                                            OR more