Skip to contentSkip to navigationSkip to topbar
On this page

Trying Out WhatsApp with Conversations


In this tutorial, we'll explore how you can use Twilio's Conversations with Twilio's Sandbox for WhatsApp. Twilio controls a dedicated WhatsApp phone number that you can use to test WhatsApp with Conversations.

You will need a few things before you start this tutorial:

Note: We're using the Twilio CLI to make API requests to Twilio's Conversations service, but you can choose another method such as a helper library.

Let's get started!


Step 1: Opt-in to the Twilio Sandbox for WhatsApp

step-1-opt-in-to-the-twilio-sandbox-for-whatsapp page anchor

You can test your application in a developer environment by connecting to Twilio Sandbox for WhatsApp.

Navigate to the Conversations > Try it out(link takes you to an external page) section in the Twilio Console. Choose a use case - then, send the "join <your Sandbox keyword>" WhatsApp message from your device to the Twilio Sandbox for a WhatsApp phone number to connect to your sandbox.

Check your join phrase.

Once you've joined Twilio WhatsApp Sandbox, you'll receive a confirmation message. To disconnect from the sandbox, you can reply to the message from WhatsApp with the word "stop".

Optional: Remove the Inbound URL in the WhatsApp sandbox settings in the Twilio Console

optional-remove-the-inbound-url-in-the-whatsapp-sandbox-settings-in-the-twilio-console page anchor

In this tutorial, we won't need to set a webhook URL for inbound messaging. Let's remove the existing testing webhook URL configured in the WhatsApp Sandbox's settings section. Not doing so would result in an automatic reply requesting to update the configuration for your WhatsApp Sandbox's Inbound URL when you send your first message.

To update the WhatsApp Sandbox inbound URL, go to the Messaging > Settings > WhatsApp sandbox settings(link takes you to an external page) section in the Twilio Console and remove the webhook URL. Click Save.


Step 2: Create a Conversation

step-2-create-a-conversation page anchor

Now that you have your Twilio Sandbox for WhatsApp configured, it's time to create your first Conversation!

Let's make a Conversation using the Twilio CLI (but remember that you can choose another tool for making the API requests):

1
# Install the twilio-cli from https://twil.io/cli
2
3
twilio api:conversations:v1:conversations:create \
4
--friendly-name "sandbox-test"

Copy down the Conversation SID - it starts with CHXXXXXXX. You'll use this value in the next steps.


Step 3: Add a WhatsApp Participant to the Conversation

step-3-add-a-whatsapp-participant-to-the-conversation page anchor

You've created a Conversation, which you can think of as a virtual space that users can join using a channel of their choice.

Next, you'll add yourself as a WhatsApp Participant. The following code sample does this for you. You'll need to replace the following information:

  • CHXXXXXXXXXXX - the Conversation SID.
  • YOUR_WHATSAPP_NUMBER - your own mobile phone number in E.164 format .
  • TWI_SANDBOX_WA_NUMBER - the Twilio Sandbox WhatsApp phone number in E.164 format .
1
# Install the twilio-cli from https://twil.io/cli
2
3
twilio api:conversations:v1:conversations:participants:create \
4
--conversation-sid CHXXXXXXX \
5
--messaging-binding.address whatsapp:YOUR_WHATSAPP_NUMBER \
6
--messaging-binding.proxy-address whatsapp:TWI_SANDBOX_WA_NUMBER

Step 4: Add a chat Participant to the Conversation

step-4-add-a-chat-participant-to-the-conversation page anchor

For this step, you'll add a chat Participant to the Conversation (remember that you can also add an SMS Participant).

The following code sample does this for you. You'll need to replace the following information:

  • CHXXXXXXXXXX - the Conversation SID.
  • <Chat_User_Identity> - the identity of your chat user. In this tutorial, we'll use "chat-user".
1
# Install the twilio-cli from https://twil.io/cli
2
3
twilio api:conversations:v1:conversations:participants:create \
4
--conversation-sid CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
5
--identity "<Chat_User_Identity>"

New Conversation? Check. Two Participants? Check.

Now it's time to start talking!


Step 5: Send a Message from WhatsApp to the Sandbox number

step-5-send-a-message-from-whatsapp-to-the-sandbox-number page anchor

Let's start a Conversation by sending a WhatsApp message to the Twilio sandbox phone number.

WhatsApp-Participant-first-message.

Step 6: Reply to the message

step-6-reply-to-the-message page anchor

Let's reply to the previous message from the REST API. Remember that you can do it from the SDK or SMS (if that's how you added the second Participant).

Make sure to replace CHXXXXXXX for your Conversation SID, the <Chat_User_Identity> with "chat-user" and add the content of the message that you'd like to send. For example, we'll use "Hello from the other side 👋🏼".

The following code sample does this for you:

1
# Install the twilio-cli from https://twil.io/cli
2
3
twilio api:conversations:v1:conversations:messages:create \
4
--conversation-sid CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
5
--author "<Chat_User_Identity>" \
6
--body "Hello from the other side 👋🏼"
7
Chat-Participant-reply.

Well done! You've successfully connected your Twilio's WhatsApp Sandbox Number with Conversations.


Step 7: Twilio Conversations Logs

step-7-twilio-conversations-logs page anchor

To check if the WhatsApp message arrived in the Conversation, navigate to Monitor > Logs > Errors > Conversations(link takes you to an external page) and click the Conversation you created previously. You'll see on the top of the page the name of the Conversation you created. Click the Messages tab to see, for example, the WhatsApp phone number and chat identity that was used in the Conversation and the content of the message.

Conversations-logs.

Congratulations! 🎉 You have learned how to connect the Twilio's Sandbox for WhatsApp with Conversations. As a following step, you can:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.