> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hypersender.com/llms.txt
> Use this file to discover all available pages before exploring further.

# WhatsApp Webhooks in Go

> Receive WhatsApp webhooks in Golang with net/http: verify the secret, read incoming messages, delivery and read receipts, reactions, and poll votes.

## Overview

Hypersender sends a WhatsApp webhook to your URL whenever something happens on your instance: a new message, a delivery or read receipt, a reaction, a poll vote. You receive it with your own route. The SDK is only needed when you reply.

A webhook is a `POST` with a JSON body and your secret in the `Authorization` header, so any Go router can handle it. The examples use the standard library's `net/http`, and the same handler works with Gin, Echo, or chi. Combine the two examples on this page to build a Golang WhatsApp bot that answers incoming messages.

## Set up the webhook

**1. Add the webhook in the dashboard**

In the [Hypersender dashboard](https://app.hypersender.com), open your WhatsApp instance and add a webhook with your public HTTPS URL, for example `https://example.com/whatsapp/webhook`.

**2. Set a secret**

Set an authorization secret on the webhook. Hypersender sends it in the `Authorization` header so you can reject requests that did not come from Hypersender. Save the same value as `HYPERSENDER_WHATSAPP_WEBHOOK_SECRET`.

**3. Answer quickly**

Reply with a `2xx` status right away. If handling an event takes time, store the payload and process it in the background.

## Receive and verify webhooks

> [tab] net/http
>
> ```go theme={null}
> package main
>
> import (
>     "crypto/subtle"
>     "encoding/json"
>     "fmt"
>     "log"
>     "net/http"
>     "os"
> )
>
> var secret = os.Getenv("HYPERSENDER_WHATSAPP_WEBHOOK_SECRET")
>
> type webhook struct {
>     Event string `json:"event"`
>     Data  struct {
>         Payload map[string]any `json:"payload"`
>     } `json:"data"`
> }
>
> func whatsappWebhook(w http.ResponseWriter, r *http.Request) {
>     if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte(secret)) != 1 {
>         http.Error(w, "unauthorized", http.StatusUnauthorized)
>         return
>     }
>
>     var body webhook
>     if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
>         http.Error(w, "bad request", http.StatusBadRequest)
>         return
>     }
>     fmt.Println(body.Event, body.Data.Payload)
>
>     w.Header().Set("Content-Type", "application/json")
>     w.Write([]byte(`{"status":"ok"}`))
> }
>
> func main() {
>     if secret == "" {
>         log.Fatal("set HYPERSENDER_WHATSAPP_WEBHOOK_SECRET")
>     }
>
>     http.HandleFunc("POST /whatsapp/webhook", whatsappWebhook)
>     log.Fatal(http.ListenAndServe(":8080", nil))
> }
> ```

> [!NOTE]
>
> Compare the secret with `subtle.ConstantTimeCompare`, not `==`, so the comparison takes the same time whether or not the secret matches. Refuse to start when the secret is empty, or a request with no `Authorization` header would match it.

## Payload

Every WhatsApp webhook has the same outer shape:

```json theme={null}
{
  "event": "message.any",
  "data": {
    "event": "message.any",
    "instanceId": "123e4567-e89b-12d3-a456-426614174000",
    "userId": "123e4567-e89b-12d3-a456-426614174000",
    "me": { "id": "201234567890@c.us", "pushName": "My Business" },
    "payload": { "...": "the message, receipt, reaction, or vote" }
  }
}
```

The event-specific fields are in `data.payload`. Each event page in the [WhatsApp webhooks reference](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/whatsapp-webhooks-introduction.md) shows the full payload.

## WhatsApp webhook events

| Event | Fires when |
| - | - |
| [`message.any`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/messages-any) | A message is created in any chat, including messages you send |
| [`message.ack`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/messages-ack) | A message is delivered, read, or played (`ackName`: `SERVER`, `DEVICE`, `READ`, `PLAYED`, or `ERROR`) |
| [`message.reaction`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/message-reaction) | Someone reacts to a message or removes a reaction |
| [`message.revoked`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/messages-revoked) | A message is deleted for everyone |
| [`message.waiting`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/messages-waiting) | WhatsApp shows "Waiting for this message" |
| [`poll.vote`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/poll-vote) | New votes arrive on a poll |
| [`poll.vote.failed`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/poll-vote-failed) | A poll vote could not be decrypted |
| [`presence.update`](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/presence-update) | A contact starts or stops typing or recording |

## Handle each event

Route on `event` and reply with the SDK. This example answers incoming messages and records read receipts:

> [tab] Basic Usage
>
> ```go theme={null}
> var client = hypersender.NewClient()
>
> func handle(ctx context.Context, event string, payload map[string]any) error {
>     switch event {
>     case "message.any":
>         if fromMe, _ := payload["fromMe"].(bool); fromMe {
>             return nil // skip messages your own number sent
>         }
>         from, _ := payload["from"].(string)
>         text, _ := payload["body"].(string)
>         _, err := client.WhatsApp.SendText(ctx, from, "Thanks! We got: "+text, nil)
>         return err
>
>     case "message.ack":
>         if payload["ackName"] == "READ" {
>             log.Printf("Message %v was read", payload["id"])
>         }
>     }
>     return nil
> }
> ```
>
> Call `handle(r.Context(), body.Event, body.Data.Payload)` from your handler after the secret check. For slow work, hand it to a goroutine or a job queue and return right away. A goroutine must not use `r.Context()`, which is canceled once the response is sent; pass `context.Background()` instead.

> [!TIP]
>
> Always skip `message.any` events where `fromMe` is `true`. Otherwise a bot that replies to every message also replies to its own replies.

## Next steps

* [Send Messages](https://docs.hypersender.com/v2/packages-and-sdks/go/whatsapp/send-messages.md): reply with text, media, polls, and more
* [WhatsApp webhooks reference](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/whatsapp-webhooks-introduction.md): every payload in full


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.