A node wrapper for the HubSpot API





Javascript / node.js wrapper for the HubSpot API
``shell`
npm install hubspot
`javascript`
const Hubspot = require('hubspot')
const hubspot = new Hubspot({
apiKey: 'abc',
checkLimit: false // (Optional) Specify whether to check the API limit on each call. Default: true
})
You can also authenticate via token:
`javascript`
const hubspot = new Hubspot({ accessToken: YOUR_ACCESS_TOKEN })
To change the base url:
`javascript`
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:
`javascript
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.
`javascript`
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:
`javascript`
hubspot.contacts
.get(options)
.then(results => {
console.log(results)
})
.catch(err => {
console.error(err)
})
[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Please see repository with [samples] applications with common cases.
[samples]: https://github.com/HubSpot/integration-examples-nodejs
`javascript
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
`json`
{ "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
`javascript`
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)
`javascript`
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.
`javascript`
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
`javascript
hubspot.contacts.get(opts)
hubspot.contacts.getAll(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.updateByEmail(email, data)
hubspot.contacts.delete(id)
hubspot.contacts.merge(primaryId, secondaryId)
// Add a secondary email address to a contact
hubspot.contacts.addSecondaryEmail(vid, secondaryEmail)
`
`javascript`
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)
`javascript`
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
`javascript`
// more opts can be found at https://developers.hubspot.com/docs/methods/pages/get_pages
hubspot.pages.get(opts) // eg: opts = {is_draft: false}
`javascript`
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)
`javascript`
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
`javascript`
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)
hubspot.deals.properties.groups.delete(name)
// not an official API, wrapper doing two API calls. Callbacks not supported at
// this time
`javascript`
hubspot.engagements.create(data)
hubspot.engagements.get(opts)
hubspot.engagements.update(engagementId, data)
hubspot.engagements.getRecentlyModified(opts)
hubspot.engagements.getAssociated(objectType, objectId, opts)
hubspot.engagements.getCallDispositions()
`javascript`
hubspot.owners.get(opts)
`javascript`
hubspot.pipelines.get(opts)
`javascript`
hubspot.lists.get(opts)
hubspot.lists.getOne(id)
hubspot.lists.getByIdBatch(ids)
hubspot.lists.create(data)
hubspot.lists.delete(id)
hubspot.lists.getContacts(id, opts)
hubspot.lists.getRecentContacts(id, opts)
hubspot.lists.getRecentUpdates(opts)
hubspot.lists.addContacts(id, contactBody)
hubspot.lists.removeContacts(id, contactBody)
`javascript`
hubspot.files.get()
hubspot.files.getOne(id)
hubspot.files.upload(fileDetails, overwrite, hidden)
hubspot.files.uploadByUrl(fileDetails, overwrite, hidden)
`javascript
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.forms.getUploadedFileByUrl(url)
`
`javascript`
hubspot.subscriptions.get(opts)
hubspot.subscriptions.subscribeToAll(email)
hubspot.subscriptions.unsubscribe(email)
`javascript`
hubspot.campaigns.getById()
hubspot.campaigns.get(opts)
hubspot.campaigns.getOne(id)
hubspot.campaigns.events(opts)
`javascript`
hubspot.marketingEmail.get(opts)
hubspot.marketingEmail.getById(id)
hubspot.marketingEmail.create(data)
hubspot.marketingEmail.update(id, data)
hubspot.marketingEmail.clone(id, data)
hubspot.marketingEmail.delete(id)
hubspot.marketingEmail.versions(id)
hubspot.marketingEmail.restore(id)
hubspot.marketingEmail.hasBufferedChanges(id)
hubspot.marketingEmail.statistics(opts)
hubspot.marketingEmail.statisticsById(id)
`javascript`
hubspot.broadcasts.get(opts)
`javascript`
// 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.
[documentation]: https://developers.hubspot.com/docs/methods/timeline/create-or-update-event
`javascript`
hubspot.emails.sendTransactionalEmail(data)
`javascript`
hubspot.workflows.getAll()
hubspot.workflows.get(workflowId)
hubspot.workflows.create(data)
hubspot.workflows.delete(workflowId)
hubspot.workflows.enroll(workflowId, email)
hubspot.workflows.unenroll(workflowId, email)
hubspot.workflows.current(contactId)
`javascript`
hubspot.oauth.getAuthorizationUrl(opts)
hubspot.oauth.getAccessToken(data)
hubspot.oauth.refreshAccessToken()
hubspot.oauth.getPortalInfo(token)
#### Obtain your authorization url
`javascript`
const params = {
client_id: 'your_client_id',
scope: 'some scopes',
redirect_uri: 'take_me_to_the_ballpark',
}
const uri = hubspot.oauth.getAuthorizationUrl(params)
#### Obtain an access token from an authorization_code
`javascript`
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)
`javascript`
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(...)
`javascript
const data = [
{
name: 'subject',
value: 'This is an example ticket'
},
{
name: 'content',
value: 'Here are the details of the ticket.'
},
{
name: 'hs_pipeline',
value: '0'
},
{
name: 'hs_pipeline_stage',
value: '1'
}
];
const ids = [176606, 177919];
const properties = ['subject', 'content', 'hs_pipeline'];
const newDataId = [
{
objectId: 176606,
properties: [
{
name: 'subject',
value: 'SUBJECT 001'
},
{
name: 'content',
value: 'TICKET 001'
}
]
},
{
objectId: 177919,
properties: [
{
name: 'subject',
value: 'SUBJECT 002'
},
{
name: 'content',
value: 'TICKET 002'
}
]
}
];
hubspot.tickets.create(data);
hubspot.tickets.createBatch(data);
hubspot.tickets.getAll();
hubspot.tickets.getAll(properties);
hubspot.tickets.getById(id);
hubspot.tickets.getById(id, properties);
hubspot.tickets.getBatchById(ids);
hubspot.tickets.getBatchById(ids, properties);
hubspot.tickets.delete(id);
hubspot.tickets.deleteBatch(ids);
hubspot.tickets.update(id, newData);
hubspot.tickets.updateBatch(newDataId);
`
It is possible to access the hubspot request method directly,
it could be handy if wrapper doesn't have implementation for some endpoint yet.
Using of exposed request method benefits by the bottleneck throttling, auth and request parsing and formatting already in place
`javascript`
hubspot.apiRequest({
method: 'PUT',
path: '/some/api/not/wrapped/yet',
body: { key: 'value' },
})
Also it is possible to overlap hubspot base API URL using overlapUrl parameter
`javascript`
hubspot.apiRequest({
method: 'GET',
overlapUrl: 'https://api.hubspot.com/some/alternative/api',
})
You may use this library in your Typescript project via:
`typescript`
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 thenpm test
test suite with . From master, this should be green. Our tests mockNOCK_OFF=true npm run mocha
out requests to HubSpot's api using [nock]. We then recommend running the test
suite without mocking api requests with . This
should fail as you'll need some [environment variables] for real requests to
pass.
[nock]: https://github.com/nock/nock
[environment variables]: https://github.com/motdotla/dotenv
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.
[developer account]: https://developer.hubspot.com
[create an app]: https://developers.hubspot.com/docs/faq/how-do-i-create-an-app-in-hubspot
[test account]: https://developers.hubspot.com/docs/faq/how-do-i-create-a-test-account
[app id]: https://developers.hubspot.com/docs/faq/how-do-i-find-the-app-id
[oauth access token]: https://developers.hubspot.com/docs/methods/oauth2/oauth2-overview
[api key]: https://developers.hubspot.com/docs/faq/developer-hapikeys
[workflow id]: https://developers.hubspot.com/docs/methods/workflows/workflows_overview
`.env`
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].
[instructions here]: https://github.com/HubSpot/oauth-quickstart-nodejs
[pull-request]: https://github.com/HubSpot/oauth-quickstart-nodejs/pull/1
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][commit]. Submit a pull request.
[commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
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].
[contributors]: https://github.com/MadKudu/node-hubspot/graphs/contributors