Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hi,
I have been using the webhook "app_subscriptions/update" without any error for a year I think. But all of a sudden I am getting errors in the partner dashboard. When I checked the code I can see the following error when a webhook is received. Same happens for uninstall webhook also.
Failed to process webhook: Error: No webhook is registered for topic app_subscriptions/update
Webhook setup as below.
const response = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: "/webhooks",
topic: "APP_UNINSTALLED",
webhookHandler: async (topic, shop, body) =>{
console.log('App uninstalled');
// const obj = JSON.parse(body);
// console.log(obj);
delete ACTIVE_SHOPIFY_SHOPS[shop];
}
,
})
router.post("/webhooks", async (ctx) => {
try {
console.log("processing webhook");
await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
console.log(`Webhook processed, returned status code 200`);
} catch (error) {
console.log(`Failed to process webhook: ${error}`);
}
});
Can anybody trace the issue ?
The recent update I have done updating koa-shopify-auth library
"@shopify/koa-shopify-auth": "^5.0.3",
Solved! Go to the solution
This is an accepted solution.
I can only speak for fixing the uninstall webhook, but I assume it may apply to the /update one as well. Make sure you have the Shopify.Webhooks.Registry.addHandler code above app.prepare. Your router.post code is fine though
Shopify.Webhooks.Registry.addHandler("APP_UNINSTALLED", {
path: "/webhooks",
webhookHandler: async (topic, shop, body) =>
delete ACTIVE_SHOPIFY_SHOPS[shop],
});
app.prepare().then(async () => {
const server = new Koa();
const router = new Router();
server.keys = [Shopify.Context.API_SECRET_KEY];
server.use(
createShopifyAuth({
async afterAuth(ctx) {
// Access token and shop available in ctx.state.shopify
const { shop, accessToken, scope } = ctx.state.shopify;
const host = ctx.query.host;
ACTIVE_SHOPIFY_SHOPS[shop] = scope;
const responses = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: "/webhooks",
topic: "APP_UNINSTALLED",
});
if (!responses["APP_UNINSTALLED"].success) {
console.log(
`Failed to register APP_UNINSTALLED webhook: ${responses.result}`
);
}
// Redirect to app with shop parameter upon auth
ctx.redirect(`/?shop=${shop}&host=${host}`);
},
})
);
This is an accepted solution.
I can only speak for fixing the uninstall webhook, but I assume it may apply to the /update one as well. Make sure you have the Shopify.Webhooks.Registry.addHandler code above app.prepare. Your router.post code is fine though
Shopify.Webhooks.Registry.addHandler("APP_UNINSTALLED", {
path: "/webhooks",
webhookHandler: async (topic, shop, body) =>
delete ACTIVE_SHOPIFY_SHOPS[shop],
});
app.prepare().then(async () => {
const server = new Koa();
const router = new Router();
server.keys = [Shopify.Context.API_SECRET_KEY];
server.use(
createShopifyAuth({
async afterAuth(ctx) {
// Access token and shop available in ctx.state.shopify
const { shop, accessToken, scope } = ctx.state.shopify;
const host = ctx.query.host;
ACTIVE_SHOPIFY_SHOPS[shop] = scope;
const responses = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: "/webhooks",
topic: "APP_UNINSTALLED",
});
if (!responses["APP_UNINSTALLED"].success) {
console.log(
`Failed to register APP_UNINSTALLED webhook: ${responses.result}`
);
}
// Redirect to app with shop parameter upon auth
ctx.redirect(`/?shop=${shop}&host=${host}`);
},
})
);
Hi @technologytest .
Thanks a lot for the quick solution. Implemented the changes and my webhook issues are completely resolved now for both uninstall & app_subscriptions/update.
Can you please explain in detail what happens in the flow of doing so...
As mentioned before, All my webhooks were failing. After changing the code only the webhooks started working again.
Yes all webhooks are working fine after this fix..Thank you..