Webhook.construct_event()
Verify the signature, then return the parsed event envelope.
@staticmethoddef construct_event(payload: Union[bytes, str], sig_header: str, secret: str, tolerance: int = DEFAULT_TOLERANCE, now: Optional[int] = None) -> WebhookEventParameters
Section titled “Parameters”payloadUnion[bytes, str]RequiredThe raw request body (bytes or str), never re-serialized JSON.
sig_headerstrRequiredThe TYS-Signature header.
secretstrRequiredThe endpoint's signing secret (whsec_…).
toleranceintOptional= DEFAULT_TOLERANCEAccepted age in seconds (either direction); 0 disables the check.
nowOptional[int]Optional= NoneOverrides the clock (unix seconds), for tests.
Returns
Section titled “Returns”WebhookEvent: The event envelope (id, type, api_version, created, mode, store_id, data).
Errors
Section titled “Errors”SignatureVerificationError.
Example
Section titled “Example”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 "", 200import 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);});See also
Section titled “See also”Was this page helpful?
Thank you. We read every answer.