Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this pageProducts used

Programmable Messaging for WhatsApp and Java Quickstart


(warning)

Warning

Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. Learn more about the new Console(link takes you to an external page).

With just a few lines of code, your application can send and receive messages with WhatsApp using the Twilio API for WhatsApp.

This WhatsApp Quickstart will teach you how to do this using the Twilio Sandbox for WhatsApp, Java, the Twilio Java Twilio helper library, and the Spark web framework(link takes you to an external page). In this Quickstart, you will learn how to:

  1. Sign up for Twilio and activate the Sandbox.
  2. Set up your development environment to send and receive messages
  3. Opt-in to the Sandbox
  4. Send your first WhatsApp message
  5. Receive inbound WhatsApp messages
  6. Reply to incoming WhatsApp messages

Prefer to watch a video? The video below shows you how to use the Sandbox to send and receive WhatsApp messages.


Sign up for Twilio and activate the Sandbox

sign-up-for-twilio-and-activate-the-sandbox page anchor

Before you can send a WhatsApp message from your web language, you'll need to sign up for a Twilio account(link takes you to an external page) or sign into your existing account and activate the Twilio Sandbox for WhatsApp(link takes you to an external page). It allows you to prototype with WhatsApp immediately using a shared phone number, without waiting for a dedicated number to be approved by WhatsApp.

To get started, select a number from the available sandbox numbers to activate your sandbox.

WA_Sandbox.join <your sandbox keyword>" to your Sandbox number in WhatsApp to join your Sandbox, and we'll reply with a confirmation that you've joined. Your sandbox keyword can be found in the console.

Once you join, you will only receive messages from your specific Sandbox. To disconnect from the sandbox, you can reply to the message from WhatsApp with sandbox stop, or switch to a different sandbox by messaging join <other sandbox keyword>.

Screen Shot 2018-08-01 at 8.31.05 AM.
Java

_23
// Install the Java helper library from twilio.com/docs/java/install
_23
_23
import com.twilio.Twilio;
_23
import com.twilio.rest.api.v2010.account.Message;
_23
import com.twilio.type.PhoneNumber;
_23
_23
public class Example {
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_23
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_23
_23
public static void main(String[] args) {
_23
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_23
Message message = Message.creator(
_23
new com.twilio.type.PhoneNumber("whatsapp:+15005550006"),
_23
new com.twilio.type.PhoneNumber("whatsapp:+14155238886"),
_23
"Hello there!")
_23
.create();
_23
_23
System.out.println(message.getSid());
_23
}
_23
}

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "Hello there!",
_24
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "whatsapp:+14155238886",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "whatsapp:+15005550006",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}


Receive and reply to messages from WhatsApp

receive-and-reply-to-messages-from-whatsapp page anchor

When someone replies to one of your messages, you will receive a webhook request from Twilio.

You can configure webhooks by connecting your Sandbox to an app you've already built for handling incoming messages, or build a new one for WhatsApp messages.

Configure WhatsApp Sandbox Webhook.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.