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

# SMS Webhooks in Go: Delivery Reports and Incoming SMS

> Receive SMS webhooks in Golang with net/http: verify the secret, get delivery reports, read incoming SMS, and alert when your phone goes offline.

## Overview

Hypersender sends an SMS webhook to your URL when a message is sent, delivered, fails, or expires, when your phone receives an SMS, and when the phone goes online or offline. 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.

## Set up the webhook

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

In the [Hypersender dashboard](https://app.hypersender.com), open your SMS instance, add a webhook with your public HTTPS URL, for example `https://example.com/sms/webhook`, and pick the events you want.

**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_SMS_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_SMS_WEBHOOK_SECRET")
>
> type webhook struct {
>     Event string         `json:"event"`
>     Data  map[string]any `json:"data"`
> }
>
> func smsWebhook(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["id"], body.Data["status"])
>
>     w.Header().Set("Content-Type", "application/json")
>     w.Write([]byte(`{"status":"ok"}`))
> }
>
> func main() {
>     if secret == "" {
>         log.Fatal("set HYPERSENDER_SMS_WEBHOOK_SECRET")
>     }
>
>     http.HandleFunc("POST /sms/webhook", smsWebhook)
>     log.Fatal(http.ListenAndServe(":8080", nil))
> }
> ```

> [!NOTE]
>
> The secret is sent exactly as you saved it, with no `Bearer` prefix. Compare it with `subtle.ConstantTimeCompare`, not `==`.

## Payload

Every SMS webhook has two keys: `event` and `data`. For message events, `data` is the message:

```json theme={null}
{
  "event": "message.phone.received",
  "data": {
    "id": "9f8b7c6d-6666-4a2b-8c3d-4e5f6a7b8c9d",
    "phone_id": "9f8b7c6d-1111-4a2b-8c3d-4e5f6a7b8c9d",
    "request_id": null,
    "from_phone_number": "+12025550188",
    "to_phone_number": "+12025550101",
    "content": "Hi, is my order ready?",
    "status": "received",
    "sim": "SIM1",
    "tags": [],
    "failure_reason": null,
    "received_at": "2026-11-19 10:05:12"
  }
}
```

For `phone.*` events, `data` is the phone, with fields such as `is_online`, `last_heartbeat_at`, and `phone_number`. `request_id` is the value you passed as `RequestID` to `SendMessage`, so you can match a delivery report to your own record. The [SMS webhooks reference](https://docs.hypersender.com/v2/api-reference/sms-webhooks/sms-webhooks-introduction.md) shows every field and header.

## SMS webhook events

| Event | Fires when |
| - | - |
| [`message.notification.scheduled`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-scheduled) | A message is waiting for its send time or the phone's sending rate |
| [`message.notification.failed`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-notification-failed) | Hypersender could not push the message to the phone |
| [`message.phone.sent`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-sent) | The phone handed the message to the carrier |
| [`message.phone.delivered`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-delivered) | The carrier confirmed delivery |
| [`message.send.failed`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-failed) | The phone could not send the message |
| [`message.send.expired`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-expired) | The message was not sent before it expired |
| [`message.phone.ussd`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-ussd) | The phone got a USSD or flash reply for a message you sent |
| [`message.phone.received`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/messages-events/message-received) | The phone received an SMS |
| [`phone.heartbeat.received`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/heartbeats/heartbeat-received) | An online phone sent a heartbeat |
| [`phone.heartbeat.online`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/heartbeats/heartbeat-online) | The phone is back online |
| [`phone.heartbeat.missed`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/heartbeats/heartbeat-missed) | No heartbeat for 16 minutes |
| [`phone.heartbeat.offline`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/heartbeats/heartbeat-offline) | No heartbeat for about an hour; the phone is marked offline |
| [`phone.heartbeat.disabled`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/heartbeats/heartbeat-disabled) | No heartbeat for 2 hours; checks stop |
| [`phone.logout`](https://docs.hypersender.com/v2/api-reference/sms-webhooks/phone-events/phone-logout) | The phone was logged out of the app |

## Handle each event

Route on `event`. This example auto-replies to incoming SMS, tracks delivery, and alerts when the phone goes offline:

> [tab] Basic Usage
>
> ```go theme={null}
> var client = hypersender.NewClient()
>
> func handle(ctx context.Context, event string, data map[string]any) error {
>     switch event {
>     case "message.phone.received":
>         from, _ := data["from_phone_number"].(string)
>         _, err := client.SMS.SendMessage(ctx, from, "Thanks for your message! We'll reply soon.", nil)
>         return err
>
>     case "message.phone.delivered":
>         log.Printf("Delivered: %v", data["request_id"])
>
>     case "message.send.failed":
>         log.Printf("Failed: %v (%v)", data["request_id"], data["failure_reason"])
>
>     case "phone.heartbeat.offline":
>         log.Printf("Phone %v is offline", data["phone_number"])
>     }
>     return nil
> }
> ```
>
> Call `handle(r.Context(), body.Event, body.Data)` 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.

## Retries and duplicates

If your endpoint does not answer with a `2xx` in time, Hypersender retries, so the same event can arrive more than once and out of order.

* Use `event` plus `data["id"]` (and `data["status"]` for message events) as the key to skip duplicates.
* Compare `updated_at` before overwriting a newer state.

## Next steps

* [Send and Manage SMS](https://docs.hypersender.com/v2/packages-and-sdks/go/sms/send-and-manage-sms-messages.md): send, schedule, and list messages
* [Heartbeat Management](https://docs.hypersender.com/v2/packages-and-sdks/go/sms/heartbeat-management.md): check the phone's status on demand
* [SMS webhooks reference](https://docs.hypersender.com/v2/api-reference/sms-webhooks/sms-webhooks-introduction.md): every payload and header in full


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