HELLO,
I am new to the Shopify community and need assistance in troubleshooting why my webhooks are not firing as expected.
I have registered the webhooks and even reset the app, but I see a message stating:
“Sending APP_UNINSTALLED webhook to app server”
This webhook gets delivered successfully. However, the other webhooks are not firing when triggered from the Shopify admin.
Interestingly, if I manually trigger them using the Shopify CLI’s shopify app webhook trigger command, they fire and function as expected.
Below is the relevant code from my shopify.server.ts file:
import "@shopify/shopify-app-remix/adapters/node";
import {
ApiVersion,
AppDistribution,
DeliveryMethod,
shopifyApp,
} from "@shopify/shopify-app-remix/server";
import { PrismaSessionStorage } from "@shopify/shopify-app-session-storage-prisma";
import { restResources } from "@shopify/shopify-api/rest/admin/2024-07";
import prisma from "./db.server";
const shopify = shopifyApp({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET || "",
apiVersion: ApiVersion.October24,
scopes: process.env.SCOPES?.split(","),
appUrl: process.env.SHOPIFY_APP_URL || "",
authPathPrefix: "/auth",
sessionStorage: new PrismaSessionStorage(prisma),
distribution: AppDistribution.AppStore,
restResources,
webhooks: {
APP_UNINSTALLED: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: `/webhooks`,
},
SHOP_REDACT: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: `/webhooks`,
},
CUSTOMERS_DATA_REQUEST: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: `/webhooks`,
},
CUSTOMERS_REDACT: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: `/webhooks`,
},
},
hooks: {
afterAuth({ session }) {
console.log("After Auth");
shopify.registerWebhooks({ session });
},
},
future: {
unstable_newEmbeddedAuthStrategy: true,
},
...(process.env.SHOP_CUSTOM_DOMAIN
? { customShopDomains: [process.env.SHOP_CUSTOM_DOMAIN] }
: {}),
});
export default shopify;
export const apiVersion = ApiVersion.October24;
export const addDocumentResponseHeaders = shopify.addDocumentResponseHeaders;
export const authenticate = shopify.authenticate;
export const unauthenticated = shopify.unauthenticated;
export const login = shopify.login;
export const registerWebhooks = shopify.registerWebhooks;
export const sessionStorage = shopify.sessionStorage;
Below is the relevant code from my webhooks.tsx file:
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
import db from "../db.server";
export const action = async ({ request }: ActionFunctionArgs) => {
try {
// Authenticate webhook and validate HMAC
const { topic, shop, session, admin, payload } =
await authenticate.webhook(request);
console.log("Webhook received", topic);
// console.log("Admin:", admin);
// Check if the request is from an admin, unless it's for "SHOP_REDACT"
// if (!admin && topic !== "SHOP_REDACT") {
// console.log("Unauthorized webhook request");
// return new Response("Unauthorized", { status: 401 });
// }
switch (topic) {
case "APP_UNINSTALLED":
if (session) {
await db.session.deleteMany({ where: { shop } });
console.log(`Sessions for shop ${shop} have been deleted.`);
}
return new Response("App uninstalled data cleared", { status: 200 });
case "SHOP_REDACT":
console.log(`Shop data for shop ${shop} has been erased.`);
return new Response("Shop redaction processed", { status: 200 });
case "CUSTOMERS_DATA_REQUEST":
console.log("Customer data request received");
return new Response("Data request received", { status: 200 });
case "CUSTOMERS_REDACT":
console.log("Customer data redaction received");
return new Response("Customer redaction processed", { status: 200 });
default:
return new Response("Unhandled webhook topic", { status: 404 });
}
} catch (error) {
console.error("Webhook error:", error);
return new Response("Unauthorized", { status: 401 });
}
};
I tested the CUSTOMERS_DATA_REQUEST webhook from the Shopify admin by requesting customer data through the “More Actions” option in the Customers section.
Please solve the issue.
Thank You.
