I’m hitting a weird issue with a Node.js Lambda function that consumes Genesys Cloud webhooks. The handler keeps crashing with TypeError: Cannot read properties of undefined (reading 'id').
The docs say the event structure is standard, but the payload I’m seeing in CloudWatch looks like this:
{
"body": "{\"id\": \"123\", \"routing\": {\"queueId\": \"456\"}}",
"headers": { "content-type": "application/json" }
}
My code assumes event.body is already an object:
exports.handler = async (event) => {
const data = JSON.parse(event.body);
console.log(data.id);
// ... do stuff
};
Wait, no. I’m parsing it. The error is happening before that. Oh, right. Sometimes the body is null if the trigger is invalid. But the console logs show the JSON string is there.
I’ve tried checking event.body for null. I’ve tried JSON.parse(event.body || '{}'). It still fails intermittently. Is Genesys sending a weird format sometimes? Or is this an API Gateway integration issue where the body isn’t being passed correctly to the Lambda?
The documentation for the webhook event format is pretty sparse on the Lambda side. It just says “the payload is sent as JSON”. It doesn’t mention the wrapper object from API Gateway.
I’m using the default API Gateway trigger. No custom mapping templates. Just straight through.
Why is the body undefined sometimes? The logs are confusing. One minute it works, the next it’s null. I’ve added a try-catch but it’s just swallowing the error. I need to know why the structure changes.