# Webhooks

URL: https://www.digia.tech/docs/developer/reference/webhooks

Receive Digia webhooks on your server: setup, headers, signature check and delivery.

Digia sends a webhook to your server when something happens in your project. All events use one URL and one signing secret.

| Event | Sent when | Page | `reward.won` | A user wins a reward in a gamification campaign | Gamification: reward.won | `submission.created` | The app sends a submission, such as a survey answer | Surveys: submission.created 

## Set up

In the dashboard, open **Project settings → Webhook** .

1. Enter the HTTPS **URL** of your endpoint.
2. Choose **Copy** next to the **signing secret** and store it on your server.
3. Tick the **events** you want.
4. Turn the webhook **On** and choose **Save** .

## Request

Digia sends a `POST` with a JSON body.

| Header | Value | `Content-Type` | `application/json` | `X-Digia-Event` | The event name, such as `reward.won` . | `X-Digia-Delivery-Id` | A unique ID for the delivery. | `X-Digia-Timestamp` | When the request was signed, in Unix seconds. | `X-Digia-Signature` | The signature, in lowercase hex. 

Every event has the same body shape, so one handler with a switch on `event` covers them all.

```json
{
  "event": "reward.won",
  "test": false,
  "grant": { "grantId": "665f1c2e9a1b2c3d4e5f6a7b", "...": "..." },
  "occurredAt": "2026-11-20T10:15:00.000Z"
}
```

| Field | Meaning | `event` | The event name. Same as the `X-Digia-Event` header. | `test` | `true` when a dashboard test sent it. Don't give anything real for a test event. | `occurredAt` | When it happened, in ISO 8601 UTC. | One object named after the event | The event's data, such as `grant` or `submission` . It holds its own ID, such as `grantId` or `submissionId` . 

## Verify the signature

Check the signature before you trust a request.

| | | Algorithm | HMAC-SHA256 | Key | The signing secret, as a text string. | Signed text | `${timestamp}.${rawBody}` : the `X-Digia-Timestamp` value, a dot, and the exact body bytes. 

Sign the raw body, not a re-written copy. Reject a timestamp more than 5 minutes from your clock.

- Node.js - Python (Flask)

```js
import crypto from 'node:crypto';
import express from 'express';

const app = express();
const SECRET = process.env.DIGIA_WEBHOOK_SECRET;
const seen = new Set(); // use your database in production

// express.raw keeps the body as bytes, which is what Digia signed.
app.post('/webhooks/digia', express.raw({ type: 'application/json' }), (req, res) => {
  const timestamp = req.get('X-Digia-Timestamp') ?? '';
  const signature = req.get('X-Digia-Signature') ?? '';
  if (!/^\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) {
    return res.sendStatus(400);
  }

  const expected = crypto
    .createHmac('sha256', SECRET)
    .update(`${timestamp}.`)
    .update(req.body)
    .digest('hex');
  const given = Buffer.from(signature);
  const wanted = Buffer.from(expected);
  if (given.length !== wanted.length || !crypto.timingSafeEqual(given, wanted)) {
    return res.sendStatus(401);
  }

  const deliveryId = req.get('X-Digia-Delivery-Id');
  if (!seen.has(deliveryId)) {
    const event = JSON.parse(req.body.toString('utf8'));
    if (!event.test) {
      // Handle event.event ("reward.won", "submission.created") here.
    }
    seen.add(deliveryId);
  }
  res.sendStatus(200);
});

app.listen(3000);
```

```python
import hashlib
import hmac
import json
import os
import time

from flask import Flask, abort, request

app = Flask(__name__)
SECRET = os.environ["DIGIA_WEBHOOK_SECRET"].encode()
seen = set()  # use your database in production

@app.post("/webhooks/digia")
def digia_webhook():
    timestamp = request.headers.get("X-Digia-Timestamp", "")
    signature = request.headers.get("X-Digia-Signature", "")
    if not timestamp.isdigit() or abs(time.time() - int(timestamp)) > 300:
        abort(400)

    body = request.get_data()  # the raw bytes, which is what Digia signed
    expected = hmac.new(SECRET, timestamp.encode() + b"." + body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(signature.encode(), expected.encode()):
        abort(401)

    delivery_id = request.headers.get("X-Digia-Delivery-Id")
    if delivery_id not in seen:
        event = json.loads(body)
        if not event.get("test"):
            pass  # Handle event["event"] ("reward.won", "submission.created") here.
        seen.add(delivery_id)
    return "", 200
```

## Delivery

Reply with any `2xx` status within 2 seconds. Save the event first and do slow work after you reply.

Digia retries failed deliveries for up to 38 hours. A retry can arrive twice or out of order, so make your endpoint idempotent: skip any `X-Digia-Delivery-Id` you already handled.

Test plays send `reward.won` with `"test": true` . Check it before you give anything real.
