What is a hook?
A hook is a javascript code snippet run in response to a webhook event.
Trigger event
It starts with a webhook event happening in the store, like a customer adding a product to the cart, completing a checkout, or requesting a refund.
When the trigger event is fired, the hook's code gets run.
Hook code
The hook code is a CommonJS module that needs to export a javascript function which is run in response to trigger event.
The module may contain other functions or variable declarations, and it can call any number of actions.
The exported function has the following signature:
Parameters of the function:
- payload - Data sent by the trigger event.
- actions - Object containing all functions that can be used to call Shopify API endpoints, make HTTP requests, send emails, etc.
- context - Environment-specific variables that can be used to tailor the hook code.
The hook runs in a sandbox, and importing other javascript modules is disabled.
Deployment states
A hook can be active or inactive.
An active hook is live, and it responds to its trigger event.
An active hook can be tested, and its trigger event or its properties can be changed at any time.
An inactive hook is dormant, so it doesn't respond to its trigger event.
Inactive hooks can be tested or modified any way.
Run logs
Active hooks generate logs upon every run triggered by a trigger event.
The run logs can be filtered by:
- date
- resource id
- status (successful or failed).
A run log may contain actions, errors, or debug log entries.