What is @hubspot/api-client?
The @hubspot/api-client npm package is a comprehensive client library for interacting with HubSpot's APIs. It allows developers to easily integrate HubSpot's CRM, marketing, sales, and service functionalities into their applications.
What are @hubspot/api-client's main functionalities?
CRM
This feature allows you to manage CRM functionalities such as creating, reading, updating, and deleting contacts. The code sample demonstrates how to create a new contact in HubSpot.
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ apiKey: 'your-api-key' });
async function createContact() {
const contactObj = { properties: { firstname: 'John', lastname: 'Doe', email: 'john.doe@example.com' } };
const createResponse = await hubspotClient.crm.contacts.basicApi.create(contactObj);
console.log(createResponse);
}
createContact();
Marketing
This feature allows you to manage marketing functionalities such as creating and managing email campaigns. The code sample demonstrates how to create a new email campaign in HubSpot.
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ apiKey: 'your-api-key' });
async function createEmailCampaign() {
const emailCampaignObj = { name: 'New Campaign', subject: 'Hello World', fromName: 'Your Company', fromEmail: 'info@yourcompany.com' };
const createResponse = await hubspotClient.marketing.emailCampaigns.create(emailCampaignObj);
console.log(createResponse);
}
createEmailCampaign();
Sales
This feature allows you to manage sales functionalities such as creating, reading, updating, and deleting deals. The code sample demonstrates how to create a new deal in HubSpot.
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ apiKey: 'your-api-key' });
async function createDeal() {
const dealObj = { properties: { dealname: 'New Deal', amount: '1000', dealstage: 'qualifiedtobuy' } };
const createResponse = await hubspotClient.crm.deals.basicApi.create(dealObj);
console.log(createResponse);
}
createDeal();
Service
This feature allows you to manage service functionalities such as creating, reading, updating, and deleting tickets. The code sample demonstrates how to create a new support ticket in HubSpot.
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ apiKey: 'your-api-key' });
async function createTicket() {
const ticketObj = { properties: { subject: 'Support Request', content: 'Need help with product', hs_pipeline: '0', hs_pipeline_stage: '1' } };
const createResponse = await hubspotClient.crm.tickets.basicApi.create(ticketObj);
console.log(createResponse);
}
createTicket();
Other packages similar to @hubspot/api-client
node-hubspot
The node-hubspot package is another client library for interacting with HubSpot's APIs. It offers similar functionalities to @hubspot/api-client but may have a different API design and feature set.
hubspot-api
The hubspot-api package provides a simplified interface for accessing HubSpot's APIs. It is designed to be easy to use and may be a good alternative for developers looking for a more lightweight solution.
hubspot-api-nodejs
NodeJS v3 HubSpot API SDK(Client) files and sample apps
Sample Applications can be found in sample-apps folder
Installing
npm install @hubspot/api-client
Instantiate client
const hubspot = require('hubspot')
const hubspotClient = new hubspot.Client({ apiKey: 'abc'})
You can also authenticate via token:
const hubspotClient = new hubspot.Client({ accessToken: YOUR_ACCESS_TOKEN })
To change the base path:
const hubspotClient = new hubspot.Client({ accessToken: YOUR_ACCESS_TOKEN, basePath: 'https://some-url' })
To add custom headers to all request:
const hubspotClient = new hubspot.Client({ accessToken: YOUR_ACCESS_TOKEN, defaultHeaders: { "My-header": "test-example" } })
If you're an app developer, you can also instantiate a client and obtain a new accessToken with your app
details and a refresh_token:
const hubspotClient = new hubspot.Client();
return hubspotClient.oauth.defaultApi.createToken(
'refresh_token',
undefined,
undefined,
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REFRESH_TOKEN
)
.then(results => {
console.log(results.body);
hubspotClient.setAccessToken(results.body.accessToken)
return hubspotClient.crm.companies.basicApi.getPage()
})
Bottleneck is used for rate limiting. To override the default settings, pass a limiterOptions
object when instantiating the client. Bottleneck options can be found here.
Please note that Apps using OAuth are only subject to a limit of 100 requests every 10 seconds. Limits related to the API Add-on don't apply.
Default settings for the limiter are:
const DEFAULT_LIMITER_OPTIONS = {
minTime: 1000 / 9,
maxConcurrent: 5,
}
It's possible to turn off rate limiting:
const hubspotClient = new hubspot.Client({
accessToken: YOUR_ACCESS_TOKEN,
useLimiter: false,
})
It's possible to turn on retry for failed requests with statuses 429 or 5xx. To turn on/off Configurable Retries use numberOfApiCallRetries option on Client instance creation.
numberOfApiCallRetries could be set to a numberfrom 0 - 6. If numberOfApiCallRetries is set to a number greater than 0 it means that if any API Call receives ISE5xx this call will be retried after a delay 200 * retryNumber ms and if 429 (Rate limit is exceeded) is returned for "TEN_SECONDLY_ROLLING" the call will be retried after a delay 10 sec. Number of retries will not exceed numberOfApiCallRetries value.
const hubspotClient = new hubspot.Client({
accessToken: YOUR_ACCESS_TOKEN,
numberOfApiCallRetries : NumberOfRetries.Six
})
It's possible to create client instance with Interceptors functions which would be called and awaited before request is made:
const hubspotClient = new hubspot.Client({
accessToken: YOUR_ACCESS_TOKEN,
interceptors: [interceptorFn1, interceptorFn2]
})
Usage
All methods return a promise. The success includes the serialized to JSON body and response objects. Use the API method via:
hubspotClient.crm.contacts.basicApi.getPage(limit, after, properties, associations, archived)
.then(results => {
console.log(results.body)
})
.catch(err => {
console.error(err)
})
{EXAMPLE} Create Contact, Company and associate created objects
const contactObj = {
properties: {
firstname: yourValue,
lastname: yourValue
}
};
const companyObj = {
properties: {
domain: yourValue,
name: yourValue
}
};
const hubspotClient = new hubspot.Client({ apiKey: YOUR_API_KEY });
const createContactResponse = await hubspotClient.crm.contacts.basicApi.create(contactObj)
const createCompanyResponse = await hubspotClient.crm.companies.basicApi.create(companyObj)
await hubspotClient.crm.companies.associationsApi.createAssociation(createCompanyResponse.body.id, 'contacts', createContactResponse.body.id)
OAuth
Obtain your authorization url
const client_id = 'your_client_id'
const redirect_uri = 'take_me_to_the_ballpark'
const scope = 'some scopes'
const uri = hubspotClient.oauth.getAuthorizationUrl(client_id, redirect_uri, scope)
Obtain an access token from an authorization_code
return hubspotClient.oauth.defaultApi.createToken(
'authorization_code',
code,
YOUR_REDIRECT_URI,
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
).then(...)
Not wrapped endpoint(s)
It is possible to access the hubspot request method directly,
it could be handy if client doesn't have implementation for some endpoint yet.
Exposed request method benefits by having all configured client params.
hubspotClient.apiRequest({
method: 'PUT',
path: '/some/api/not/wrapped/yet',
body: { key: 'value' },
})
Typescript
You may use this library in your Typescript project via:
import Client from 'hubspot';
const hubspotClient = new Client({ apiKey: YOUR_API_KEY });
License
MIT