Skip to content

Webhook.construct_event()

Methodtryyoursize

Verify the signature, then return the parsed event envelope.

@staticmethod
def construct_event(payload: Union[bytes, str], sig_header: str, secret: str, tolerance: int = DEFAULT_TOLERANCE, now: Optional[int] = None) -> WebhookEvent
payloadUnion[bytes, str]Required

The raw request body (bytes or str), never re-serialized JSON.

sig_headerstrRequired

The TYS-Signature header.

secretstrRequired

The endpoint's signing secret (whsec_…).

toleranceintOptional= DEFAULT_TOLERANCE

Accepted age in seconds (either direction); 0 disables the check.

nowOptional[int]Optional= None

Overrides the clock (unix seconds), for tests.

WebhookEvent: The event envelope (id, type, api_version, created, mode, store_id, data).

SignatureVerificationError.

from flask import Flask, request
import 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 "", 200

Was this page helpful?