Back

Step by Step Guide to Building a HubSpot API Integration in JS

Jul 17, 20245 minute read

Hey there, fellow developer! Ready to dive into the world of HubSpot API integration? Buckle up, because we're about to embark on a journey that'll have you wielding the power of HubSpot's API like a pro. We'll be using the @hubspot/api-client package, so get ready for some smooth sailing.

Prerequisites

Before we jump in, make sure you've got:

  • Node.js and npm installed (you're a dev, so I'm sure you've got this covered)
  • A HubSpot developer account (if you don't have one, go grab it – it's free!)
  • API key or OAuth credentials (we'll focus on API key for simplicity)

Setting Up the Project

Let's get this party started:

mkdir hubspot-api-project cd hubspot-api-project npm init -y npm install @hubspot/api-client

Boom! You're ready to roll.

Authentication

We'll use API key authentication because it's quick and dirty. In a production environment, you'd want to use OAuth 2.0, but let's keep it simple for now.

Creating the HubSpot Client

Time to initialize our HubSpot client:

const hubspot = require('@hubspot/api-client'); const hubspotClient = new hubspot.Client({ apiKey: 'YOUR_API_KEY' });

Replace 'YOUR_API_KEY' with your actual API key, and you're good to go!

Basic API Operations

Let's run through the CRUD operations real quick:

Fetching Data (GET)

async function getContact(id) { const contact = await hubspotClient.crm.contacts.basicApi.getById(id); console.log(contact); }

Creating Data (POST)

async function createContact(properties) { const contact = await hubspotClient.crm.contacts.basicApi.create({ properties }); console.log('Created contact:', contact); }

Updating Data (PATCH)

async function updateContact(id, properties) { const contact = await hubspotClient.crm.contacts.basicApi.update(id, { properties }); console.log('Updated contact:', contact); }

Deleting Data (DELETE)

async function deleteContact(id) { await hubspotClient.crm.contacts.basicApi.archive(id); console.log('Contact deleted'); }

Working with Specific HubSpot APIs

The @hubspot/api-client package gives you access to a ton of APIs. Here's a quick look at a few:

Contacts API

const contacts = await hubspotClient.crm.contacts.getAll();

Companies API

const companies = await hubspotClient.crm.companies.getAll();

Deals API

const deals = await hubspotClient.crm.deals.getAll();

Custom Objects API

const schema = await hubspotClient.crm.schemas.coreApi.getAll();

Error Handling and Best Practices

Always wrap your API calls in try/catch blocks:

try { const contact = await hubspotClient.crm.contacts.basicApi.getById(id); } catch (error) { console.error('Error:', error.message); }

And don't forget about rate limits! The client handles most of this for you, but it's good to be aware.

Advanced Topics

Want to level up? Look into:

  • Webhooks for real-time data
  • Batch operations for handling bulk data

Conclusion

And there you have it! You're now armed with the knowledge to build some seriously cool HubSpot integrations. Remember, the @hubspot/api-client package is your friend – it's got your back with most of the heavy lifting.

Want to dive deeper? Check out the HubSpot Developer Docs for more info.

Now go forth and code, you magnificent developer, you!