Skip to contentSkip to navigationSkip to topbar
On this pageProducts used

Programmable Messaging for WhatsApp and Go Quickstart


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(link takes you to an external page), and the Gin web framework(link takes you to an external page). 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). 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(link takes you to an external page). Look for your unique sandbox keyword listed as "join <your sandbox keyword>" as shown in the image below.

The Twilio Sandbox for WhatsApp screenshot.

Send this join message to the pre-provisioned Twilio WhatsApp phone number (+1-415-523-8886). For example, if your code is 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>.

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.
(warning)

Warning

Please be aware that the Twilio Sandbox for WhatsApp is meant for testing and discovery purposes only. It should not be used for production.


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 - A unique identifier for your account that is used to authenticate REST API requests
  • Auth Token - Essentially the password for your account, used with the Account SID to authenticate REST API requests

You can reveal your Auth Token by clicking Show:

How to show your Auth Token from the Twilio Console.

Set your credentials as environment variables

set-your-credentials-as-environment-variables page anchor

You'll notice that this code's main function creates a new Twilio client using the twilio.NewRestClient method, but no credentials are passed to it directly. The Twilio Go helper checks to see if your credentials are available as environment variables, and automatically consumes them for you.

Copy the values from the previous step, and set them as environment variables in your terminal with the following commands (replacing the placeholders with your own values):

1
export TWILIO_ACCOUNT_SID=<your-account-sid>
2
export TWILIO_AUTH_TOKEN=<your-auth-token>
(information)

Info

Check out how to set environment variables(link takes you to an external page) for more information or other platform-specific syntax.


Install Go and the Twilio Helper Library

install-go-and-the-twilio-helper-library page anchor
(information)

Info

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:

go version

You should see something like:

1
$ go version
2
go 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(link takes you to an external page). 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.

Initialize your project and install the Twilio Go Helper Library

initialize-your-project-and-install-the-twilio-go-helper-library page anchor

Create a new Go project from your terminal using:

go 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.

go 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.


Send a message with WhatsApp in Go

send-a-message-with-whatsapp-in-go page anchor

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.

Send a message with WhatsApp and GoLink to code sample: Send a message with WhatsApp and Go
1
// Download the helper library from https://www.twilio.com/docs/go/install
2
package main
3
4
import (
5
"fmt"
6
"github.com/twilio/twilio-go"
7
api "github.com/twilio/twilio-go/rest/api/v2010"
8
"os"
9
)
10
11
func main() {
12
// Find your Account SID and Auth Token at twilio.com/console
13
// and set the environment variables. See http://twil.io/secure
14
// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
15
client := twilio.NewRestClient()
16
17
params := &api.CreateMessageParams{}
18
params.SetFrom("whatsapp:+14155238886")
19
params.SetTo("whatsapp:+15005550006")
20
params.SetBody("Hello there!")
21
22
resp, err := client.Api.CreateMessage(params)
23
if err != nil {
24
fmt.Println(err.Error())
25
os.Exit(1)
26
} else {
27
if resp.Body != nil {
28
fmt.Println(*resp.Body)
29
} else {
30
fmt.Println(resp.Body)
31
}
32
}
33
}

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.

To handle this request, you need to set up a web application and expose it to the internet. The Go Programmable Messaging Quickstart shows you how to respond to a message and generate TwiML in Go with Gin.

And—that's all there is to it; receiving and responding is exactly the same as you would do in any SMS app with our Messaging API. Cool, right?

(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 Go?

whats-next-for-whatsapp-and-go 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.