Adds webhook handlers to the library registry, allowing you to register them with Shopify and process HTTP webhook requests from Shopify.
See the documentation for the full list of accepted topics.
Note: you can only register multiple handlers with the same address when the delivery method is HTTP - the library will automatically chain the requests together when handling events. The library will fail when trying to add duplicate paths for other delivery methods.
const shopify = shopifyApi({
/* ... */
});
const handleWebhookRequest = async (
topic: string,
shop: string,
webhookRequestBody: string,
webhookId: string,
apiVersion: string,
) => {
const sessionId = shopify.session.getOfflineId(shop);
// Fetch the session from storage and process the webhook event
};
shopify.webhooks.addHandlers({
PRODUCTS_CREATE: [
{
deliveryMethod: DeliveryMethod.Http,
callbackUrl: '/webhooks',
callback: handleWebhookRequest,
},
{
deliveryMethod: DeliveryMethod.Http,
callbackUrl: '/webhooks',
callback: handleWebhookRequestPart2,
},
],
});
This method accepts an object indexed by topic. Each topic can map to an object or an array of objects with format:
DeliveryMethod
| ❗ required
The delivery method for this handler. Different fields for this object are allowed depending on the method.
string[]
| Defaults to []
Fields to be included in the callback, defaulting to including all of them.
string[]
| Defaults to []
Namespaces to be included in the callback, defaulting to including all of them.
string
Webhook sub-topics are an extra level of grouping available for some webhook topics. See the Shopify documentation for more information, and the list of topics that support sub-topics.
string
| ❗ required
The path for this handler within your app. The app's host will be automatically set to your configured host.
WebhookHandlerFunction
| ❗ required
The async
callback to call when a shop triggers a topic
event.
string
| ❗ required
The ARN address for the handler.
string
| ❗ required
The Pub-Sub project for your handler.
string
| ❗ required
The Pub-Sub topic for your handler.
When a shop triggers an event you subscribed to, the process
method will call your Http
callbacks with the following arguments:
string
The webhook topic.
string
The shop for which the webhook was triggered.
string
The payload of the POST request made by Shopify.
string
The id of the webhook registration in Shopify.
string
The apiVersion of the webhook.