Getting undefined property error when accessing HubSpot API response data

I’m working with HubSpot workflows and trying to use their custom code feature. Every time I run my script I get an error about properties being undefined. Here’s what happens:

const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) => {
    const client = new hubspot.Client({
        accessToken: process.env.TOKEN_SECRET
    });
    
    client.crm.deals.basicApi.getById(event.object.objectId, ["created_date", "amount"])
    .then(response => {
        console.log("API call successful");
        
        let createdDate = response.body.properties.created_date;
        let dealAmount = response.body.properties.amount;
        
        let processedDate = new Date(createdDate);
        let finalAmount = parseInt(dealAmount) * 1.1;
        
        callback({
            outputFields: {
                processed_date: processedDate.getTime(),
                final_amount: finalAmount
            }
        });
    })
    .catch(error => {
        console.error(error);
    });
};

The error message shows: TypeError: Cannot read properties of undefined (reading 'properties') and it points to the line where I try to access response.body.properties. The console log shows the API call works but then it fails right after. What could be causing the response structure to be different than expected?

The issue’s likely with the response structure. Newer HubSpot SDK versions access properties directly from response.properties instead of response.body.properties. I ran into this exact same problem when migrating code from older API versions.

Try changing your property access to:

let createdDate = response.properties.created_date;
let dealAmount = response.properties.amount;

Also check if you’re using the right API method. The basicApi.getById method signature changed in recent SDK updates. Make sure your SDK version matches whatever documentation you’re following - HubSpot’s made breaking changes to their response format between major versions.

log the full response object first - see what’s actually coming back. hubspot’s api sometimes returns data differently than their docs show. drop console.log(JSON.stringify(response, null, 2)) before you access any properties. thisll show you the real response format you’re working with.