How to Send a Message on WhatsApp with cURL
Time to read: 3 minutes
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.
Prerequisites
To follow this tutorial you need the following items:
- A Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. You can review the features and limitations of a free Twilio account.
- A copy of cURL installed on your computer. If you are using a Unix or Mac OS computer you likely have it already installed. For Windows, you can download an installer.
- A smartphone with an active WhatsApp account, to test the project.
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.
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:
Once you have located the Account SID and Auth Token, we’ll be setting environment variables by typing the following commands into your terminal:
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:
For Windows, the syntax to insert variables in the command is slightly different, so use this version instead:
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:
- How to Send an SMS from a Shell Script using cURL
- How to Send a Picture on WhatsApp from the Command Line Using Twilio and cURL
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!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.