Skip to contentSkip to navigationSkip to topbar
On this pageProducts used

Programmable Messaging for WhatsApp curl Quickstart


With just a few lines of code, your application can send and receive messages with WhatsApp using the Twilio API for WhatsApp and curl(link takes you to an external page).

This WhatsApp Quickstart will teach you how to do this using the Twilio Sandbox for WhatsApp, curl, the Twilio API for WhatsApp, and a web framework of your choice.

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.

Be sure to take note of the phone number you choose in the Sandbox. You will need this later when we're ready to send some messages.


Gather your Twilio account information

gather-your-twilio-account-information page anchor

Before you can send any messages, you'll need to gather your Twilio account credentials. You can find these in the Twilio Console(link takes you to an external page).

  • Account SID - Used to authenticate REST API requests
  • Auth Token - Used to authenticate REST API requests
Account Credentials.

For all of our code snippets and curl examples, you will need to authenticate with the Account SID and Auth Token.


Set up your "development environment"

set-up-your-development-environment page anchor

curl(link takes you to an external page) should work by default in a recent Linux or macOS terminal. If you are using Windows, you can find curl packages already compiled here(link takes you to an external page).


Send a message with WhatsApp and curl

send-a-message-with-whatsapp-and-curl page anchor
(warning)

Opt-in required for WhatsApp Sandbox numbers

The WhatsApp Sandbox uses a single WhatsApp Sender shared by all Twilio users. To begin using the WhatsApp Sandbox, you must send a join message from the number you wish to send outbound messages. Each number may only receive messages from one WhatsApp Sandbox at a time.

When registering your own WhatsApp Sender, these limits do not apply. Learn how to register your own WhatsApp Senders.

Sandbox Opt-in Message

sandbox-opt-in-message page anchor

Send "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.

Inviting other users to your sandbox (OPTIONAL)

inviting-other-users-to-your-sandbox-optional page anchor

To invite someone else to your sandbox, create a link with the following format containing the opt-in message and send it to them: whatsapp://send?phone=<Your Sandbox Number>&text=<your URL-encoded sandbox keyword>

You can also create a QR code(link takes you to an external page) with the link format above that users can scan on their phone to opt-in to your sandbox.

whatsapp-sandbox-opt-in-qr.

To send a message, use the following code and replace the To parameter with the phone number for your personal WhatsApp account in the E.164 format. (If you haven't already, install WhatsApp on your device and register for an account.) For the From parameter, be sure to include the whatsapp: channel identifier before the Sandbox number in E.164 format.

For the From parameter, you need your Sender ID which should be shown on the installed channel in the Twilio Console, as shown previously.

Send a message with WhatsApp and curlLink to code sample: Send a message with WhatsApp and curl
1
EXCLAMATION_MARK='!'
2
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
3
--data-urlencode "To=whatsapp:+15005550006" \
4
--data-urlencode "From=whatsapp:+14155238886" \
5
--data-urlencode "Body=Hello there$EXCLAMATION_MARK" \
6
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Output

1
{
2
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"api_version": "2010-04-01",
4
"body": "Hello there!",
5
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
6
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
7
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
8
"direction": "outbound-api",
9
"error_code": null,
10
"error_message": null,
11
"from": "whatsapp:+14155238886",
12
"num_media": "0",
13
"num_segments": "1",
14
"price": null,
15
"price_unit": null,
16
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"status": "queued",
19
"subresource_uris": {
20
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
21
},
22
"tags": {
23
"campaign_name": "Spring Sale 2022",
24
"message_type": "cart_abandoned"
25
},
26
"to": "whatsapp:+15005550006",
27
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
28
}

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.

This webhook request also supports TwiML (the Twilio Markup Language) just like a regular Twilio SMS request.

Although we're sure it's possible to run a server from your command line, we prefer you use a language with more of a web focus at this point. To handle this request, you need to set up a web application and expose it to the internet. The following SMS Quickstarts show you how to respond to a message and generate TwiML in your language of (second?) choice.

If you're a Node developer, you can also host this web application with Twilio Functions.

(information)

Info

Although these Quickstarts show you how to receive an SMS message, the webhook that Twilio will send will include the same parameters as an incoming SMS message, with the exception that To and From addresses will be set to the WhatsApp number receiving the message (whatsapp:<E.164 formatted Twilio phone number associated with your business>) and the WhatsApp number sending the message (whatsapp:<User's E.164 phone number>), respectively.


What's next for WhatsApp and curl?

whats-next-for-whatsapp-and-curl page anchor

Because the Twilio WhatsApp API is essentially the same as the Twilio Programmable Messaging API, all of the documentation for that API applies to your apps sending and receiving messages with WhatsApp. To dive deeper with a WhatsApp integration, see the WhatsApp documentation overview as well as the API Reference.

Here are some areas you might like to explore next.

We can't wait to see what kind of WhatsApp integration you build!

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.