I’ve got a simple Node.js Lambda function set up to catch routing.queueMember.added events from Genesys Cloud. The idea is straightforward: receive the webhook, parse the JSON, and push the interaction ID to an SQS queue for later processing.
The problem is that the Lambda keeps crashing with a SyntaxError: Unexpected token o in JSON at position 1 when I try to parse the body field from the event object.
Here’s the relevant bit of my handler code:
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event));
// The webhook payload is in event.body
const payload = JSON.parse(event.body);
// Process payload...
return { statusCode: 200, body: 'OK' };
};
When I test this locally with a raw JSON string, it works fine. But when Genesys Cloud actually hits the endpoint, event.body isn’t a string. It’s already an object. I checked the CloudWatch logs, and event.body looks like this:
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"type": "routing.queueMember.added",
"timestamp": "2023-10-27T10:15:30.000Z",
"data": {
"conversationId": "conv-123",
"memberId": "user-456"
}
}
Wait, no. Actually, when I log typeof event.body, it says object. But if I try to access event.body.id, it’s undefined. It seems like the body is double-wrapped or maybe the Lambda integration is decoding it before it hits my handler.
I’m using the serverless framework to deploy. My serverless.yml has:
functions:
webhookHandler:
handler: handler.handler
events:
- http:
path: /webhooks/genesys
method: post
cors: true
Is there a setting in Genesys Cloud or the API Gateway that changes how the body is passed? I’ve tried adding contentHandling: CONVERT_TO_STRING but that just broke it more. I need to know how to correctly extract the interaction ID from this payload without hitting that JSON parse error. The logs are messy and I can’t figure out if it’s an encoding issue or just me missing a basic Lambda event structure detail.