how do I make a brand new shopify app write a file when a webhook action is being called?

440 views Asked by At

New App is created using shopify cli with remix.js

currently in webhooks.js

`import { authenticate } from "../shopify.server";
import db from "../db.server";

export const action = async ({ request }) => {
  const { topic, shop, session } = await authenticate.webhook(request);

  switch (topic) {
    case "APP_UNINSTALLED":
      if (session) {
        await db.session.deleteMany({ where: { shop } });
      }
      break;
    case "CUSTOMERS_DATA_REQUEST":
    case "CUSTOMERS_REDACT":
    case "SHOP_REDACT":
    default:
      throw new Response("Unhandled webhook topic", { status: 404 });
  }

  throw new Response();
};
`

let's say I want to write a file everytime the carts_create webhooks is being called , How Would I do that?

1

There are 1 answers

1
Unknown User On

You can use fs

    case "CARTS_CREATE": 
      if (session) {
        const cartData = await request.json(); is JSON
      fs.writeFileSync("cart_data.json", JSON.stringify(cartData, null, 2));
      break;
    }