Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

shopify-api Nodejs

Solved

shopify-api Nodejs

Zesa
Shopify Partner
2 0 0

Hello there,

I am currently working on a shopify app (not embedded, api version: 6.2.0),

following this steps: GitHub - Shopify/shopify-api-js: Shopify Admin API Library for Node. Accelerate development with sup...

 

this app will be installed at a single shop and the only function will be to detect a new order via webhook and send an email with a specific format of the order data

 

the Webhook seems to be registered correctly but my Problem arises when I try to Process the Webhook. I just get the error:

 

//Error

C:\Users\censored\Desktop\Auth\node_modules\@shopify\shopify-api\lib\webhooks\process.js:72
throw new ShopifyErrors.InvalidWebhookError({
^

InvalidWebhookError: Could not validate request for topic orders/create
at Object.<anonymous> (C:\Users\censored\Desktop\Auth\node_modules\@shopify\shopify-api\lib\webhooks\process.js:72:23)
at Generator.next (<anonymous>)
at fulfilled (C:\Users\censored\Desktop\Auth\node_modules\tslib\tslib.js:164:62) {
response: undefined
}

 

//app use

app.use(express.json());
app.use(cors());

 

//addHandlers

shopify.webhooks.addHandlers({
    ORDERS_CREATE : [
        {
            deliveryMethod: DeliveryMethod.Http,
            callbackUrl: "/shop/webhook",
            callback: handleWebhookRequest,
        }
    ]
});
 
//handleWebhookRequest
const handleWebhookRequest = async (
    topic,
    shop,
    webhookRequestBody,
    webhookId,
    apiVersion,
    ) => {
    const sessionId = await shopify.session.getOfflineId({shop});
   
    console.log(topic)
    console.log(webhookRequestBody)
    // Fetch the session from storage and process the webhook event
    return
};
 
//Register webhook
router.get("/auth/callback", async (req,res,next) => {
    const callback = await shopify.auth.callback({
        rawRequest : req,
        rawResponse : res
    })
   
    const response = await shopify.webhooks.register({
        session: callback.session
    })

    if (!response['ORDERS_CREATE'][0].success) {
        console.log(
          `Failed to register ORDERS_CREATE webhook: ${response['ORDERS_CREATE'][0].result}`,
        );
    }
});
 
//Process webhook
router.post("/webhook",async (req,res,next) => {
    await shopify.webhooks.process({
        rawBody : JSON.stringify(req.body),
        rawRequest : req,
        rawResponse : res
    })

    res.status(200).send('OK')
    return
})
 
I looked for this problem, but only found threads which manually compared the hmac
 
/Manual Hmac compare
const genhmac = crypto.createHmac('sha256', process.env.SHOPIFY_SECRET)
                                                                   .update(JSON.stringify(req.body))
                                                                   .digest('base64')
 
if (req.header('x-shopify-hmac-sha256') == genhmac) {
    console.log(verified)
}
 
but I never got matching hmacs
 
I hope someone can help me with that
Best regards,
Zesa
Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1453 238 528

This is an accepted solution.

Hey Zesa, 

Just wanted to pass on a quick suggestion regarding this post. If you are have any technical questions or concerns about the functionality of a Shopify development tool(Shopify CLI, API Libraries, etc), the best place to bring them would be the official Github repository; in this case [Issues - shopify-api-js](https://github.com/Shopify/shopify-api-js/issues), though searching through open and closed issues is always a good place to start. If you can't find anything related or haven't already created a post over there, we suggest including as much detail as possible and any troubleshooting, findings, or updates like you have here.

 

Hope this helps provide some direction - Cheers!
@awwdam - Shopify Developer Support

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Zesa
Shopify Partner
2 0 0

I got a small little update:

 

by changing app.use(express.json()) to

app.use(bodyparser.raw({type: 'application/json'}));
 
and then manually compare the hmacs:
 
const genhmac = crypto.createHmac('sha256', process.env.SHOPIFY_SECRET)
                                                                   .update(req.body.toString())
                                                                   .digest('base64')
 
if (req.header('x-shopify-hmac-sha256') == genhmac) {
    console.log(verified)
}
 
the hmacs now match.
However now when I want to use: 
await shopify.webhooks.process({
        rawBody : req.body.toString(),
        rawRequest : req,
        rawResponse : res
    })
 
I get the Error:
InvalidWebhookError: Received invalid shop argument

ShopifyDevSup
Shopify Staff
1453 238 528

This is an accepted solution.

Hey Zesa, 

Just wanted to pass on a quick suggestion regarding this post. If you are have any technical questions or concerns about the functionality of a Shopify development tool(Shopify CLI, API Libraries, etc), the best place to bring them would be the official Github repository; in this case [Issues - shopify-api-js](https://github.com/Shopify/shopify-api-js/issues), though searching through open and closed issues is always a good place to start. If you can't find anything related or haven't already created a post over there, we suggest including as much detail as possible and any troubleshooting, findings, or updates like you have here.

 

Hope this helps provide some direction - Cheers!
@awwdam - Shopify Developer Support

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog