HubSpot API Client for Node.js

1.3.1 · active · verified Tue Apr 21

The `node-hubspot-api` package provides a Node.js wrapper for interacting with the HubSpot API. Currently at version 1.3.1, this library simplifies common API operations such as retrieving, creating, and updating contacts, and fetching deals. It employs a promises-based architecture for asynchronous operations, returning data in `.then()` and catching errors in `.catch()`. The release cadence is moderate, with several minor releases since v1.0.0, indicating ongoing maintenance and incremental feature additions, such as new methods for getting contacts by email or ID, and bug fixes. Its primary differentiator is its straightforward, idiomatic JavaScript interface to the HubSpot CRM platform, abstracting the underlying HTTP complexities. This wrapper directly exposes common HubSpot API endpoints through a convenient object structure, focusing on essential CRM entities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the HubSpot API client with an API key and perform basic operations like fetching a list of contacts and a single contact by email.

import NodeHubSpotApi from 'node-hubspot-api';

// It is crucial to manage API keys securely, e.g., via environment variables.
const HUBSPOT_API_KEY = process.env.HUBSPOT_API_KEY ?? 'YOUR_SECRET_API_KEY'; 

const api = new NodeHubSpotApi(HUBSPOT_API_KEY);

// Example: Get all contacts with specific properties
api.contacts.getAll({
  count: 2, // Fetch a small number for a quick example
  property: [
    'firstname', 'lastname', 'email', 'company'
  ],
  showListMemberships: false
})
.then(response => {
  console.log('Successfully fetched contacts:');
  if (response.data && response.data.contacts) {
    response.data.contacts.forEach(contact => {
      console.log(`- ${contact.properties.firstname?.value} ${contact.properties.lastname?.value} (${contact.properties.email?.value})`);
    });
  } else {
    console.log('No contacts found or unexpected response structure.');
  }
})
.catch(error => {
  console.error('Error fetching contacts:', error.response ? error.response.data : error.message);
});

// Example: Get a contact by email
const targetEmail = 'test@example.com'; // Replace with a real email for testing
api.contacts.getContactByEmail(targetEmail, {
  property: ['firstname', 'lastname', 'email'],
})
.then(response => {
  console.log(`\nSuccessfully fetched contact by email "${targetEmail}":`);
  console.log(response.data);
})
.catch(error => {
  if (error.response && error.response.status === 404) {
    console.warn(`\nContact with email "${targetEmail}" not found.`);
  } else {
    console.error(`\nError fetching contact by email "${targetEmail}":`, error.response ? error.response.data : error.message);
  }
});

view raw JSON →