Webhooks
Get a signed request on your backend when something happens in an environment.
Webhooks tell your backend what happened, so you react without polling: a customer's balance
runs low, a credit lands, a customer is refused at a spend limit, a key is revoked. For each event,
a signed POST with a JSON body goes to every endpoint of the environment that subscribes to it.
Add an endpoint#
An environment has up to 16 endpoints. Add one in the dashboard under Developers → Webhooks, or:
curl https://api.hixel.dev/v1/webhook_endpoints \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://api.example.com/webhooks/gateway",
"description": "Billing worker",
"event_types": ["wallet.balance_low", "wallet.depleted", "wallet.credited"]
}'const { endpoint, secret } = await hixel.webhookEndpoints.create({
url: "https://api.example.com/webhooks/gateway",
description: "Billing worker",
event_types: ["wallet.balance_low", "wallet.depleted", "wallet.credited"],
});
// Store `secret` now: it is shown only once.{
"endpoint": {
"id": "we_…",
"url": "https://api.example.com/webhooks/gateway",
"description": "Billing worker",
"event_types": ["wallet.balance_low", "wallet.depleted", "wallet.credited"],
"enabled": true,
"secret_last_four": "3rXy",
…
},
"secret": "whsec_5Wq3n0xYdR8mJ2kLp7TzA4vHc9GbE1sUfK6iNoQ3rXy"
}Store secret: it signs every request to this endpoint and is shown only now. See
Verifying signatures.
urlmust behttps://on a public host name, with no IP address, no private name such aslocalhost, no user information, query or fragment. Redirects are never followed.event_typeslists event types, or["*"](the default) for every type exceptusage.recorded, which sends one event per request and so must be named.
| Call | What it does |
|---|---|
GET /v1/webhook_endpoints, GET /v1/webhook_endpoints/{id} | Read endpoints (a page, oldest first), never their secret |
PATCH /v1/webhook_endpoints/{id} | Change url, description, event_types or enabled |
DELETE /v1/webhook_endpoints/{id} | Delete it and its deliveries; answers 204, no body |
POST /v1/webhook_endpoints/{id}/rotate_secret | A new secret, shown once; the old one keeps signing for grace_period_seconds (default 24 hours, at most 7 days) |
POST /v1/webhook_endpoints/{id}/test | Send a webhook.test event to this endpoint only |
GET /v1/webhook_endpoints/{id}/deliveries | Recent deliveries: attempts, status, the start of your answer |
POST /v1/webhook_endpoints/{id}/deliveries/{delivery_id}/redeliver | Send a failed delivery again |
GET /v1/events, GET /v1/events/{id} | The environment's events of the last 30 days, exactly as they were sent |
Answer quickly#
Answer with any 2xx within 10 seconds, then do the work: put the event on your own queue.
Anything else is a failure: a 3xx, 4xx or 5xx, no answer in 10 seconds, or no connection.
// An Express handler. Verify on the raw body, before parsing it.
app.post("/webhooks/gateway", express.raw({ type: "application/json" }), (req, res) => {
const raw = req.body.toString("utf8");
if (!verify(raw, req.header("hixel-signature") ?? "", process.env.HIXEL_WEBHOOK_SECRET)) {
return res.status(400).end();
}
const event = JSON.parse(raw);
queue.add(event); // process it elsewhere
res.status(200).end();
});verify is on the next page.
At least once#
An event can arrive more than once: after a timeout whose answer was lost, after a redelivery, or
when the sender stopped between sending and recording your answer. Every delivery of an event has
the same id (evt_…). Record the ids you processed and skip one you have seen.
Retries#
A failed delivery is tried again after about 30 seconds, 2 minutes, 8 minutes, 32 minutes, 2
hours, 8.5 hours and 12 hours, each wait shortened by up to a fifth at random: eight attempts in
about 23 hours. Then the delivery is failed; redeliver it once your endpoint works again.
An endpoint that failed 10 times in a row over at least 24 hours, with no success between, is
disabled (enabled: false, disabled_reason: "failing"). Its deliveries wait; enabling it with
PATCH and {"enabled": true} resumes them.
Order#
Events about one customer's wallet (wallet.*, and usage.recorded of a request its wallet paid)
reach an endpoint one at a time, in the order they happened: a later one is not sent until the
earlier one was delivered or given up. So a wallet.balance_low never arrives after a later
wallet.credited of the same wallet, and each event's balance is the balance after its change.
Other events, and events of different wallets, can arrive in any order. Use created_at to order
them. A redelivery you ask for is outside the order.
Testing#
POST /v1/webhook_endpoints/{id}/test (or Send test event in the dashboard) sends a
webhook.test event to that endpoint, whatever it subscribes to. In a test environment, the events
of mocked requests and test payments are real events: point a test endpoint at your staging
backend.