# Signed payloads and webhooks

Source: https://cubibase.com/docs/functions/signatures

Also available: [Markdown version](https://cubibase.com/docs/functions/signatures.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Payload signature verification is independent of caller authentication. Use built-in CubiBase HMAC-SHA256 for your own senders, or verify a provider’s scheme (Stripe, GitHub) inside your function against the exact raw body.

## Three signature settings

| Setting | Verified by | Use for |
| --- | --- | --- |
| `none` | Nobody — authentication mode still applies | JWT/API-key protected endpoints |
| `hmac` | The gateway, before your code runs | Your own servers and services sending webhooks |
| `custom` | Your function code, using request.body and request.headers | Stripe, GitHub and other providers with their own schemes |

> **important**: A JWT or API key proves who called — not that the payload is intact. For public webhook endpoints, always require a signature: built-in HMAC for your senders, provider-specific verification for third parties.

## Built-in HMAC-SHA256

When HMAC mode is enabled, the gateway rejects any request without a valid signature. The signed input is `timestamp + "." + nonce + "." + rawBody`, HMAC-SHA256 with the function’s signing secret, hex-encoded.

| Header | Value |
| --- | --- |
| `x-cubibase-timestamp` | Unix seconds; must be within the tolerance window (default 300 s) |
| `x-cubibase-nonce` | 16–128 chars of [A-Za-z0-9._-], single use per function |
| `x-cubibase-signature` | hex(HMAC_SHA256(secret, `${timestamp}.${nonce}.${rawBody}`)) |

**Sign a delivery from your server**

```ts
import { createHmac, randomBytes } from 'node:crypto';

const body = JSON.stringify({ order: 4711, total: 4200 });
const timestamp = String(Math.floor(Date.now() / 1000));
const nonce = randomBytes(16).toString('hex'); // 16–128 chars, single use
const signature = createHmac('sha256', process.env.CUBIBASE_SIGNING_SECRET)
  .update(`${timestamp}.${nonce}.`)
  .update(body) // EXACT raw bytes you send — never re-serialize
  .digest('hex');

await fetch('https://example-project.api.cubibase.com/functions/orders-hook', {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    'x-cubibase-timestamp': timestamp,
    'x-cubibase-nonce': nonce,
    'x-cubibase-signature': signature,
  },
  body,
});
```

- Comparison is constant-time; the timestamp must be fresh; each nonce can be used once — replays are rejected with 401 signature_replayed.
- The signing secret is shown once when you enable HMAC or rotate it, and stored encrypted. It is never returned by read APIs and never logged.
- Rotation keeps the previous secret valid, so senders can roll over without dropped deliveries.

## Stripe, GitHub and other providers

Third parties cannot satisfy a CubiBase-specific format, and the platform does not force one on them. Set authentication to public and signature mode to custom, then verify the provider’s own header inside your handler using request.body (the exact raw bytes) and request.headers.

- Stripe: verify Stripe-Signature (t=timestamp, v1=HMAC) with your whsec_… signing secret, stored as STRIPE_WEBHOOK_SECRET. The Stripe Webhook template does this with the official SDK and handles event idempotency.
- GitHub: verify X-Hub-Signature-256 (sha256=HMAC of the raw body) with the secret you configured on the hook. The GitHub Webhook template is ready to edit.
- Reject invalid signatures with a 4xx before any side effects. Return a fast 2xx only after the work (or its durable record) succeeded.

## Additional resources

- [Templates](https://cubibase.com/docs/functions/templates) The Stripe and GitHub handlers, ready to deploy.
- [Secrets](https://cubibase.com/docs/functions/secrets) Where signing secrets live.
