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

# Webhooks

> Receive real-time delivery events from Torpedo

## Register a webhook

```bash theme={null}
curl -X POST https://api.torpedo.co.mz/api/v1/webhooks \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://myapp.com/webhooks/torpedo",
    "events": ["email.delivered", "email.bounced", "email.complained", "email.failed"]
  }'
```

The response includes a `signingSecret` — **save it immediately**, it is returned once only.

***

## Available events

| Event | When it fires |
| - | - |
| `email.delivered` | AWS SES confirmed delivery to the recipient's inbox |
| `email.bounced` | Email bounced — hard bounces are suppressed automatically |
| `email.complained` | Recipient marked the email as spam |
| `email.failed` | Delivery failed after retries |
| `domain.verified` | Domain DNS records verified successfully |
| `domain.failed` | Domain verification failed |
| `sms.delivered` | The mobile operator confirmed delivery to the handset |
| `sms.undelivered` | The operator could not deliver the SMS (status `undelivered`) |
| `sms.failed` | Torpedo couldn't hand the SMS to the operator |
| `sms.delivery_unknown` | The operator sent no delivery receipt within 25 hours |
| `verification.sent` | A verification code was accepted by WhatsApp or the SMS operator |
| `verification.delivered` | A verification code reached the phone |
| `verification.delivery_failed` | A verification code couldn't be delivered |
| `verification.fallback` | WhatsApp failed and the code was sent by SMS instead |
| `verification.approved` | A verification code was checked or reported as correct |
| `verification.expired` | A verification code expired without being approved |
| `verification.max_attempts_reached` | The user entered too many wrong codes |

***

## Event payload shape

Torpedo sends a `POST` with a JSON body. Every event uses the same envelope:

```json theme={null}
{
  "id": "6f1c2a9e-4b7d-4c1e-9a53-2d8f0b7e4c11",
  "event": "sms.delivered",
  "data": {
    "smsId": "01960000-0000-0000-0000-000000000000",
    "toAddress": "+258841234567"
  },
  "timestamp": "2026-04-15T10:00:00.000Z"
}
```

| Field | Description |
| - | - |
| `id` | Unique event ID. It stays the same when the event is retried, so use it to drop duplicates |
| `event` | The event name, for example `email.delivered` |
| `data` | Event-specific fields, listed below |
| `timestamp` | When Torpedo sent this request, in ISO 8601 (UTC) |

The request also carries these headers:

| Header | Value |
| - | - |
| `Content-Type` | `application/json` |
| `X-Torpedo-Event` | The event name, same as `event` in the body |
| `webhook-id` | The event ID, same as `id` in the body |
| `X-Signature` | `sha256=` followed by the HMAC of the raw body (see below) |

### `data` by event

**Email**

| Event | Fields |
| - | - |
| `email.delivered` | `emailId`, `toAddress` |
| `email.bounced` | `emailId`, `toAddress` |
| `email.complained` | `emailId`, `toAddress` |
| `email.failed` | `emailId`, `toAddress`, `reason` — why sending failed |

**SMS**

| Event | Fields |
| - | - |
| `sms.delivered` | `smsId`, `toAddress` |
| `sms.undelivered` | `smsId`, `toAddress`, `stat`, `providerStatus`, `providerErrorCode`, `failureCategory` |
| `sms.failed` | `smsId`, `toAddress`, `reason`, `providerStatus`, `providerErrorCode`, `failureCategory` |
| `sms.delivery_unknown` | `smsId`, `toAddress`, `sentAt`, `reason` |

* `stat` and `providerStatus` are the operator's delivery status, for example `UNDELIV`, `EXPIRED`
  or `REJECTD`.
* `providerErrorCode` is the operator's error code, or `null` when it didn't send one.
* `failureCategory` is one of `undeliverable`, `expired`, `rejected`, `deleted` or `unknown` for
  `sms.undelivered`, and `submission_error` for `sms.failed`.

A permanent failure (`undeliverable` or `rejected`) also adds the number to your
[suppression list](https://torpedo.co.mz/docs/guides/manage-suppressions.md).

Example `sms.undelivered`:

```json theme={null}
{
  "event": "sms.undelivered",
  "data": {
    "smsId": "01960000-0000-0000-0000-000000000000",
    "toAddress": "+258841234567",
    "stat": "UNDELIV",
    "providerStatus": "UNDELIV",
    "providerErrorCode": "001",
    "failureCategory": "undeliverable"
  },
  "timestamp": "2026-04-15T10:00:00.000Z"
}
```

**Verify (WhatsApp OTP)**

Every `verification.*` event includes `verificationId`, `to` and `clientRef` (your reference from
the start request, or `null`). Some events add more fields:

| Event | Extra fields |
| - | - |
| `verification.sent` | `attemptId`, `channel` |
| `verification.delivered` | `attemptId`, `channel` |
| `verification.delivery_failed` | `attemptId`, `channel`, `errorCode`, `errorCategory`, `reason` |
| `verification.fallback` | `attemptId`, `fromChannel` (`whatsapp`), `toChannel` (`sms`), `reason` |
| `verification.approved` | none |
| `verification.expired` | none |
| `verification.max_attempts_reached` | none |

* `channel` is `whatsapp` or `sms`.
* `errorCode` is the WhatsApp error code, for example `131026` when the number isn't on WhatsApp.
* For WhatsApp, `errorCategory` is one of `recipient_unreachable`, `throttled`, `transient`,
  `invalid_request`, `expired` or `platform`. For SMS it uses the same categories as `sms.undelivered`.
* In `verification.fallback`, `reason` is the WhatsApp `errorCategory` that triggered the fallback.

Example `verification.fallback`:

```json theme={null}
{
  "event": "verification.fallback",
  "data": {
    "verificationId": "0b6d1f3e-7a2c-4f51-9d8e-3c4b2a1f0e9d",
    "to": "+258841234567",
    "clientRef": "signup-4821",
    "attemptId": "5a9e2c4d-1b3f-4e6a-8c7d-9f0e1a2b3c4d",
    "fromChannel": "whatsapp",
    "toChannel": "sms",
    "reason": "recipient_unreachable"
  },
  "timestamp": "2026-10-14T10:00:04.000Z"
}
```

**Domains**

| Event | Fields |
| - | - |
| `domain.verified` | `id`, `domain` |
| `domain.failed` | `id`, `domain`, `reason` |

Use the ID in `data` to fetch the full record, for example `GET /api/v1/sms/{smsId}`,
`GET /api/v1/emails/{emailId}` or `GET /api/v1/verify/{verificationId}`.

***

## Verifying the signature

Every request from Torpedo includes an `X-Signature` header:

```
X-Signature: sha256=a3f2c1d8...
```

Verify it to confirm the request is genuine:

```js Node.js theme={null}
import { createHmac, timingSafeEqual } from 'crypto'

function verifySignature(rawBody, signature, secret) {
const expected = 'sha256=' + createHmac('sha256', secret)
.update(rawBody)
.digest('hex')
return timingSafeEqual(Buffer.from(expected), Buffer.from(signature))
}

// Express example
app.post('/webhooks/torpedo', express.raw({ type: 'application/json' }), (req, res) => {
const sig = req.headers['x-signature']
if (!verifySignature(req.body, sig, process.env.TORPEDO_WEBHOOK_SECRET)) {
return res.status(401).send('Invalid signature')
}
const { event, data } = JSON.parse(req.body)
res.status(200).send('ok')
})

```

```python Python theme={null}
import hmac
import hashlib

def verify_signature(raw_body: bytes, signature: str, secret: str) -> bool:
    expected = 'sha256=' + hmac.new(
        secret.encode(), raw_body, hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(expected, signature)
```

```go Go theme={null}
package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

func verifySignature(rawBody []byte, signature, secret string) bool {
	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write(rawBody)
	expected := "sha256=" + hex.EncodeToString(mac.Sum(nil))
	return hmac.Equal([]byte(expected), []byte(signature))
}

func main() {
	ok := verifySignature([]byte(`{"event":"email.delivered",...}`),
		"sha256=abc123...", "your_signing_secret")
	fmt.Println("valid:", ok)
}
```

```java Java theme={null}
boolean valid = WebhookVerifier.verify(rawBody, signingSecret, signatureHeader);
```

```csharp C# theme={null}
using System.Security.Cryptography;
using System.Text;

static bool VerifySignature(byte[] rawBody, string signature, string secret)
{
    using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
    var hash = hmac.ComputeHash(rawBody);
    var expected = "sha256=" + Convert.ToHexString(hash).ToLower();
    return CryptographicOperations.FixedTimeEquals(
        Encoding.UTF8.GetBytes(expected),
        Encoding.UTF8.GetBytes(signature)
    );
}
```

> [!WARNING]
>
> Always verify the signature before processing webhook payloads. Use timing-safe comparison
> (`timingSafeEqual`, `hmac.Equal`, `CryptographicOperations.FixedTimeEquals`) to prevent timing
> attacks — never use plain string equality.

***

## Delivery retries

Torpedo retries failed webhook deliveries up to 7 times over about 27 hours: after 5 seconds,
5 minutes, 30 minutes, 2 hours, 5 hours, 10 hours and 10 hours. Your endpoint should:

* Return `2xx` within 5 seconds
* Be idempotent. The same event may be delivered more than once, always with the same `id`

Status codes that change retry behaviour:

| Response | What Torpedo does |
| - | - |
| `410 Gone` | Stops retrying and disables the webhook. Send a test event from the dashboard to turn it back on |
| `429` or `503` with a `Retry-After` header | Waits the requested time (up to 24 hours) before the next attempt |

View delivery history via `GET /api/v1/webhooks/{id}/deliveries`.


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