Webhooks and events
Signed webhooks, every event type, retries, and the events feed for staying in sync.
Add an endpoint
Add one in the dashboard under Developers → Webhooks, or with the API. The signing secret (whsec_…) is shown once: store it as MUNIM_BILLING_WEBHOOK_SECRET. Test and live endpoints are separate and have different secrets.
curl https://billing-sandbox-api.munimtech.com/v1/webhook_endpoints \
-H "Authorization: Bearer $MUNIM_BILLING_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/webhooks/munim",
"events": ["transaction.completed", "subscription.canceled"]}'{
"id": "whk_…",
"url": "https://example.com/webhooks/munim",
"events": ["transaction.completed", "subscription.canceled"],
"project_id": null,
"secret": "whsec_…",
"created_at": 1764460800000
}Leave out events (or send an empty list) to get all of them. POST /v1/webhook_endpoints/:id/test sends a ping event; GET /v1/webhook_endpoints shows each endpoint’s recent deliveries. Add project_id to only get one project’s events.
Verify and handle events
verifyWebhook checks the signature in constant time, rejects timestamps more than five minutes off, and returns a typed event. Pass the raw request body, before any JSON parsing.
// app/webhooks/munim/route.ts
import { verifyWebhook } from 'munim-billing'
export async function POST(request: Request) {
let event
try {
event = await verifyWebhook(
await request.text(), // the raw body
request.headers.get('Munim-Signature'),
process.env.MUNIM_BILLING_WEBHOOK_SECRET!,
)
} catch {
return new Response('bad signature', { status: 400 })
}
switch (event.type) {
case 'transaction.completed':
await grantAccess(event.data.object)
break
case 'subscription.canceled':
await revokeAtPeriodEnd(event.data.object)
break
}
return new Response('ok')
}import express from 'express'
import { verifyWebhook } from 'munim-billing'
const app = express()
// The raw body, not express.json(), so the signature can be checked.
app.post('/webhooks/munim', express.text({ type: '*/*' }), async (req, res) => {
try {
const event = await verifyWebhook(req.body, req.header('Munim-Signature'), process.env.MUNIM_BILLING_WEBHOOK_SECRET!)
await handle(event)
res.sendStatus(200)
} catch {
res.sendStatus(400)
}
})Each delivery is a POST of an event:
{
"id": "evt_…",
"object": "event",
"type": "transaction.completed",
"created_at": 1764460800000,
"project_id": "prj_…",
"data": {
"object": { "id": "txn_…", "object": "transaction", "amount": 999, "fee": 100, "net": 899, … }
}
}Munim-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, `${t}.${rawBody}`)>
Munim-Event-Id: evt_…Not using the SDK? Compute HMAC-SHA256 of `${t}.${rawBody}` with the whole secret string (whsec_ included) as the key, hex-encode it, and compare it with each v1= value. A header with several v1= values passes when any one matches.
Event types
| Event | data.object | When |
|---|---|---|
checkout.completed | Checkout | A checkout was paid. |
transaction.completed | Transaction | A payment went through: a one-time sale, a new subscription or a renewal. |
transaction.refunded | Transaction | All or part of a payment was refunded. |
transaction.disputed | Transaction | The customer opened a chargeback. |
subscription.created | Subscription | A subscription started. |
subscription.updated | Subscription | Its status, period or cancel-at-period-end changed. |
subscription.canceled | Subscription | It ended or was canceled. |
payout.paid | Payout | A payout reached your bank. |
payout.failed | Payout | A payout failed; see failure_reason. |
report.ready | Report | An export finished. download_url is null here; fetch the report for one. |
Retries and duplicates
Answer with a 2xx within 10 seconds. Failed deliveries are retried after 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours, 12 hours and 24 hours.
The same event can arrive more than once. Use Munim-Event-Id (or event.id) to skip ones you’ve handled. Events can also arrive out of order; when it matters, fetch the object for its latest state.
The events feed
Every event webhooks send is also in GET /v1/events. With after it returns the events since that one, oldest first: the simplest way to keep an internal tool or warehouse in sync without running a webhook server. Save the last id you processed and pass it next time.
# The first time: everything from the start, oldest first
curl "https://billing-sandbox-api.munimtech.com/v1/events?after=start&limit=100" -H "Authorization: Bearer $MUNIM_BILLING_SECRET_KEY"
# Every time after: what happened since the last one you saw
curl "https://billing-sandbox-api.munimtech.com/v1/events?after=evt_…" -H "Authorization: Bearer $MUNIM_BILLING_SECRET_KEY"let last = await loadCursor() ?? 'start'
for await (const event of munim.events.since(last)) {
await handle(event)
last = event.id
}
await saveCursor(last)Filter with type (comma-separated) and project_id; limit is 1 to 100, default 50. Without after, the list is newest first and pages with cursor. Read-only keys can read it.