Quantity Verification
Why the worker re-checks line-item quantities against the ShipHero API, how it fails open, and how to disable it.
ShipHero's fulfillment webhook intermittently reports quantity: 1 for line
items that actually shipped more (for example, one unit on a three-unit line).
Trusting that number would understate what shipped. To guard against it, the
worker re-checks each order's true line-item quantities against the ShipHero API
before writing them.
How it works
The check runs once per webhook, right after the brand and warehouse lookups and before any line items are written.
buildVerifiedQuantitiesdecides whether to verify. It returnsnull(meaning "just use the webhook values") if verification is disabled or the brand has no ShipHero access token.fetchShipheroLineItemQuantitiesruns a GraphQL query againsthttps://public-api.shiphero.com/graphqlfor the order's line items, building a map oflegacy_id â quantity. The request has a 3-second timeout (AbortSignal.timeout).resolveQuantityis called for each line item as it is written. It compares the webhook quantity with the verified one:- If there is no verified value, or the two match, it uses the webhook value.
- If they differ, it uses the ShipHero value and logs a
đ¨ Quantity mismatchwarning naming the line item, SKU, and both numbers.
The resolved number is what lands in SKU_Quantity on the
Shipment Line Items row.
Fail-open by design
Verification is a safety net, not a hard dependency. Every failure path (the
feature being disabled, a missing token, a timeout, a throttle, a GraphQL error,
or the order not being found) returns null, and the worker falls back to the
webhook's own quantities. A slow or unavailable ShipHero API can never block a
shipment from being recorded.
The kill switch
Verification is controlled by the optional VERIFY_QUANTITIES environment
variable:
- Unset or any value other than
"false"â verification is on (the default). "false"â verification is off; the worker uses webhook quantities and logsâšī¸ Quantity verification disabled via VERIFY_QUANTITIES.
Set it as a Cloudflare Worker secret to toggle the behavior without a code change:
npx wrangler secret put VERIFY_QUANTITIES
# enter: false