Javascript / node.js wrapper for the HubSpot API
npm install hubspot
const Hubspot = require('hubspot')
const hubspot = new Hubspot({
apiKey: 'abc',
checkLimit: false // (Optional) Specify whether or not to check the API limit on each call. Default: true
})
You can also authenticate via token:
const hubspot = new Hubspot({ accessToken: YOUR_ACCESS_TOKEN })
To change the base url:
const hubspot = new Hubspot({ accessToken: YOUR_ACCESS_TOKEN, baseUrl: 'https://some-url' })
If you're an app developer, you can also instantiate a client with your app details and a refresh_token and obtain a new accessToken:
const hubspot = new Hubspot({
clientId: ...,
clientSecret: ...,
redirectUri: ...,
refreshToken: ...
})
return hubspot.refreshAccessToken()
.then(results => {
console.log(results.access_token)
// this assigns the new accessToken to the client, so your client is ready
// to use
console.log(hubspot.accessToken)
return hubspot.contacts.get()
})
Bottleneck is used for rate limiting. To override the default settings, pass a limiter
object when instantiating the client. Bottleneck options can be found here.
const hubspot = new Hubspot({
apiKey: YOUR_API_KEY,
limiter: {
maxConcurrent: 2,
minTime: 1000 / 9,
}
})
All methods return a promise. The success case includes the returned object from the response. Use the API method via:
hubspot.contacts
.get(options)
.then(results => {
console.log(results)
})
.catch(err => {
console.error(err)
})
const contactObj = {
"properties": [
{ "property": "firstname","value": yourvalue },
{ "property": "lastname", "value": yourvalue }
]
};
const hubspot = new Hubspot({ apiKey: YOUR_API_KEY });
const hubspotContact = await hubspot.contacts.create(contactObj);
Each value must have a semi colon after each value
{ "property": "foo", "value": "value1;value2;value3;value4" }
HubSpot has relatively stringent API limits (40,000 per day by default). To prevent from consuming it all-at-once, this library checks API quotas regularly and will fail requests if the total is too close to the max. By default
hubspot.companies.get(opts)
hubspot.companies.getById(id)
hubspot.companies.getRecentlyCreated(opts)
hubspot.companies.getRecentlyModified(opts)
hubspot.companies.getByDomain(domain)
hubspot.companies.create(data)
hubspot.companies.addContactToCompany(data)
// data = { companyId: 123, contactVid: 123 }
hubspot.companies.getContactIds(id, options)
hubspot.companies.getContacts(id, options)
hubspot.companies.update(id, data)
hubspot.companies.updateBatch(data)
// data = [{ objectId: 123, properties: [] }]
hubspot.companies.delete(id)
hubspot.companies.properties.get(query) // query is optional
hubspot.companies.properties.getByName(name)
hubspot.companies.properties.create(data)
hubspot.companies.properties.update(name, data)
hubspot.companies.properties.upsert(data)
// not an official API, wrapper doing two API calls. Callbacks not supported
// at this time.
hubspot.companies.properties.groups.get(query) // query is optional
hubspot.companies.properties.groups.create(data)
hubspot.companies.properties.groups.update(name, data)
hubspot.companies.properties.groups.upsert(data)
// not an official API, wrapper doing two API calls. Callbacks not supported at
// this time
hubspot.contacts.get(opts)
hubspot.contacts.getByEmail(email)
hubspot.contacts.getByEmailBatch(emails)
hubspot.contacts.getById(id)
hubspot.contacts.getByIdBatch(ids)
hubspot.contacts.getByToken(utk)
hubspot.contacts.update(id, data)
hubspot.contacts.create(data)
hubspot.contacts.createOrUpdateBatch(data)
// data = [{ vid/email: '', properties: [] }]
hubspot.contacts.search(query)
hubspot.contacts.getRecentlyCreated()
hubspot.contacts.getRecentlyModified()
hubspot.contacts.createOrUpdate(email, data)
hubspot.contacts.delete(id)
hubspot.contacts.merge(primaryId, secondaryId)
hubspot.contacts.properties.get()
hubspot.contacts.properties.getByName(name)
hubspot.contacts.properties.create(data)
hubspot.contacts.properties.update(name, data)
hubspot.contacts.properties.upsert(data)
// not an official API, wrapper doing two API calls.
// Callbacks not supported at this time
hubspot.contacts.properties.getGroups()
// => [ {name: '...', displayName: '...'}, ...]
hubspot.contacts.properties.createGroup({ name, displayName })
hubspot.contacts.properties.updateGroup(name, { displayName })
hubspot.contacts.properties.deleteGroup(name)
hubspot.contacts.properties.delete(name)
hubspot.crm.associations.create(data)
hubspot.crm.associations.createBatch(data)
hubspot.crm.associations.delete(data)
hubspot.crm.associations.deleteBatch(data)
// not an official API, wrapper doing two API calls. Callbacks not supported at
// this time
// more opts can be found at https://developers.hubspot.com/docs/methods/pages/get_pages
hubspot.pages.get(opts) // eg: opts = {is_draft: false}
hubspot.deals.get(opts)
hubspot.deals.getRecentlyModified(opts)
hubspot.deals.getRecentlyCreated(opts)
hubspot.deals.getById(id)
hubspot.deals.getAssociated(objectType, objectId, opts)
hubspot.deals.deleteById(id)
hubspot.deals.updateById(id, data)
hubspot.deals.updateBatch(data)
hubspot.deals.create(data)
hubspot.deals.associate(id, objectType, associatedObjectId)
hubspot.deals.removeAssociation(id, objectType, associatedObjectId)
hubspot.deals.properties.get(query) // query is optional
hubspot.deals.properties.getByName(name)
hubspot.deals.properties.create(data)
hubspot.deals.properties.update(name, data)
hubspot.deals.properties.upsert(data)
// not an official API, wrapper doing two API calls. Callbacks not supported at
// this time
hubspot.deals.properties.groups.get(query) // query is optional
hubspot.deals.properties.groups.create(data)
hubspot.deals.properties.groups.update(name, data)
hubspot.deals.properties.groups.upsert(data)
// not an official API, wrapper doing two API calls. Callbacks not supported at
// this time
hubspot.engagements.create(data)
hubspot.engagements.get(opts)
hubspot.engagements.getRecentlyModified(opts)
hubspot.engagements.getAssociated(objectType, objectId, opts)
hubspot.engagements.getCallDispositions()
hubspot.owners.get(opts)
hubspot.pipelines.get(opts)
hubspot.lists.get(opts)
hubspot.lists.getOne(id)
hubspot.lists.create(data)
hubspot.lists.delete(id)
hubspot.lists.getContacts(id, opts)
hubspot.lists.getRecentContacts(id, opts)
hubspot.lists.addContacts(id, contactBody)
hubspot.files.get()
hubspot.files.getOne(id)
hubspot.forms.get(opts)
hubspot.forms.getById(id)
hubspot.forms.getSingleField(guid, fieldname)
hubspot.forms.getSubmissions(guid, opts)
hubspot.forms.create(data)
hubspot.forms.update(id, data)
hubspot.forms.delete(id)
hubspot.forms.submit(portalId, formId, data)
hubspot.subscriptions.get(opts)
hubspot.campaigns.getById()
hubspot.campaigns.get(opts)
hubspot.campaigns.getOne(id)
hubspot.campaigns.events(opts)
hubspot.broadcasts.get(opts)
// setup for timeline events
hubspot.timelines.createEventType(applicationId, userId, data)
hubspot.timelines.updateEventType(applicationId, eventTypeId, data)
hubspot.timelines.createEventTypeProperty(
applicationId,
eventTypeId,
userId,
data,
)
hubspot.timelines.updateEventTypeProperty(
applicationId,
eventTypeId,
propertyId,
data,
)
// creating timeline events
hubspot.timelines.createTimelineEvent(applicationId, eventTypeId, data)
NOTE: From the documentation for createTimelineEvent:
Returns a 204 response on success. Otherwise, you'll receive a 4xx error, with more details about the specific error in the body of the response.
So on success the body is empty or undefined
and you will not get a result
from the resolved promise.
hubspot.emails.sendTransactionalEmail(data)
hubspot.workflows.getAll()
hubspot.workflows.get(workflowId)
hubspot.workflows.enroll(workflowId, email)
hubspot.workflows.unenroll(workflowId, email)
hubspot.workflows.current(contactId)
const params = {
client_id: 'your_client_id',
scope: 'some scopes',
redirect_uri: 'take_me_to_the_ballpark',
}
const uri = hubspot.oauth.getAuthorizationUrl(params)
const hubspot = new Hubspot({
clientId: '',
clientSecret: '',
redirectUri: ''
})
return hubspot.oauth.getAccessToken({
code: 'abc' // the code you received from the oauth flow
}).then(...)
You can also pass the constructor directly as parameters (although with a slightly awkward case change)
const params = {
code: 'abc' // the code you received from the oauth flow
client_id: '',
client_secret: '',
redirect_uri: ''
}
const hubspot = new Hubspot(params)
return hubspot.oauth.getAccessToken(params).then(...)
You may use this library in your Typescript project via:
import Hubspot from 'hubspot';
const hubspot = new Hubspot({ apiKey: YOUR_API_KEY });
MIT
See our contributing guidelines
After cloning this repo, run npm install
to fetch dependencies. Then run the
test suite with npm test
. From master, this should be green. Our tests mock
out requests to HubSpot's api using nock. We then recommend running the test
suite without mocking api requests with NOCK_OFF=true npm run mocha
. This
should fail as you'll need some environment variables for real requests to
pass.
If you haven't already, create a developer account on hubspot. You'll want to
create an app and a test account as well. Then, create a new file, .env
in
the root of the repo. Inside you'll need to add an app id, a HubSpot user id,
and an oauth access token, also you will need to provide api key and
set some workflow id;
NOTE: Your HubSpot user ID; This can be found in the same place as your Developer HAPIkey in your Developer portal.
APPLICATION_ID=111111
USER_ID=2222222
ACCESS_TOKEN=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_BBB_CCCC_____-D_EEEEEEEEEEEEEEEEEEEEEEE_fffffff-gggggg"
HUBSPOT_API_KEY=1111-2222-3333-4444-5555
TEST_WORKFLOW_ID=333
To get an access token, you should follow the instructions here after cloning
the oauth-quickstart-nodejs project. Make sure to modify index.js
to include
all the required scopes as shown in this pull-request.
Once you have a green test suite with mocking turned off (run
NOCK_OFF=true npm run mocha
to confirm) you can write a test for the new
feature or bug fix hitting the live API. Once that test is passing try mocking
out the endpoint using the fakeHubspotApi test helper.
Push to your fork. Write a good commit message. Submit a pull request.
Others will give constructive feedback. This is a time for discussion and improvements, and making the necessary changes will be required before we can merge the contribution.
Thank you to all our contributors.