tys.webhooks.constructEvent()
Esta página aún no está disponible en tu idioma.
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, ): WebhookEventParameters
Section titled “Parameters”rawBodyRawBodyObligatorioThe raw request body (string or Buffer), not parsed JSON. With Express use express.raw({ type: "application/json" }).
signatureHeaderstring | string[] | null | undefinedObligatorioThe TYS-Signature header.
secretstringObligatorioThe endpoint's signing secret (whsec_…) from Studio → Developers → Webhooks.
optsConstructEventOptionsOpcionalPropiedades
toleranceSecondsnumber | undefinedOpcionalMaximum |now − t| in seconds (default 300). 0 or less disables the check.
nownumber | Date | undefinedOpcionalThe current time (unix seconds or a Date); for tests.
Returns
Section titled “Returns”WebhookEvent: The event. Narrow on type to get typed data. Payloads never carry shopper identity or measurements.
Errors
Section titled “Errors”SignatureVerificationError when no signature matches or the timestamp is outside the tolerance (5 minutes by default).
Example
Section titled “Example”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);});from flask import Flask, requestimport tryyoursize
@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 "", 200See also
Section titled “See also”¿Te ha resultado útil esta página?
Gracias. Leemos todas las respuestas.