Fetching Hubspot deal data using API integration

I’m working on pulling Hubspot deal info into Google Sheets. I followed a guide online but hit a snag. When I run the script, I get this error: “Exception: The number of columns in the data does not match the number of columns in the range. The data has 0 but the range has 2.”

Here’s a simplified version of what I’m trying:

function fetchHubspotDeals() {
  const API_KEY = 'your_api_key_here';
  const API_ENDPOINT = 'https://api.hubapi.com/deals/v1/deal/paged';

  const options = {
    headers: { Authorization: `Bearer ${API_KEY}` },
    muteHttpExceptions: true
  };

  const response = UrlFetchApp.fetch(API_ENDPOINT, options);
  const data = JSON.parse(response.getContentText());

  // Process and write data to sheet
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange(1, 1, data.deals.length, 2).setValues(data.deals.map(deal => [deal.id, deal.properties.amount.value]));
}

Any ideas on what might be causing this error or how to fix it? I’m pretty new to API stuff, so I might be missing something obvious. Thanks for any help!

Hey there, I’ve dealt with similar Hubspot API issues before. The error you’re seeing usually occurs when the data structure doesn’t match what you expect. In this case, you might be receiving an empty or unexpected response from Hubspot. I suggest first logging the response to see what the API is returning. That’ll help you understand if the problem is with the API itself or with how you’re processing the data.

Also, verify your API key permissions and consider adding error handling to check if data.deals exists and has the expected length before setting values. This approach can help pinpoint whether the issue is due to an API limitation or data structure changes.

yo Luke, ive messed with HubSpot API before. looks like ur assuming data.deals exists and has stuff in it. maybe try logging the response first to see whats actually coming back? could be empty or formatted different than u expect. also double check ur API key has right permissions. good luck man!