Webhooks
Webhooks allow UsageFlow to notify your application when important billing events occur.
Instead of continuously polling the UsageFlow API, your system can receive real-time HTTP notifications whenever billing events happen.
This allows you to automatically:
- update internal dashboards
- trigger payment workflows
- suspend customer access
- send billing notifications
How Webhooks Work
When an event occurs in UsageFlow, the system sends an HTTP request to a webhook endpoint configured by your organization.
Example workflow:
Invoice Generated
↓
Webhook Event Created
↓
Webhook Worker Processes Event
↓
HTTP POST Request Sent to Your Endpoint
↓
Your Application Handles Event
This allows your application to react immediately to billing events.
Supported Webhook Events
Legacy integrations may receive these events:
invoice.created
invoice.paid
invoice.failed
subscription.suspended
BillingRecord comparison calculations emit invoice.finalized and invoice.revised after an owner action in the nonproduction acceptance environment. The deployed owner finalization gate remains closed. These records are not tax invoices or payment requests.
Registering a Webhook Endpoint
You can configure webhook endpoints from the dashboard.
Navigate to:
Dashboard → Webhooks → Add Endpoint
Example configuration:
Webhook URL: `https://api.yourapp.com/usageflow/webhook`
Events:
- invoice.created
- invoice.failed
Once configured, UsageFlow will begin sending webhook notifications to this endpoint.
Example Webhook Request
When an event occurs, UsageFlow sends an HTTP POST request.
Example request:
POST https://api.yourapp.com/usageflow/webhook
Content-Type: application/json
Example payload:
{
"type": "invoice.created",
"data": {
"invoiceId": "inv_123",
"amount": 120,
"periodStart": "2025-03-01T00:00:00.000Z",
"periodEnd": "2025-03-31T00:00:00.000Z"
}
}
Your backend should parse this payload and perform appropriate actions.
Handling Webhooks
Your backend should expose an endpoint that processes webhook requests.
Example using Node.js:
import express from "express";
const app = express();
app.use(express.json());
app.post("/usageflow/webhook", (req, res) => {
const event = req.body;
switch (event.type) {
case "invoice.created":
console.log("New invoice generated:", event.data.invoiceId);
break;
case "invoice.paid":
console.log("Invoice paid:", event.data.invoiceId);
break;
case "invoice.failed":
console.log("Payment failed:", event.data.invoiceId);
break;
case "subscription.suspended":
console.log("Subscription suspended:", event.data.subscriptionId);
break;
}
res.status(200).send({ received: true });
});
app.listen(3000);
This allows your application to automatically respond to billing changes.
Webhook Delivery
Webhook delivery is handled asynchronously by a background worker.
Delivery pipeline:
Billing Event Occurs
↓
Webhook Event Stored
↓
Worker Picks Up Event
↓
HTTP Request Sent to Endpoint
↓
Response Logged
Each webhook attempt is recorded in the Webhook Logs section of the dashboard.
Retry Mechanism
Webhook delivery includes a retry system. For BillingRecord invoice.finalized and invoice.revised events, five recorded failed responses exhaust automatic delivery. Failed responses one through four are retried after 1, 2, 4, and 8 minutes. An uncertain response after a worker crash remains retryable. Legacy events retain their existing schedule and bare-hex signature.
Organization owners can inspect each BillingRecord comparison calculation event in Webhook Logs. The view shows selected endpoints, current outcomes, attempts, terminal failures, and replay cycles. BillingRecords are comparison calculations, not tax invoices or payment requests. The view omits endpoint secrets, replay reasons, and receiver response bodies.
To roll back the owner delivery view, remove the Billing event outcomes section and its owner read action, then hide the replay and rotation controls. Keep the WebhookEvent, BillingWebhookWork, WebhookDelivery, and BillingWebhookReplay records; rollback must not delete delivery evidence. This view adds no database migration. The deployed finalization gate remains closed.
If your endpoint does not respond successfully, UsageFlow will retry the request using exponential backoff.
Billing event retry schedule:
- Attempt 1 → immediately
- Attempt 2 → after 1 minute
- Attempt 3 → after 2 additional minutes
- Attempt 4 → after 4 additional minutes
- Attempt 5 → after 8 additional minutes
If all attempts fail, the webhook event is marked as failed.
You can inspect failures in:
Dashboard → Webhooks → Logs
Billing event signature and migration
For invoice.finalized and invoice.revised only, the body is one UTF-8 JSON byte sequence with id, type, createdAt (UTC ISO 8601), organizationId, billingRecordVersionId, and the stored payload. Its serialized bytes and billing facts remain the same across attempts; the timestamp and signature are refreshed for each request. Use id to deduplicate. Verify the signature against the exact received bytes before parsing JSON; do not reserialize the parsed body.
X-UsageFlow-Timestamp is Unix seconds. X-UsageFlow-Signature is v1= followed by 64 lowercase hex characters. The signed bytes are the UTF-8 decimal timestamp, a period, then the raw body bytes. The key is that endpoint's secret and the algorithm is HMAC-SHA256. Reject timestamps more than 300 seconds before or after the receiver's clock. Requests exactly 300 seconds from that clock are valid. Compare decoded 32-byte digests in constant time.
A runnable Express receiver is in webhook-receiver/index.js; run cd webhook-receiver && npm ci && WEBHOOK_SECRET=your-endpoint-secret node index.js. Send billing events to /billing-webhook. Its billing-verifier.js uses express.raw so it verifies before parsing. Its in-memory event-ID Set only demonstrates deduplication; production receivers should commit the event ID under a unique constraint together with their processing result, then return 2xx for duplicates.
Existing /webhook integrations that receive legacy events still use the bare-hex X-UsageFlow-Signature over the raw body and have no timestamp header. To migrate a receiver that also subscribes to billing events, route by the configured endpoint path or event subscription, accept the new v1= protocol for billing events, and retain the legacy verifier for legacy events. Do not choose a verifier based solely on untrusted body fields before authentication. Coordinate receiver deployment before switching billing subscriptions.
Best Practices
Your webhook endpoint should respond within a few seconds.
Long processing tasks should be handled asynchronously.
Webhook events may be retried, so your system should handle duplicate events safely.
Example strategy:
Check if event already processed
↓
If yes → ignore
If no → process event
Logging webhook requests can help debug integration issues.
Example logged data:
event type
invoice id
delivery timestamp
response status
Example Workflow
Here is a typical billing workflow using webhooks:
Customer uses API
↓
UsageFlow records usage events
↓
Invoice generated
↓
invoice.created webhook sent
↓
Your system notifies customer
↓
Customer pays invoice
↓
invoice.paid webhook sent
This allows you to automate billing operations.
Next Steps
Continue exploring the documentation: