Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shopify-app-remix] Allow to add dynamic webhooks in afterAuth() #1030

Open
zirkelc opened this issue Jun 9, 2024 · 4 comments
Open

[shopify-app-remix] Allow to add dynamic webhooks in afterAuth() #1030

zirkelc opened this issue Jun 9, 2024 · 4 comments
Assignees

Comments

@zirkelc
Copy link
Contributor

zirkelc commented Jun 9, 2024

Overview

The shopifyApp() function accepts webhooks on initialization. These webhooks can then be registered in afterAuth():

const shopify = shopifyApp({
  apiKey: 'abcde1234567890',
  // ...etc
  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: '/webhooks',
    },
  },
  hooks: {
    afterAuth: async ({session}) => {
      shopify.registerWebhooks({session});
    },
  },
});

This works for static webhook where the callback URL or AWS event source is static and available on initialization. In my case, the webhook callback URLs depend on values that I first receive after authorization when I have a valid session.

In this case, I cannot use the static webhooks object on shopifyApp(). Instead, I create a second shopifyApp() object inside the afterAuth which I use to register the webhooks:

const shopify = shopifyApp({
  apiKey: 'abcde1234567890',
  // ...etc
  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: '/webhooks',
    },
  },
  hooks: {
    afterAuth: async ({session}) => {
       // get app id and tenant id from API
       // ...

       // format dynamic callback url
       const callbackUrl = formatCallbackUrl(appId, tenantId);

        const webhookFactory = shopifyApp({
          ...config,
          webhooks: {
            APP_UNINSTALLED: {
              deliveryMethod: DeliveryMethod.Http,
              callbackUrl: callbackUrl,
            },
        });

        const result = await webhookFactory.registerWebhooks({ session });
    },
  },
});

It would be nice if there were another option to add webhooks to the shopifyApp() instance after it was created. For example, the instance could a expose the webhooks object so that we can add additional webhooks in afterAuth() OR the shopify.registerWebhooks() function could accept an optional object with webhooks to register:

const shopify = shopifyApp({
  apiKey: 'abcde1234567890',
  // ...etc
  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: '/webhooks',
    },
  },
  hooks: {
    afterAuth: async ({session}) => {
      // Add webhooks to object
      shopify.webhooks = {  
        ...shopify.webhooks,
        APP_UNINSTALLED: {
              deliveryMethod: DeliveryMethod.Http,
              callbackUrl: "dynamic-url",
          },
      };
      shopify.registerWebhooks({session});

      // OR

      // pass additional webhooks to function
      shopify.registerWebhooks({session, webhooks: { 
         APP_UNINSTALLED: {
             deliveryMethod: DeliveryMethod.Http,
             callbackUrl: "dynamic-url",
         }
      }});
    },
  },
});
@lizkenyon
Copy link
Contributor

Hi there 👋

Thank you for this! There are some webhook changes coming down the pipe, and we are looking to make some improvements in this area. I am going to flag this with the rest of the team, and we will be keeping this in consideration!

Copy link
Contributor

We're labeling this issue as stale because there hasn't been any activity on it for 60 days. While the issue will stay open and we hope to resolve it, this helps us prioritize community requests.

You can add a comment to remove the label if it's still relevant, and we can re-evaluate it.

@github-actions github-actions bot added the Stale label Aug 13, 2024
@zirkelc
Copy link
Contributor Author

zirkelc commented Aug 13, 2024

Hi @lizkenyon

is there any roadmap for these webhooks changes? :-)

@lizkenyon
Copy link
Contributor

Hi

I added this ticket to our work backlog. There are some things ahead of this, so transparently we won't be starting on this immediately.

With the release of subscribing to webhooks in the shopify.app.toml file, it makes sense for us to have a high DX way for dynamically subscribing shops to webhooks. We will update this ticket with an idea of the what the API for this will look like when we start working on it.

@github-actions github-actions bot removed the Stale label Aug 14, 2024
@lizkenyon lizkenyon self-assigned this Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants