What is a hook?
A hook is a piece of JavaScript code that connects your Shopify store with other systems and automates business processes. It can either respond to Shopify store events (like orders or inventory changes) or accept requests from external systems to make changes in your store.
How hooks work
Every hook follows a simple trigger-and-response pattern:
- A trigger occurs (either a Shopify store event or an external API call)
- The hook detects this trigger
- Your custom code runs automatically
- The hook performs actions in Shopify or external systems
Creating a hook
Hooks are written as CommonJS modules that export a single function. Here's the basic structure:
Your hook function receives three important parameters:
- payload: Contains all the data from the trigger event
- actions: Provides functions to interact with Shopify, send emails, make HTTP requests, and more
- context: Gives access to environment-specific variables for customizing your hook's behavior
Hooks run in a secure sandbox environment, so you can’t import external JavaScript modules.
Managing your hooks
Hooks can be in one of two states:
Active Hooks
- Respond to triggers in real-time
- Can be tested at any time
- Allow modifications to trigger events and properties
Inactive Hooks
- Don't respond to triggers
- Can be safely tested and modified
- Useful for development and maintenance
Monitoring hook performance
Every active hook generates detailed run logs, giving you visibility into their performance. You can:
- Filter logs by date, resource ID, or status
- Track successful and failed executions
- Add debug log entries for troubleshooting
- Monitor all actions performed by your hooks
These logs are essential for maintaining and optimizing your hooks' performance.