Skip to content
OnCharge.io
View as Markdown
Developer docs

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.

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. 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>
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 have livemode: false. More fields may be added to data during early access, so ignore fields you don't use.

Event types#

Email#

EventWhen
email.scheduledAn email with scheduled_at was accepted and is waiting for its time.
email.sentOnCharge handed the email to the recipient's mail server.
email.deliveredThe recipient's mail server accepted it.
email.delivery_delayedThe recipient's server is deferring it. OnCharge keeps trying; a later event says how it ended.
email.bouncedThe recipient's server refused it. A hard bounce adds the address to your suppression list.
email.complainedThe recipient marked it as spam. The address is suppressed for your account.
email.openedThe recipient opened it (only with open tracking on). Approximate: some mail apps open every message automatically.
email.clickedThe recipient clicked a link (only with click tracking on).
email.failedIt could not be sent, for example because of an invalid attachment or a content check.
email.suppressedIt was not sent, because the address is on your suppression list.
email.receivedMail arrived at your inbound address. See Inbound email.

Domains#

EventWhen
domain.createdA sending domain was added.
domain.updatedA domain's settings or status changed.
domain.verifiedEvery required record was found.
domain.failedVerification failed.

Coming later in early access#

EventWhen
contact.created, contact.updated, contact.unsubscribedMarketing contacts: added, changed, or unsubscribed from a channel.
sms.sent, sms.delivered, sms.failed, sms.opted_outText messages, once SMS is available.
push.sent, push.clicked, push.failedWeb 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 and Catch up and replay.