# tys.webhooks.constructEvent()

> Verifies a delivery's TYS-Signature header against the raw request body and returns the parsed event.

Source: https://docs.tryyoursize.com/reference/node/webhooks-construct-event/

*Method* · `@tryyoursize/node`

Verifies a delivery's `TYS-Signature` header against the raw request body and returns the parsed event. Also exported as a function: `import { constructEvent } from "@tryyoursize/node"`.

```ts
constructEvent(
    rawBody: RawBody,
    signatureHeader: string | string[] | null | undefined,
    secret: string,
    opts?: ConstructEventOptions,
  ): WebhookEvent
```

## Parameters

- **`rawBody`** (`RawBody`, required): The raw request body (string or Buffer), not parsed JSON. With Express use `express.raw({ type: "application/json" })`.
- **`signatureHeader`** (`string | string[] | null | undefined`, required): The `TYS-Signature` header.
- **`secret`** (`string`, required): The endpoint's signing secret (`whsec_…`) from Studio → Developers → Webhooks.
- **`opts`** (`ConstructEventOptions`, optional)
  - **`toleranceSeconds`** (`number | undefined`, optional): Maximum |now − t| in seconds (default 300). 0 or less disables the check.
  - **`now`** (`number | Date | undefined`, optional): The current time (unix seconds or a Date); for tests.

## Returns

`WebhookEvent`: The event. Narrow on `type` to get typed `data`. Payloads never carry shopper identity or measurements.

## Errors

`SignatureVerificationError` when no signature matches or the timestamp is outside the tolerance (5 minutes by default).

## Example

**Node.js**

```ts

app.post("/webhooks/tryyoursize", express.raw({ type: "application/json" }), (req, res) => {
  let event;
  try {
    event = tys.webhooks.constructEvent(req.body, req.headers["tys-signature"], process.env.TYS_WEBHOOK_SECRET!);
  } catch {
    return res.sendStatus(400);
  }
  if (event.type === "garment.published") console.log(event.data.product_skus);
  res.sendStatus(200);
});
```

**Python**

```python
from flask import Flask, request

@app.post("/webhooks/tryyoursize")
def webhook():
    try:
        event = tryyoursize.Webhook.construct_event(
            request.get_data(), request.headers.get("TYS-Signature", ""), os.environ["TYS_WEBHOOK_SECRET"]
        )
    except tryyoursize.SignatureVerificationError:
        return "", 400
    if event["type"] == "garment.published":
        print(event["data"]["product_skus"])
    return "", 200
```

## See also

- [Webhooks guide](https://docs.tryyoursize.com/guides/webhooks.md)
