Skip to content

Instantly share code, notes, and snippets.

@abhinavjonnada82
Last active October 15, 2024 05:19
Show Gist options
  • Save abhinavjonnada82/91d6f6dd6bc91ef5eefee30a28771c7d to your computer and use it in GitHub Desktop.
Save abhinavjonnada82/91d6f6dd6bc91ef5eefee30a28771c7d to your computer and use it in GitHub Desktop.
app.post('/webhook/typeform', async (request, response) => {
console.log('Received webhook request');
const signature = request.headers['typeform-signature'];
if (!signature) {
console.error('No Typeform signature found in headers');
return response.status(400).send('No signature provided');
}
const payload = request.body.toString();
const isValid = verifySignature(signature, payload);
if (isValid) {
console.log('Signature verified successfully');
// Parse the JSON payload
const jsonPayload = JSON.parse(payload);
console.log('Webhook payload:', jsonPayload);
// Process the webhook payload here temporarily need to UPDATE!!!
if (db && db.collection) {
try {
await db.collection('EventsEngine').add(jsonPayload);
console.log('Data added to EventsEngine collection');
} catch (error) {
console.error('Error adding data to EventsEngine:', error);
}
} else {
console.error('Database or collection not properly initialized');
}
response.status(200).send('Webhook received successfully');
} else {
console.error('Invalid webhook signature');
response.status(403).send('Invalid signature');
}
});
const verifySignature = function (receivedSignature, payload) {
const secret = process.env.SECRET_TOKEN;
const hash = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('base64');
console.log('Calculated hash:', hash);
console.log('Received signature:', receivedSignature);
return receivedSignature === `sha256=${hash}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment