How To Send a WhatsApp Message in 30 Seconds with Golang
Time to read: 4 minutes
Twilio is all about powering communication conveniently and quickly in any programming language.
In this tutorial you’ll learn how to deliver a message or notification via WhatsApp with a few lines of Go that can be added to any application with the new Twilio Go Helper Library. Ready? Let's get started!
Tutorial requirements
- 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.
- The Go language, version 1.15 or newer. You can download an installer from the official Go web site.
- A smartphone with active service and WhatsApp installed, 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 in the Twilio Console. 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 will 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. Your account can now 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.
Configure Twilio credentials
To be able to send WhatsApp messages via Twilio, the Go application needs to have access to your Twilio account credentials to properly authenticate. The most convenient and secure way to define these configuration values is to set environment variables for them.
The Twilio credentials that you need are your “Account SID” and your “Auth Token”. You can find both on the dashboard of the Twilio Console:
In your terminal, define the following environment variables:
If you are following this tutorial on a Windows computer, use set
instead of export
to define your environment variables in the command prompt.
If you want to learn more about environment variables, check out our how to set environment variables tutorial.
Send a WhatsApp message with Go
With the two environment variables set as shown in the previous section, you can now write a short Go program to send a WhatsApp message. Open a terminal window, find a suitable location to store your project, and create a new directory where the Go project will live:
Then create a Go module for your new project:
After you execute the above command, a *go.mod* file will be added to your project.
The only dependency that you need for this project is the Twilio Go Helper library. You can install it as follows:
To write the application, launch your favorite code editor and open a file named *whatsapp.go* inside the project directory you created above. Enter the following code in it:
Make sure you enter the phone number of the recipient of the WhatsApp message in the place indicated. For this number, use the E.164 format, which includes a plus sign prefix and the country code. For example, if you intend to send a WhatsApp message to number 234-567-8900 in the United States, that line should read:
This application begins by creating a Twilio REST client object. This object is automatically initialized with the Account SID and Auth Token environment variables.
Then, it creates a CreateMessageParams
structure and initializes it with the “to” and “from” phone numbers. When sending from the WhatsApp sandbox, the “from” number is whatsapp:+14155238886
. For a production version of the application you will use the Twilio phone number that was approved for WhatsApp, also prefixed with whatsapp:
. The last parameter added to this structure is the actual text message you are about to send.
The Twilio client object is finally used to create a new message resource, initialized with the parameter structure created above. This is all it takes to send a WhatsApp message with Twilio!
The call to CreateMessage()
returns a response (which is not used in this short example) and an error object that will give you useful information should the message fail to send.
Save the *whatsapp.go* file and then go back to your terminal to run it as follows:
In just a moment, you will receive the message on WhatsApp!
Note that to protect users against spam, WhatsApp only allows you to send freeform messages to a WhatsApp user for a period of 24 hours after the user explicitly contacted you, or in the case of the sandbox also from the moment the user sent the sandbox join code. Visit the documenationt to learn more about the WhatsApp 24-hour window, and how to work around it with message templates.
Conclusion
I hope this short tutorial gives you ideas to continue working with the Go helper library and WhatsApp. At the time I’m writing this article, this library is still being actively developed, so be sure to check the documentation and source code repository often to learn about new releases.
I can’t wait to see what you build with Twilio and Go!
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.