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, Go, the Twilio Go helper library, and the Gin web framework. In this Quickstart, you will learn how to:
Prefer to watch a video? The video below shows you how to use the Sandbox to send and receive WhatsApp messages.
Before you can send a WhatsApp message from your web language, you'll need to sign up for a Twilio account or sign into your existing account and activate the Twilio Sandbox for WhatsApp. The sandbox 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, navigate to the Twilio Sandbox for WhatsApp. Look for your unique sandbox keyword listed as "join <your sandbox keyword>" as shown in the image below.
cheese-bags
, you would send the message "join cheese-bags". You'll then receive a confirmation message from Twilio that your sandbox can now be used to send and receive messages!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>
.
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 with the link format above that users can scan on their phone to opt in to your sandbox.
_10export TWILIO_ACCOUNT_SID=<your-account-sid>_10export TWILIO_AUTH_TOKEN=<your-auth-token>
Check out how to set environment variables for more information or other platform-specific syntax.
If you've gone through one of our other Go Quickstarts already and have Go and the Twilio Go helper library installed, you can skip this step and get to the rest of the tutorial.
Before you can follow the rest of this tutorial, you'll need to have Go and the Twilio Go module installed.
You can check if you already have Go installed on your machine by opening up a terminal and running the following command:
_10go version
You should see something like:
_10$ go version_10go version go1.19 darwin/amd64
If you don't have Go installed, head over to go.dev and download the appropriate installer for your system. Once you've installed Go, return to your terminal, and run the command above once again. If you don't see the installed Go version, you may need to relaunch your terminal.
Create a new Go project from your terminal using:
_10go mod init twilio-example
Once your project has been initialized, navigate into the newly created twilio-example
directory and install the Twilio Go helper library module.
_10go get github.com/twilio/twilio-go
This will install the twilio-go
module so that your Go code in the current directory can make use of it.
To send a message, use the following code and replace the input to params.SetTo
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 params.SetFrom
, be sure to include the whatsapp:
channel identifier before the Sandbox number in E.164 format: whatsapp:+14155238886
.
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.