# Email webhooks

OnCharge tells your server what happened to every email (sent, delivered, bounced, opened and more), when a domain is verified and when mail arrives at your inbound address. Email events go through the same webhook system as payment events: the same endpoints page, the same signature and the same retries.

> **Early access: the API may change.** Event names and fields on this page can still change before general availability.

## Set up an endpoint

Add an HTTPS endpoint on the **Webhooks** page of the dashboard and choose its events: every event, a whole group such as `email.*`, or single events. Store the endpoint's signing secret (`oc_cbsec_…`) on your server. An endpoint you already use for payment events can receive email events too; pick them on the same page.

Then, for every request:

1. Verify the `OnCharge-Signature` header against the raw body.
2. Skip events you have already handled, by `id`.
3. Respond with any `2xx` status within 8 seconds, and do slow work afterwards.

## Verify the signature

Email events are signed exactly like every other OnCharge webhook. `OnCharge-Signature` has the form `t=<unix seconds>,v1=<hex>`, where `v1` is the HMAC-SHA256 of `t + "." + raw_body` with your endpoint's signing secret as the key. Accept the event only if your value matches a `v1` (constant-time comparison) and `t` is within 300 seconds of now.

The step-by-step check, test vector and complete Node.js, PHP and Python verifiers are in [Webhooks: verify the signature](https://oncharge.io/docs/webhooks#verify-the-signature). Use them as they are.

## The request

```http
POST /webhooks/oncharge HTTP/1.1
Content-Type: application/json
OnCharge-Event-Id: evt_7a1e3c5b-9d2f-4b8a-a6c4-3e5f7a9b1d20
OnCharge-Signature: t=1767225600,v1=<hex>
```

```json title="email.delivered"
{
  "id": "evt_7a1e3c5b-9d2f-4b8a-a6c4-3e5f7a9b1d20",
  "type": "email.delivered",
  "created": 1767225600,
  "livemode": true,
  "data": {
    "email_id": "0b6f2c1e-7d4a-4e8b-9f3c-2a1d5e6b7c80",
    "from": "Example Store <orders@example.com>",
    "to": ["shopper@example.net"],
    "subject": "Your order #1001 has shipped",
    "tags": [{ "name": "type", "value": "shipping" }]
  }
}
```

Every event has `id`, `type`, `created`, `livemode` and `data`. Email events carry the email's `email_id` in `data`, with its `from`, `to`, `subject` and `tags`, so you can match it to your own records; fetch the full email with `GET /emails/{id}`. Events from [test keys](https://oncharge.io/docs/messaging#base-url-and-authentication) have `livemode: false`. More fields may be added to `data` during early access, so ignore fields you don't use.

## Event types

### Email

| Event | When |
|---|---|
| `email.scheduled` | An email with `scheduled_at` was accepted and is waiting for its time. |
| `email.sent` | OnCharge handed the email to the recipient's mail server. |
| `email.delivered` | The recipient's mail server accepted it. |
| `email.delivery_delayed` | The recipient's server is deferring it. OnCharge keeps trying; a later event says how it ended. |
| `email.bounced` | The recipient's server refused it. A hard bounce adds the address to your suppression list. |
| `email.complained` | The recipient marked it as spam. The address is suppressed for your account. |
| `email.opened` | The recipient opened it (only with open tracking on). Approximate: some mail apps open every message automatically. |
| `email.clicked` | The recipient clicked a link (only with click tracking on). |
| `email.failed` | It could not be sent, for example because of an invalid attachment or a content check. |
| `email.suppressed` | It was not sent, because the address is on your suppression list. |
| `email.received` | Mail arrived at your inbound address. See [Inbound email](https://oncharge.io/docs/messaging/inbound). |

### Domains

| Event | When |
|---|---|
| `domain.created` | A sending domain was added. |
| `domain.updated` | A domain's settings or status changed. |
| `domain.verified` | Every required record was found. |
| `domain.failed` | Verification failed. |

### Coming later in early access

| Event | When |
|---|---|
| `contact.created`, `contact.updated`, `contact.unsubscribed` | Marketing contacts: added, changed, or unsubscribed from a channel. |
| `sms.sent`, `sms.delivered`, `sms.failed`, `sms.opted_out` | Text messages, once SMS is available. |
| `push.sent`, `push.clicked`, `push.failed` | Web push notifications, once web push is available. |

## Handling events well

- **Expect duplicates and any order.** Retries can deliver an event more than once, and `email.opened` can arrive before `email.delivered`. Use `id` to skip repeats, and `created` to order them.
- **Treat opens as a hint.** Use clicks, replies and orders to judge engagement.
- **Stop mailing on bounces and complaints.** OnCharge already suppresses those addresses; mirror that in your own list so you don't keep trying.
- **Retries and replay** work as for every OnCharge webhook: see [Delivery and retries](https://oncharge.io/docs/webhooks#delivery-and-retries) and [Catch up and replay](https://oncharge.io/docs/webhooks#catch-up-and-replay).
