Node.js HubSpot Timeline API throws 'eventsApi' undefined error when creating events

I’m working with the HubSpot Timeline API in Node.js to create timeline events, but I keep running into this error:

TypeError: Cannot read properties of undefined (reading 'eventsApi')

The strange thing is that the exact same code works perfectly when I test it directly on the HubSpot developer documentation page. However, when I run it on my local server, it fails with this error. I’m using a valid access token and have the @hubspot/api-client package installed (version 6.0.1-beta3). I’ve also tried downgrading to older versions but get the same result.

Here’s my code:

async function createTimelineEvent() {
  const hubspot = require('@hubspot/api-client');
  
  const client = new hubspot.Client({"accessToken": "my-access-token"});
  
  const tokenData = {
    "event-created": "",
    "param1": "123",
    "param2": "xyz"
  };
  
  const additionalInfo = {
    "details": [
      {
        "field": "What's your favorite color?",
        "value": "Blue!"
      },
      {
        "field": "Where do you live?",
        "value": "Mars!"
      }
    ]
  };
  
  const iframeConfig = {
    "linkLabel": "View Profile",
    "headerLabel": "User Dashboard",
    "url": "https://example.com/user/profile",
    "width": 800,
    "height": 500
  };
  
  const eventData = {
    eventTemplateId: "9876543",
    email: "[email protected]",
    tokens: tokenData,
    extraData: additionalInfo,
    timelineIFrame: iframeConfig
  };
  
  try {
    const result = await client.crm.timeline.events.eventsApi.create(eventData);
    console.log(JSON.stringify(result.body, null, 2));
  } catch (error) {
    error.message === 'HTTP request failed'
      ? console.error(JSON.stringify(error.response, null, 2))
      : console.error(error)
  }
}

createTimelineEvent();

Any ideas what might be causing this issue with the eventsApi being undefined?

quick fix - try importing the client differently. I hit this same error cuz the module exports changed. Use const hubspotClient = require('@hubspot/api-client') then const client = new hubspotClient.Client({accessToken: 'token'}). that beta version’s probably causing it too - switch to stable 5.x until they get v6 working right.

You’ve got a version mismatch between your local setup and what HubSpot’s docs show. I hit the same thing with their Timeline API last year. Your beta version (6.0.1-beta3) has known issues with API endpoint structures. The docs probably reference a stable release that handles eventsApi differently. Here’s what fixed it for me: check your package.json is pulling the right version, then nuke node_modules and reinstall with a stable version. Try npm install @hubspot/api-client@^5.0.0 to get back to something that works. Beta releases are full of these undefined property bugs since they’re still finalizing the API structure.

Had the same issue with HubSpot’s client library - it’s probably a mixing initialization patterns problem. Instead of your current setup, try destructuring the Client directly: const { Client } = require('@hubspot/api-client'); then const client = new Client({accessToken: "your-token"});. Also check your Node.js version matches what HubSpot’s docs are running on. I hit weird inconsistencies when I was on an older Node version locally. That beta version might be causing issues too since their docs probably use the stable release.

Had this exact issue a few months back when migrating to v6 of the HubSpot client. The timeline API structure changed big time in version 6.x. That eventsApi path you’re using? That’s from the old v5 syntax. In v6, you need client.crm.timeline.eventsApi.create() instead of client.crm.timeline.events.eventsApi.create(). See how there’s no events in the middle? HubSpot’s docs probably work because they’re still showing v5 examples or running a different version. I’d check which version their docs actually reference. Also, some property names in your eventData object might need tweaking for v6. I had to change a few field names when I switched.