Integrate your Shopify webhooks with Evolv AI to send events.
To fire events to Evolv AI, you'll need to do the following:
- Create a custom Shopify app
- Set up a webhook to respond to the required events
- The ability to make a POST call to Evolv AI
Create a custom app
If you don’t already have a custom Shopify app, you must create one and install it in your store.
- See Custom Apps
Ensure the Custom App has the appropriate permissions for the webhook you register.
For this example, we’ll register to "orders/create." This requires the permission “read_orders.”
Register Webhooks
There are several ways to handle webhook registration. We’ll use the example app generated for Node.js by the Shopify CLI with the HTTP delivery method.
Create a file for the events to handle through webhooks. In this case, we will add a hook for "orders/create."
We’ll use the hook to report back to Evolv AI that:
- The event "orders_create" was fired.
- The current total price on the context field "current_total_price."
Ensure a corresponding event is created in the Evolv AI Manager so it can be processed.
If added to the schema, the current total price can be used for value-based optimization.
Example code:
import { DeliveryMethod } from "@shopify/shopify-api";
export default {
/**
* When an order is created, Shopify invokes this webhook.
*/
ORDERS_CREATE: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: "/api/webhooks",
callback: async (topic, shop, body, webhookId) => {
const payload = JSON.parse(body);
const ENV_ID = '123456' // Use your Evolv environment ID
const date = Date.now();
const USER_EVENT = 'orders_create';
fetch('https://participants.evolv.ai/v1/ENV_ID/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"client": "shopify",
"messages": [{
"type": "context.initialized",
"timestamp": date,
"append":true,
"payload": {
"events": [{
"type": USER_EVENT,
"timestamp": date
}],
"order": {
"current_total_price": payload.current_total_price
}
}
}],
"uid": payload.customer.id
})
}).then(res => res.json())
},
}
};
Add the EvolvWebhookHandlers to the list handlers to process.
import EvolvWebhookHandlers from "./evolv-events.js";
…
// Set up Shopify authentication and webhook handling
app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
shopify.config.auth.callbackPath,
shopify.auth.callback(),
shopify.redirectToShopifyOrAppRoot()
);
app.post(
shopify.config.webhooks.path,
shopify.processWebhooks({ webhookHandlers: {...GDPRWebhookHandlers, ...EvolvWebhookHandlers} })
);
If you don't need to use "value", there's an alternative method of sending events.