I am new with the HubSpot API and was working with the sample code from the HubSpot Academy tutorials, and would like to narrow down the API call to the ZIP code. I would like to know, how would I do this using Node.js? Could you please help me out here? Thanks.
Hey @ChrisSosa1337, I’m not super familiar with the exact tutorial you are referring to but if you are hoping to retrieve a specific property for a company you can do something like this:
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});
const companyId = "8109465573"; //ID of the company
const properties = ["zip"]; // Add whatever properties you'd like
const propertiesWithHistory = undefined;
const associations = undefined;
const archived = false;
const idProperty = undefined;
try {
const apiResponse = await hubspotClient.crm.companies.basicApi.getById(companyId, properties, propertiesWithHistory, associations, archived, idProperty);
console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
Which would return:
{
"id": "8109465573",
"properties": {
"createdate": "2023-07-20T13:25:52.999Z",
"hs_lastmodifieddate": "2023-07-20T13:25:55.548Z",
"hs_object_id": "8109465573",
"zip": "11040"
},
"createdAt": "2023-07-20T13:25:52.999Z",
"updatedAt": "2023-07-20T13:25:55.548Z",
"archived": false
}
This is my current code:
const express = require('express');
const axios = require('axios');
const hubspot = require('@hubspot/api-client');
const app = express();
const private_app_token = 'app_token'
app.get('/companies', async (req, res) => {
const companies = 'https://api.hubspot.com/crm/v3/objects/companies';
const headers = {
Authorization : `Bearer ${private_app_token}`,
'Content-Type': 'application/json'
}
const companyId = "8109465573"; //ID of the company
const properties = ["zip"]; // Add whatever properties you'd like
const propertiesWithHistory = undefined;
const associations = undefined;
const archived = false;
const idProperty = undefined;
try {
const apiResponse = await hubspotClient.crm.companies.basicApi.getById(companyId, properties, propertiesWithHistory, associations, archived, idProperty);
console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
});
app.listen(3000, () => console.log('Listening on http://localhost:3000'));
I am new to this. Anything I am doing wrong? I type http://localhost:3000/companies into my browser and I get the error “hubspotClient is not defined”. Could you please help me here?
The example I am using is from this course:
Introduction to the HubSpot APIs
I am new to the HubSpot APIs. BTW this is a private app. From your example code, how would I set up Port 3000? It appears your code isn’t using Express. I tried running your code and I get an error that await must be in an async function. Wrapping the code in an async block, nothing happens. I should perhaps view some of the tutorials with the HubSpot CLI to do what I need.