Subscribing webhook using nodejs and express

Highlighted
Shopify Partner
3 1 1

I am new to creating Shopify apps. I have followed the 'Build a Shopify app with Node and Express' instructions (https://help.shopify.com/en/api/tutorials/build-a-shopify-app-with-node-and-express) and that all works great, I can successfully make an API call to my development store to see all orders. What I want to do is to remove the part about making an API call (below code from the instructions) for all orders and change this to subscribe a webhook for orders that are created. I have followed the steps within the webhook reference page (https://help.shopify.com/en/api/reference/events/webhook#create-2019-07) but have not had any luck! Would you have a sample code or know how to amend this?

 

 

-------------------------

const shopRequestUrl = 'https://' + shop + '/admin/api/2019-07/shop.json';
const shopRequestHeaders = {
  'X-Shopify-Access-Token': accessToken,
};

request.get(shopRequestUrl, { headers: shopRequestHeaders })
.then((shopResponse) => {
  res.end(shopResponse);
})
.catch((error) => {
  res.status(error.statusCode).send(error.error.error_description);
});

---------------------------------------------------------------------

 

0 Likes
Highlighted
Excursionist
17 2 5

Hopefully it's not too late, but there is a tutorial here, and if you don't need the tutorial, the github repo is already set up with webhooks. Just make sure to copy the correct branch; it's not 'master', it's 'listen-for-store-events-with-webhooks-starter-files'. 

 

HTH! ;-)

0 Likes
Highlighted
Shopify Partner
7 0 2

Is there any tutorial for creating webhooks on nodeJS with Express

0 Likes
Highlighted
Shopify Partner
5 0 0

@ophinjay were you ever able to find a tutorial or get the Nodejs/Express webhook working? I'm hung up on trying to get the raw body.

0 Likes
Highlighted
Shopify Partner
7 0 2

@krumholz I did figure this out. I have detailed it here.

0 Likes
Highlighted
Shopify Partner
5 0 0

For whatever reason I'm missing something, would you mind glancing at this to see what could be tripping me up? I made a few minor modifications that I don't believe change the outcome of what you wrote:

app.use(bodyParser.json({
    limit: '50mb',
    verify(req: any, res: any, buf: Buffer) {
        req.textBody = buf.toString();
    }
}));

app.post('/uninstall', async (req: any, res) => {
    ...
    const hash = crypto
        .createHmac('sha256', apiSecret)
        .update(JSON.stringify(req.textBody))
        .digest('base64');
    ...
});

req.textBody is undefined when I console.log

Would you have a GitHub repo for the code you wrote?

0 Likes
Highlighted
Shopify Partner
7 0 2

@krumholz

My code is in a private repo. So I will not be able to share that.

  1. There was a mistake in the docs. The JSON.stringify(req.textBody) is wrong. Just using the req.textBody directly will do. I have corrected this in the post.
  2. Just check if you are actually getting control in the verify function and that the textBody is getting assigned.
    1. If no, then I don't know whether bodyparser has this feature in all its versions. The one that I'm using is 1.19.0 where it is working fine
    2. If yes, please check if you are overwriting req.textBody in any intermediate middlewares
0 Likes
Highlighted
Excursionist
21 1 3

i have a similar issue where the webhook is failing to register and break the apps from installing. Nodejs throws the following error:

 

 

Failed to register webhook {
  data: {
    webhookSubscriptionCreate: { userErrors: [Array], webhookSubscription: null }
  },
  extensions: {
    cost: {
      requestedQueryCost: 10,
      actualQueryCost: 10,
      throttleStatus: [Object]
    }
  }
}

 

any help please?

 

0 Likes
Highlighted
Shopify Partner
7 0 2

@mrfochon It seems like your API call has been throttled because you are not adhering to the API rate limits. Refer https://shopify.dev/concepts/about-apis/rate-limits

0 Likes
Highlighted
Excursionist
21 1 3

Hi @ophinjay thank you for your kind response. 
This is tue error from the first time attempting to install the app. 
And it’s the Shopify sample app which is published on github. 

0 Likes