How to Send a Message on WhatsApp with cURL

June 30, 2021
Written by
Reviewed by
Diane Phan
Twilion

How to Send a Message on WhatsApp with cURL

The WhatsApp Business API from Twilio is a powerful, yet easy to use service that allows you to communicate with your users on the popular messaging app.

In this article, you’ll learn how to send a message to a user through WhatsApp using cURL, which would make this functionality accessible from shell scripts.

Project demo

Prerequisites

To follow this tutorial you need the following items:

The Twilio WhatsApp sandbox

Twilio provides a WhatsApp sandbox, where you can easily develop and test your application. Once your application is complete you can request production access for your Twilio phone number, which requires approval by WhatsApp.

In this section you are going to connect your smartphone to the sandbox. From your Twilio Console, select Messaging, then select Try it Out on the sidebar. Open the WhatsApp section. The WhatsApp sandbox page will show you the sandbox number assigned to your account, and a join code.

WhatsApp Sandbox configuration

To enable the WhatsApp sandbox for your smartphone send a WhatsApp message with the given code to the number assigned to your account. The code is going to begin with the word "join", followed by a randomly generated two-word phrase.

Shortly after you send the message you should receive a response from Twilio indicating that your mobile number is connected to the sandbox and can start sending and receiving messages.

If you intend to test your application with additional smartphones, then you must repeat the sandbox registration process with each of them.

Authenticate against Twilio services

We need to safely store some important credentials that will be used to authenticate against the Twilio service.

You can obtain the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN credentials that apply to your Twilio account from the Twilio Console:

Twilio Account Credentials

Once you have located the Account SID and Auth Token, we’ll be setting environment variables by typing the following commands into your terminal:


export TWILIO_ACCOUNT_SID=<YOUR_ACCOUNT_SID>
export TWILIO_AUTH_TOKEN=<YOUR_AUTH_TOKEN>

If you are following this tutorial on a Windows computer, replace export with set in the commands above.

Send a WhatsApp message with Twilio

With the two environment variables set, you are finally ready to send a message on WhatsApp. If you are following this tutorial on a Unix or MacOS computer, here is how to do it:

curl -X POST -d "Body=Hello from cURL" --data-urlencode "From=whatsapp:+14155238886" --data-urlencode "To=whatsapp:<YOUR_NUMBER>" "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages" -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

For Windows, the syntax to insert variables in the command is slightly different, so use this version instead:

curl -X POST -d "Body=Hello from cURL" --data-urlencode "From=whatsapp:+14155238886" --data-urlencode "To=whatsapp:<YOUR_NUMBER>" "https://api.twilio.com/2010-04-01/Accounts/%TWILIO_ACCOUNT_SID%/Messages" -u "%TWILIO_ACCOUNT_SID%:%TWILIO_AUTH_TOKEN%"

In the above commands, replace <YOUR_NUMBER> with the phone number that you used to sign up to the Twilio sandbox. Use the complete number including the plus sign and country code, as defined by the E.164 format.

A moment after running the above command, you will see a ping from WhatsApp on your phone, sent directly from your computer!

What's next for sending WhatsApp messages with cURL?

Congratulations on writing a one-line command to send a quick WhatsApp message to your phone! This was so wickedly quick, can you even believe it was possible?

Are you interested in doing more things with Twilio and cURL? Here are some more tutorials for you:

I can’t wait to see what you build!

Miguel Grinberg is a Principal Software Engineer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool project you’d like to share on this blog!