Skip to content

Instantly share code, notes, and snippets.

@joshbeckman
Created October 10, 2015 17:37
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save joshbeckman/5c4f0244914adfd312e4 to your computer and use it in GitHub Desktop.
Save joshbeckman/5c4f0244914adfd312e4 to your computer and use it in GitHub Desktop.
An example Node.js server that can verify a Shopify webhook's integrity. Run with `node index.js`.
const PORT = 3000;
const SECRET = 'APP_SHARED_SECRET';
var http = require('http'),
crypto = require('crypto'),
server;
function verifyShopifyHook(req) {
var digest = crypto.createHmac('SHA256', SECRET)
.update(new Buffer(req.body, 'utf8'))
.digest('base64');
return digest === req.headers['X-Shopify-Hmac-Sha256'];
}
function parseRequestBody(req, res) {
req.body = '';
req.on('data', function(chunk) {
req.body += chunk.toString('utf8');
});
req.on('end', function() {
handleRequest(req, res);
});
}
function handleRequest(req, res) {
if (verifyShopifyHook(req)) {
res.writeHead(200);
res.end('Verified webhook');
} else {
res.writeHead(401);
res.end('Unverified webhook');
}
}
server = http.createServer(parseRequestBody);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});
@thierryskoda
Copy link

Are you sure this works ? I tried it and it doesn't work for me. I tried this:

console.log("here");
req.on('data', function(chunk) {
        console.log("test");
        req.body += chunk.toString('utf8');
    });

console.log("here"); get called but not the console.log("test");

@winsandymyint
Copy link

I tested that with both App's Secrete key and webhook verify key.
But it keeps showing me "Unverified webhook". :'(
epw_ohqgiv0

@winsandymyint
Copy link

Oh! Your code works!
my mistake. I forget to req with body when I test via postman. That's why.
Cool! Thank bro.

@joshbeckman
Copy link
Author

@winsandymyint Glad it could help!
@thierryskoda Did you POST something in your request body?

@frehner
Copy link

frehner commented Nov 4, 2016

The new buffer object (line 10) saved me; my version wasn't working on real Shopify orders (but was on test webhooks!) until I added that part in. Much thanks!

@martinliptak
Copy link

Thanks!

@jmortensen
Copy link

Thanks for making this. Another thing to keep in mind is that various middleware (e.g. body-parser) can alter the body and that can lead to being unable to verify the webhook. Make sure you have a reference to the raw body of what shopify sent so you can run that through your verification code.

@tiendq
Copy link

tiendq commented Nov 16, 2017

@andjosh great work, I don't know why Shopify documentation is so poor and it takes 2 different ways to verify HMAC.

@jmortensen and others: I got it worked well with Express and its middleware, you must use body-parser to get request.body, the key point is get it correctly :)

Use body-parser.text() even when Shopify sends you JSON data (application/json).

Replace line 10 with Buffer.from(request.body) since the constructor is deprecated.

@pritisolanki
Copy link

It is not working for me . The generated digest and received headers are different.. @tiendq can you please elaborate how to use body-parser.text() in above code ?

more details here - https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/verify-shopify-webhook-integrity-node-express-js-512656

@Kushan-
Copy link

Kushan- commented Jul 19, 2018

    let jsonString = JSON.stringify(req.body)
   
    let digest = crypto.createHmac("sha256", SECRET)
        .update(jsonString, 'utf8','hex')
        .digest('base64');
    console.log("digest->"+digest)

I hope this will help.

@mitchellporter
Copy link

@Kushan-'s solution works perfectly, while Shopify's own express middleware did not.. but they may be modifying the request somewhere... not sure, but this works.

@guillaumegarcia13
Copy link

+1 for @tiendq's solution
Somehow, using body-parser.json() blocked the processing of the request.

this.express = express()
    .use(rawBodyGetter)
    .use(bodyParser.text())
//  .use(bodyParser.json())
    .use(bodyParser.urlencoded({ extended: true }))
const checkWebHookIntegrity = async (req: any, res: any, next: any) => {
    let raw;

    try {
        raw = await rawBody(req);

        const digest = crypto.createHmac('sha256', CONFIG.SHOPIFY_WEBHOOK_SECRET)
                             .update(raw)
                             .digest('base64');
        if (digest === req.headers['x-shopify-hmac-sha256']) {
            req.body = JSON.parse(raw.toString('utf-8'));
            next();
        } else {
            console.log('Error with request', os.EOL, req.body);
            return res.status(401).send();
        }
    } catch(e) {
        console.log(e);
        return res.status(500).send();
    }
};

@TopoR1
Copy link

TopoR1 commented Jul 16, 2019

@Kushan- Thanks you!

@wrsalex
Copy link

wrsalex commented Jan 27, 2020

I tried this. But not working. Am I doing anything wrong?

    let jsonString = JSON.stringify(req.body);

    const generated_hash = crypto
        .createHmac('sha256', secret)
        .update(jsonString,'utf8','hex')
        .digest('base64');

@Kushan-
Copy link

Kushan- commented Jan 28, 2020

It's been a long time i worked with shopify api, perhaps, uppercase secret might help, what error you are getting?

@wrsalex
Copy link

wrsalex commented Jan 28, 2020

thank you for your reply. tried the uppercase approach. did not work.

got this error
0|wmbi | HASH COMPARE FAILED - Unable to verify request HMAC

0|wmbi  | POST /webhook/ads-hkg - - ms - -
0|wmbi  | { sp_hmac: 'aAwa1U3uPwRl4fJVLmxuLkaKMiyZRnCwlHMl3q8iDxI=',
0|wmbi  |   sp_topic: 'orders/create',
0|wmbi  |   sp_shopDomain: 'ads-api-testing.myshopify.com' }
0|wmbi  | HASH COMPARE FAILED - Unable to verify request HMAC
0|wmbi  | generated_hash: iiKQ9W/kInYASnwL+j2oG2cjbHBFj9CqrMlyd76yaew=
0|wmbi  | sp_hmac: aAwa1U3uPwRl4fJVLmxuLkaKMiyZRnCwlHMl3q8iDxI=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment