Error: KeyError Data
Issue
Connection syncs fail, and the API response returns the following error:
KeyError data
Environment
Connector: AWS Lambda
Resolution
To troubleshoot this issue, follow these steps:
Review the Lamba logs in CloudWatch.
i. Go to the AWS Console.
ii. Navigate to Lambda and select the relevant function.
iii. Select the Monitor tab, then click View CloudWatch logs.
iv. Find the log stream for the failure timestamp and review the API response.
Validate the Lambda function response.
i. Make sure the
api_responsefunction in your Lambda script can handle unexpected API responses.ii. Use
response.get('data')instead ofresponse['data']to avoid aKeyError.Check for third party API changes.
i. Confirm whether the vendor changed the API response structure.
ii. If the structure changed, update the code to accommodate the new structure.
Add a
try-exceptblock around the code that accesses thedatafield, and log the full response when the field is missing.If the API structure changed unexpectedly, contact the vendor for clarification.
Test the function locally or in a development environment to confirm that the error is resolved.
Cause
This issue occurs when the third-party API returns an unexpected response structure. For example, the response may be missing the expected data field due to an API change, a temporary vendor issue, or an unhandled edge case.
When this happens, the Lambda function tries to access the missing field using response['data']. Since the field does not exist, the function raises a KeyError, causing the sync to fail.