# Payload and signature

Source: https://swapcard.dev/webhooks/webhooks-payload


Every webhook delivery is an HTTP <code>POST</code> request with a JSON body. Learn what the headers and fields contain, and how to check the <code>X-Signature-256</code> header so you only trust requests that come from Swapcard.

## Delivery request
When an event you subscribed to happens, Swapcard sends a `POST` request to your endpoint URL. The JSON body depends on the action that triggered it.

:::tip
Subscribe only to the event types your server handles. This keeps the number of requests your endpoint receives down. See [Subscribe to webhooks](/webhooks/using-webhooks/subscribe).
:::

### Headers
| Header | Description |
| ------ | ----------- |
| `Content-Type` | The content type of the request, for example `application/json`. |
| `User-Agent` | The agent that sends the request, for example `Swapcard/Webhook`. |
| `X-Signature-256` | An HMAC hex digest of the request body, prefixed with `sha256=`. Use it to [verify the signature](#verify-the-signature). |

### Top-level fields
| Field | Type | Present | Description |
| ----- | ---- | ------- | ----------- |
| `context` | [Context](/webhooks/reference/context) | Always | Where the action happened and who triggered it: the event ID, the community ID (if the action happened on the community profile) and the user ID. |
| `event_profile` | [People](/webhooks/reference/people) | When the action is on a profile | The profile the action was taken on. |
| `event_exhibitor` | [Exhibitor](/webhooks/reference/exhibitor) | When the action is on an exhibitor | The exhibitor the action was taken on. |
| `planning` | [Planning](/webhooks/reference/planning) | When the action is on a session | The session (`planning` in the API) the action was taken on. |
| `action` | string | Always | What happened. One of `profile_update`, `profile_create`, `exhibitor_create`, `exhibitor_update`, `planning_create`, `planning_update`. |
| `datetime` | ISO 8601 datetime string | Always | When the action happened. |

For complete examples, see [Event types](/webhooks/webhooks-events#example-of-payload). For every field of every object, see the [object reference](/webhooks/reference/context).

## Verify the signature

The `X-Signature-256` header lets you check that a request comes from Swapcard and that its body wasn't changed. Swapcard computes it as an HMAC hex digest of the request body, with SHA-256 and the secret you set on the subscription as the key, and sends it as `sha256=<hex digest>`.

To verify a request:

1. Read the **raw** request body, exactly as received, before you parse the JSON.
2. Compute the HMAC-SHA256 of the raw body with your secret as the key, and encode it as hexadecimal.
3. Prefix the result with `sha256=` and compare it with the `X-Signature-256` header, using a constant-time comparison to avoid timing attacks.
4. If the values don't match, reject the request and don't process the payload.

:::warning[Use the raw body]
Compute the signature over the bytes you received. If you parse the JSON and serialize it again, whitespace and key order can change, and the signature no longer matches.
:::

```javascript title="Node.js"
const crypto = require("node:crypto");

function isValidSignature(rawBody, signatureHeader, secret) {
  const expected = Buffer.from(
    "sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex"),
  );
  const received = Buffer.from(signatureHeader || "");
  return (
    received.length === expected.length &&
    crypto.timingSafeEqual(received, expected)
  );
}
```

For a Go version, see [this example on the Go Playground](https://go.dev/play/p/3yXKMHzeov7).

### Test your implementation

Use this known value to check your code. With the secret `my_secret`, this request body:

```json title="Request body"
{
  "context": {},
  "event_profile": {
  },
  "action": "profile_create",
  "datetime": "2024-04-01T00:00:00.000Z"
}
```

gives this hex digest:

```text
dabe126d047775d1685e1a28eef57381dc2b31688a49959802c1f9b72a569590
```

So the request carries this header:

```text
X-Signature-256: sha256=dabe126d047775d1685e1a28eef57381dc2b31688a49959802c1f9b72a569590
```

The body must match byte for byte, including the line breaks and the two-space indentation, with no trailing newline.
