Trying to add a product Metafield definition when an app is installed to familiarize myself with requests and such. Is there not an APP_INSTALLED webhook? I checked documentation and could not find anything besides an APP_UNINSTALLED hook.
Will the APP_SUBSCRIPTIONS_UPDATE be sufficient? Or is there a more optimal way to do this type of thing? Below code is how I currently have it implemented, but my “/webhooks/app-installed” callbackUrl is never reached it seems.
Hooks in shopify.server
:
hooks: {
afterAuth: async ({ session }) => {
shopify.registerWebhooks({ session });
},
},
webhooks: {
APP_SUBSCRIPTIONS_UPDATE: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: "/webhooks/app-installed",
},
},
app-installed.tsx
:
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../app/shopify.server";
export const action = async ({ request }: ActionFunctionArgs) => {
try {
const { admin } = await authenticate.webhook(request);
const response = await admin?.graphql(
`#graphql
mutation MetafieldDefinitionCreate($definition: MetafieldDefinitionInput!) {
metafieldDefinitionCreate(definition: $definition) {
createdDefinition {
id
namespace
key
name
description
type {
name,
category
}
ownerType
}
userErrors {
field
message
code
}
}
}`,
{
variables: {
definition: {
namespace: "example_namespace",
key: "example_key",
name: "Example Metafield",
description: "An example metafield definition",
type: "single_line_text_field",
ownerType: "PRODUCT"
}
},
}
);
const data = await response?.json();
return new Response('Metafield created successfully', { status: 200 });
} catch (error) {
console.error("Error in action:", error);
return new Response(JSON.stringify(error), { status: 500 });
}
};
export default action;