Shopify orders/create returns many 422 errors in remix app

Lukas50
Shopify Partner
4 0 0

Hello, 
I'm using the latest remix template and registered an orders/create webhook. On Vercel I'm getting with this code many 422 errors. These 422 don't even get send to the webhook destination. It seems like there is something wrong and the error occurs before sending.

 

Does anyone has the same problem?

Here is my code:

webhooks.jsx

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

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

  if (!admin) {
    // The admin context isn't returned if the webhook fired after a shop was uninstalled.
    throw new Response();
  }

  switch (topic) {
    case "APP_UNINSTALLED":
      if (session) {
        await db.session.deleteMany({ where: { shop } });
      }

      break;
    case "ORDERS_CREATE":
      if (session) {
        console.log(payload);
        const webhook = new admin.rest.resources.Webhook({session: session});
        webhook.topic = "orders/create";
        webhook.address = "URL";
        webhook.format = "json";
        webhook.save({
          update: true,
        });
      } 
      break;
      
    case "CUSTOMERS_DATA_REQUEST":
    case "CUSTOMERS_REDACT":
    case "SHOP_REDACT":
    default:
      throw new Response("Unhandled webhook topic", { status: 404 });
  }

  throw new Response();
};

Part of the shopify.server.js code

  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
    ORDERS_CREATE: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
  },

Would be awesome if someone knows the answer!

Best regards

Lukas

Replies 0 (0)