Skip to content

tys.webhooks.constructEvent()

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".

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

The raw request body (string or Buffer), not parsed JSON. With Express use express.raw({ type: "application/json" }).

signatureHeaderstring | string[] | null | undefinedRequired

The TYS-Signature header.

secretstringRequired

The endpoint's signing secret (whsec_…) from Studio → Developers → Webhooks.

optsConstructEventOptionsOptional
Properties
toleranceSecondsnumber | undefinedOptional

Maximum |now − t| in seconds (default 300). 0 or less disables the check.

nownumber | Date | undefinedOptional

The current time (unix seconds or a Date); for tests.

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

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

import express from "express";
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);
});

Was this page helpful?