Skip to content

Instantly share code, notes, and snippets.

@dmathewwws
Created March 29, 2025 06:53
Show Gist options
  • Save dmathewwws/d3d56529313cb2959bc2a7ec62792a5a to your computer and use it in GitHub Desktop.
Save dmathewwws/d3d56529313cb2959bc2a7ec62792a5a to your computer and use it in GitHub Desktop.
Lambda function saving Webhook data your receive from Taddy API to a queue
import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';
// Environment variables
const QUEUE_URL = process.env.QUEUE_URL;
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
const AWS_REGION = process.env.AWS_REGION;
if (!QUEUE_URL || !WEBHOOK_SECRET || !AWS_REGION) {
throw new Error('Required environment variables are not set');
}
const sqs = new SQSClient({ region: AWS_REGION });
// Send message to SQS with improved error handling
const sendToSQS = async (message) => {
const params = {
QueueUrl: QUEUE_URL,
MessageBody: JSON.stringify(message)
};
try {
const command = new SendMessageCommand(params);
return await sqs.send(command);
} catch (error) {
console.error('Error sending message to SQS:', error);
throw new Error('Failed to send message to queue');
}
};
export const handler = async (event) => {
try {
// Validate HTTP method
if (event.requestContext?.http?.method !== 'POST') {
return {
statusCode: 405,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ error: 'Method Not Allowed' })
};
}
// Validate webhook secret
const headers = event.headers || {};
const providedWebhookSecret = headers['X-TADDY-WEBHOOK-SECRET'] || headers['x-taddy-webhook-secret'];
if (providedWebhookSecret !== WEBHOOK_SECRET) {
return {
statusCode: 401,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ error: 'Invalid webhook secret' })
};
}
// Process payload
const result = await sendToSQS(event.body);
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: 'Webhook processed successfully',
messageId: result.MessageId
})
};
} catch (error) {
console.error('Error processing webhook:', error);
return {
statusCode: error.statusCode || 500,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
error: error.message || 'Internal server error'
})
};
}
};
// Notes:
// Add the env variables QUEUE_URL (the url to the SQS Queue you want to add data from Taddy API to) and WEBHOOK_SECRET (Taddy's webhook secret for security reasons) to your lambda function (AWS_REGION is auto-configured by AWS, so it is not necessary to set)
// Make sure your lambda function has the necessary IAM permissions to send messages to the SQS queue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment