Skip to content

Instantly share code, notes, and snippets.

@picsoung
Created October 15, 2024 11:21
Show Gist options
  • Save picsoung/b7f760dafdc383b0866720390a151e8f to your computer and use it in GitHub Desktop.
Save picsoung/b7f760dafdc383b0866720390a151e8f to your computer and use it in GitHub Desktop.
typeform webhook handler for google cloudfunction
const functions = require('@google-cloud/functions-framework');
const crypto = require('crypto');
const SECRET_TOKEN = "abc123"
functions.http('webhookHandler', (req, res) => {
// Ensure the request body is available as a string
const rawBody = req.rawBody ? req.rawBody.toString() : JSON.stringify(req.body);
const signature = req.headers['typeform-signature'];
const isValid = verifySignature(signature, rawBody);
if (isValid) {
// Process the webhook payload here
console.log('Webhook signature verified');
res.status(200).send('Webhook received and verified');
} else {
console.error('Invalid webhook signature');
res.status(403).send('Invalid signature');
}
});
const verifySignature = function (receivedSignature, payload) {
const hash = crypto
.createHmac('sha256', SECRET_TOKEN)
.update(payload)
.digest('base64');
return receivedSignature === `sha256=${hash}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment