computeHmacSignature()
Signature
import { computeHmacSignature } from '@rimora/velora'
computeHmacSignature(
secret: string,
body: string | unknown
): Promise<string>Parameters#
| Param | Type | Description |
|---|---|---|
secret | string | The webhook_secret from your job. Use regenerateWebhookSecret() to create one. |
body | string | unknown | The payload to sign. Objects are serialised as JSON. Strings are signed as-is. |
Return value#
Returns Promise<string> containing a signature in the format sha256=<hex>. Pass this value to triggerWebhook() as opts.signature.
Usage#
ts
import { computeHmacSignature, VeloraClient } from '@rimora/velora'
import fetch from 'node-fetch'
const client = new VeloraClient({
apiKey: process.env.VELORA_API_KEY,
fetch: fetch as any
})
const secret = process.env.WEBHOOK_SECRET!
const body = { event: 'order.completed', orderId: 'ord_007' }
const sig = await computeHmacSignature(secret, body)
// → 'sha256=a3f9b1c2d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1'
await client.triggerWebhook(
'9f4a1c2b-3d4e-5f6a-7b8c-9d0e1f2a3b4c',
body,
{ signature: sig }
)Always use the raw, unserialized request body when verifying signatures server-side. Even minor whitespace differences in the JSON will produce a different HMAC.
Velora cron jobs now also send X-Hub-Signature-256 when POSTing to your target_url. Use this same computeHmacSignature function to verify those incoming cron job requests on your server.
How it works#
The function detects the runtime environment and selects the appropriate crypto API:
- Node.js 18+: Uses the built-in crypto.createHmac with 'sha256'.
- Browsers / Cloudflare Workers: Uses SubtleCrypto.importKey + SubtleCrypto.sign with the HMAC algorithm.
The resulting digest is hex-encoded and prefixed with sha256= to match the Velora signature header format.