Shopify webhooks: verify deliveries (base64 HMAC-SHA256 of the raw body in X-Shopify-Hmac-Sha256), read the topic, shop and delivery-id headers for routing and dedupe, and manage webhook subscriptions over the Admin API. See https://shopify.dev/docs/apps/build/webhooks/subscribe/https.

Values

SUBSCRIPTION_FIELDS

SUBSCRIPTION_FIELDS: Str "id topic uri filter includeFields metafieldNamespaces format name createdAt updatedAt"

Functions

build-subscription-input

fn (uri: Str, options: Map): Map

WebhookSubscriptionInput from a delivery uri (HTTPS URL, or a Pub/Sub / EventBridge URI) and options filter, includeFields, metafieldNamespaces, format (JSON or XML), name. Unset options are omitted. Exposed for testing.

Example

::shopify::webhooks/build-subscription-input("https://example.com/shopify/webhook", {format: "JSON", name: null})  // {uri: "https://example.com/shopify/webhook", format: "JSON"}

create-subscription

fn (topic: Str, uri: Str): Map
fn (topic: Str, uri: Str, options: Map): Map
fn (topic: Str, uri: Str, options: Map, opts: Map): Map

Subscribe the app to a topic (webhookSubscriptionCreate). topic is a WebhookSubscriptionTopic enum value such as ORDERS_CREATE or PRODUCTS_UPDATE. Returns the subscription.

Example

::shopify::webhooks/create-subscription("ORDERS_CREATE", "https://example.com/shopify/webhook", {
  includeFields: ["id", "name", "total_price"]
})

delete-subscription

fn (id: Str): Str
fn (id: Str, opts: Map): Str

Delete a webhook subscription by id (webhookSubscriptionDelete). Returns the deleted id.

Example

::shopify::webhooks/delete-subscription("gid://shopify/WebhookSubscription/3")

delivery

fn (request: Map): Map

All the routing headers at once: {topic, shop-domain, webhook-id, event-id, api-version, triggered-at, name} ("" for absent ones).

Example

info ::shopify::webhooks/delivery({headers: {"x-shopify-topic": "orders/create", "x-shopify-webhook-id": "b54557e4"}})
info.topic  // "orders/create"
info.webhook-id  // "b54557e4"
info.event-id  // ""

event-id

fn (request: Map): Str

X-Shopify-Event-Id: shared by deliveries that came from the same merchant action across several subscriptions. For correlation, not dedupe.

Example

::shopify::webhooks/event-id({headers: {"x-shopify-event-id": "98880550"}})  // "98880550"
fn (request: Map, name: Str): Str

A request header by name, case-insensitively (inbound header keys are lowercase; this also accepts maps with other casing). Returns "" when absent.

Example

::shopify::webhooks/header({headers: {"X-Shopify-Topic": "orders/create"}}, "x-shopify-topic")  // "orders/create"
::shopify::webhooks/header({headers: {}}, "x-shopify-topic")  // ""

list-subscriptions

fn (): Map
fn (options: Map): Map
fn (options: Map, opts: Map): Map

One page of the app's webhook subscriptions. Options: first, after, topics (Vec of topic enum values), uri. Returns {nodes, pageInfo}.

Example

page ::shopify::webhooks/list-subscriptions({topics: ["ORDERS_CREATE"]})
map(page.nodes, %.uri)

shop-domain

fn (request: Map): Str

The sending shop from X-Shopify-Shop-Domain, e.g. "acme.myshopify.com".

Example

::shopify::webhooks/shop-domain({headers: {"x-shopify-shop-domain": "acme.myshopify.com"}})  // "acme.myshopify.com"

topic

fn (request: Map): Str

The delivery's topic from X-Shopify-Topic, e.g. "orders/create".

Example

::shopify::webhooks/topic({headers: {"x-shopify-topic": "orders/create"}})  // "orders/create"

verify-request

fn (request: Map): Bool
fn (request: Map, secret: Str): Bool

Verify a Shopify webhook: base64 HMAC-SHA256 of the raw body in X-Shopify-Hmac-Sha256, keyed by your app's client secret (or the shop's webhook signing secret for admin-created webhooks). Uses the shopify.webhook.secret context variable by default (failing when it is unset), or an explicit secret; an empty explicit secret never verifies. Shopify signs no timestamp, so dedupe on webhook-id.

Example

shopify-webhook
meta {webhook: {service: "shopify", path: "/shopify/webhook"}}
fn (request) {
  if(not(::shopify::webhooks/verify-request(request)),
    ::hot::http/HttpResponse({status: 401, body: "invalid signature"}),
    ::hot::http/HttpResponse({status: 200, body: ::shopify::webhooks/topic(request)}))
}
Context Vars: shopify.webhook.secret

webhook-id

fn (request: Map): Str

The per-delivery id from X-Shopify-Webhook-Id. Shopify retries failed deliveries with the same id, so record it to skip duplicates.

Example

::shopify::webhooks/webhook-id({headers: {"x-shopify-webhook-id": "b54557e4"}})  // "b54557e4"