- Newest
- Most votes
- Most comments
The issue you're experiencing with AWS API Gateway and Lambda, where the event.body is undefined when receiving a Shopify webhook, could be due to a few potential reasons:
-
API Gateway Configuration: Make sure your API Gateway is set up correctly to pass the request body to Lambda. For HTTP APIs, you should use the Lambda proxy integration, which automatically passes the entire request to your Lambda function.
-
Lambda Function Code: Ensure your Lambda function is properly parsing the event object. With Lambda proxy integration, the body is typically available as a string in event.body. You may need to parse this string if it's JSON data.
-
Content Type and Encoding: Shopify webhooks typically send data as application/json. Ensure your API Gateway is configured to handle this content type. Also, check if the body is base64 encoded, which might require decoding in your Lambda function.
-
API Gateway Payload Format: For HTTP APIs, check the payload format version in your API Gateway settings. Version 2.0 structures the event differently from version 1.0.
-
Request Size Limits: Verify that the webhook payload isn't exceeding API Gateway's size limits.
-
CORS Settings: If applicable, ensure CORS settings are properly configured in API Gateway.
To troubleshoot:
- Double-check your API Gateway configuration, ensuring it's set to proxy all requests to Lambda.
- In your Lambda function, log the entire event object to see what's being received.
- Test with a simple payload using tools like Postman or cURL to isolate if it's a Shopify-specific issue.
- Review your Lambda function's IAM role to ensure it has necessary permissions.
- Consider using API Gateway's request mapping templates to explicitly map the body if proxy integration isn't working as expected.
If the issue persists after these checks, you may need to review your API Gateway and Lambda configurations in detail or consider reaching out to AWS support for further assistance.
Sources
AWS Gateway: API does not respond in Postman | AWS re:Post
Community | Demystifying AWS Lambda: Deep Dive into Serverless Computing
Creating api gateway api from openapi specification with lambda integration does not create triggers | AWS re:Post
The most likely causes of this issue are:
- Missing request body mapping template in API Gateway
- Incorrect content handling configuration
- Payload format version mismatch between API Gateway and Lambda
Verification Steps
1. Check your API Gateway Integration Type
For HTTP APIs:
- Ensure you're using AWS_PROXY integration type
- Verify payload format version is set to 2.0 (default for HTTP APIs)
2. Update Lambda Function to Handle HTTP API Format
exports.handler = async (event) => { console.log('Event:', JSON.stringify(event)); // For HTTP API (payload v2.0) let body; try { // Body might be a string that needs parsing body = event.body ? JSON.parse(event.body) : null; } catch (e) { body = event.body; // Use as-is if parsing fails } console.log('Parsed body:', JSON.stringify(body)); // Your processing logic here return { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'Webhook received' }) }; };
3. API Gateway Configuration Check
For HTTP APIs:
- Go to API Gateway console
- Select your API
- Go to Routes → Select your POST route
- Check the Integration details
- Ensure "Content handling" is set appropriately (if available)
4. Enable Binary Media Types (if needed)
If Shopify is sending data in a format that API Gateway treats as binary:
- Go to API Gateway console
- Select your API → Settings
- Under "Binary media types", add:
application/json
- Deploy your API again
5. Test with a Simple cURL Command
curl -X POST \ -H "Content-Type: application/json" \ -d '{"test":"data"}' \ https://your-api-gateway-url
Relevant content
- asked 2 years ago