> Index of every page: https://docs.merchantservicedepot.com/llms.txt

# Webhooks

> Receive signed events, verify them, and handle retries and duplicates.

Webhooks tell your server when something happens on your account, such as an invoice getting paid. You register an HTTPS endpoint, Myela sends each event to it as a signed POST, and your endpoint checks the signature before acting.

## Create an endpoint

Send the URL and, optionally, the events you want. Leave out `enabledEvents`, or send an empty list, and the endpoint receives every event.

```bash title="terminal"
curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/webhook_endpoints \
  -H "Authorization: Bearer myela_sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/webhooks/myela",
    "description": "Order system",
    "enabledEvents": ["invoice.paid", "invoice.payment_failed", "invoice.overdue"]
  }'
```

The response includes the endpoint's signing `secret`, which starts with `whsec_`. It's shown this once. Store it with your other secrets. If you lose it, delete the endpoint and create a new one; there's no way to rotate it in place.

The URL must use `https`, carry no username or password, and point at a public host. A URL that breaks a rule answers `422` with a `details.code` of `ENDPOINT_URL_NOT_HTTPS`, `ENDPOINT_URL_HAS_CREDENTIALS`, `ENDPOINT_URL_NOT_PUBLIC`, or `INVALID_ENDPOINT_URL`. An event name Myela doesn't know is `UNKNOWN_EVENT_TYPE`.

| Route | Does |
| --- | --- |
| `POST /v1/webhook_endpoints` | Create an endpoint. The secret comes back once. |
| `GET /v1/webhook_endpoints` | List your endpoints, all in one response. |
| `GET /v1/webhook_endpoints/{id}` | Retrieve an endpoint. |
| `POST /v1/webhook_endpoints/{id}` | Update url, description, enabledEvents, or status. |
| `DELETE /v1/webhook_endpoints/{id}` | Delete an endpoint. |

Webhook delivery isn't switched on in every environment. Confirm with Myela that it's on for your sandbox account before you test against it.

## Events

Each delivery body is an event: an `id`, a `type`, a `created` time in Unix seconds, and `data`, the invoice, payment or payment link in the same shape the API returns.

| type | Sent when | Every account |
| --- | --- | --- |
| `invoice.created` | An invoice was created. A repeat with the same `Idempotency-Key` doesn't send it again. | Yes |
| `invoice.scheduled` | Automatic collection was scheduled. Sent again each time you schedule the invoice again. | Yes |
| `invoice.paid` | An invoice was paid, by scheduled collection or through its payment link. Sent once per invoice. | Yes |
| `invoice.payment_failed` | A collection attempt failed. Usually another attempt is scheduled. | Yes |
| `invoice.overdue` | Collection has stopped. Nothing further will be tried. | Yes |
| `payment.settled` | A payment settled. | Not yet |
| `payment.refunded` | A payment was refunded, in full or in part. | Not yet |
| `payment_link.paid` | A payment link was paid. Sent once per link; `data` is the payment link. | Yes |

`payment.settled` and `payment.refunded` depend on settlement notices that aren't enabled on every account yet. Where they aren't, those two events don't fire, and the invoice events are unaffected. Ask Myela which applies to yours, and read the payment when you need certainty.

## Verify the signature

Every delivery carries a `Myela-Webhook-Signature` header in the form `t=<unix seconds>,id=<delivery id>,v1=<hex hmac>`. The Node SDK's `constructEvent` checks it and returns the event. If it throws, discard the request.

```ts title="webhooks.ts"
import { constructEvent } from '@myela/payments';

// The RAW body is required — see below.
app.post('/webhooks/myela', express.raw({ type: 'application/json' }), async (req, res) => {
  let event;
  try {
    event = await constructEvent({
      payload: req.body,
      signature: req.header('Myela-Webhook-Signature')!,
      secret: process.env.MYELA_WEBHOOK_SECRET!,
      toleranceSeconds: 300, // default
    });
  } catch {
    return res.sendStatus(400); // discard — do not act on it
  }

  res.sendStatus(200); // acknowledge FIRST, then work — slow handlers get retried

  // Dedupe on event.id before acting — you WILL receive the same event twice.
  switch (event.type) {
    case 'invoice.paid':
      await markPaid(event.data);
      break;
  }
});
```

Verify against the raw body, exactly the bytes you received. Parsing the JSON and serializing it again changes the bytes and the signature won't match, so mount a raw body parser on this route.

### Verifying without the SDK

1. Split the header into `t`, `id`, and `v1`.
2. Build four lines joined by `\n`, with no trailing newline: `MYELA-WEBHOOK-SHA256-V1`, the `t` value, the `id` value, and the lowercase hex SHA-256 of the raw body.
3. Compute HMAC-SHA256 of that string with the endpoint secret, as lowercase hex, and compare it to `v1` in constant time.
4. Reject the delivery if `t` is more than five minutes from your clock. Myela can't enforce that window for you.

## Handle deliveries

- Answer fast. Any `2xx` within 10 seconds counts as delivered. Return it first, then do the work, or a slow handler gets retried.
- Expect duplicates. Dedupe on the event `id` in the body. The `id` in the signature header is the delivery id, one per endpoint, so it differs between endpoints for the same event.
- Don't rely on order. Events can arrive out of sequence.
- Point the endpoint at its final URL. Redirects aren't followed, and a `3xx` counts as a failure.

Requests come with `Content-Type: application/json` and `User-Agent: Myela-Webhooks/1`.

## Retries and failures

A delivery that fails gets five more attempts, on a schedule of roughly 1 minute, 5 minutes, 30 minutes, 2 hours, and 6 hours. If all six fail, the delivery is `exhausted`. The signature's `t` is fresh on every attempt; the delivery `id` stays the same.

After 20 failed deliveries in a row to an endpoint, across all its events, Myela disables it and sets `disabledReason`. Fix whatever is failing before you turn it back on with `status: "active"` on `POST /v1/webhook_endpoints/{id}`.

## Delivery logs and replay

`GET /v1/webhook_deliveries` lists deliveries, filtered by `endpointId` and `status`, and pages like every other list (see [Pagination](https://docs.merchantservicedepot.com/docs/platform/pagination/)). Each one shows `eventId`, `eventType`, `attemptCount`, `lastResponseStatus`, `lastError` (what went wrong, including the start of your endpoint's response body), `nextAttemptAt`, and `deliveredAt`. Read one with `GET /v1/webhook_deliveries/{id}`.

| status | Means |
| --- | --- |
| `pending` | Queued to send. |
| `failed` | An attempt failed and a retry is scheduled. |
| `delivered` | Your endpoint answered with a 2xx. |
| `exhausted` | Every attempt failed. No more retries. |

`POST /v1/webhook_deliveries/{id}/replay` sends a delivery again with the same delivery id and the same payload. It's queued for the next send, not sent while you wait. The attempt count isn't reset, so an exhausted delivery gets one more try, with no retries after it. A replay to a disabled endpoint is marked `exhausted` without being sent.

Source: https://docs.merchantservicedepot.com/docs/payments/webhooks/
