Skip to content

About

ChartMogul API Node.js Client

Resources

Stars

27 stars

Watchers

30 watching

Forks

Repository files navigation

Official ChartMogul API Node.js Client

chartmogul-node provides convenient Node.js bindings for ChartMogul's API.

npm Package


Installation | Configuration | Usage | Development | Contributing | Security | License



Installation

This library supports node.js 18.x or above.

npm install --save chartmogul-node

Configuration

First create a Config object by passing your api key, available from the administration section of your ChartMogul account.

const ChartMogul = require('chartmogul-node');
const config = new ChartMogul.Config(process.env.CHARTMOGUL_API_KEY);

You need to pass this config object as the first argument to each request.

Test your authentication

ChartMogul.Ping.ping(config)
  .then(res => console.log(res))
  .catch(err => console.error(err))

Usage

The library can be used both with promise and callback patterns. Supply the callback function as the last argmunent.

Here are sample examples:

Using as promise

const ChartMogul = require('chartmogul-node');

const config = new ChartMogul.Config('apiKey');

ChartMogul.Customer.create(config, {
  data_source_uuid: 'ds_e243129a-12c0-4e29-8f54-07da7905fbd1',
  external_id: 'cus_0001',
  name: 'Adam Smith',
  email: 'adam@smith.com',
  country: 'US',
  city: 'New York'
})
.then(res => {
  console.log(res);
})
.catch(e => console.error(e.message, e.httpStatus, e.response));

Using with a callback

const ChartMogul = require('chartmogul-node');

const config = new ChartMogul.Config('apiKey');

ChartMogul.Customer.create(config, data, (err, res) => {
  if(err) {
    console.error(err.message, err.httpStatus, err.response)
  }
  console.log(res);
});

Import API

Available methods in Import API:

ChartMogul.DataSource.create(config, data)
ChartMogul.DataSource.retrieve(config, dataSourceUuid, query)
ChartMogul.DataSource.all(config, query)
ChartMogul.DataSource.destroy(config, dataSourceUuid)
ChartMogul.Customer.create(config, data)
ChartMogul.Customer.retrieve(config, customerUuid)
ChartMogul.Customer.retrieve(config, customerUuid, {
  with_overrides: true,
  // attributes_with_history must be a comma-separated string, not an array
  attributes_with_history: 'company,custom.channel'
})
ChartMogul.Customer.all(config, { per_page: 20 })
ChartMogul.Customer.modify(config, customerUuid, data)
ChartMogul.Customer.modify(config, customerUuid, {
  company: 'Pinata Technologies',
  overrides: { company: true, attributes: { custom: { channel: true } } }
})
ChartMogul.Customer.destroy(config, customerUuid)
ChartMogul.Customer.merge(config, {
  'from': {'customer_uuid': 'cus_5915ee5a-babd-406b-b8ce-d207133fb4cb'},
  'into': {'customer_uuid': 'cus_2123290f-09c8-4628-a205-db5596bd58f7'}
})
ChartMogul.Customer.unmerge(config, {
  'customer_uuid': 'cus_cd9e5f29-6299-40e5-b343-0bd1ed228b4f',
  'data_source_uuid': 'ds_788ec6ae-dd51-11ee-bd46-a3ec952dc041',
  'external_id': 'cus_O075O8NH0LrtG8',
  'move_to_new_customer': []
})
// DEPRECATED: Use ChartMogul.Metrics.Customer.connectSubscriptions instead
ChartMogul.Customer.connectSubscriptions(config, customerUuid, {
  subscriptions: [{ uuid: 'sub_uuid' }]
})
// DEPRECATED: Use ChartMogul.Metrics.Customer.disconnectSubscriptions instead
ChartMogul.Customer.disconnectSubscriptions(config, customerUuid, {
  subscriptions: [{ uuid: 'sub_uuid' }]
})

ChartMogul.Customer.contacts(config, customerUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Customer.createContact(config, customerUuid, data)

// DEPRECATED: Use ChartMogul.Customer.entityNotes instead
ChartMogul.Customer.notes(config, customerUuid, { per_page: 10, cursor: 'cursor==' })
// DEPRECATED: Use ChartMogul.Customer.createEntityNote instead
ChartMogul.Customer.createNote(config, customerUuid, data)

ChartMogul.Customer.entityNotes(config, customerUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Customer.createEntityNote(config, customerUuid, data)

ChartMogul.Customer.opportunities(config, customerUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Customer.createOpportunity(config, customerUuid, data)

ChartMogul.Customer.tasks(config, customerUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Customer.createTask(config, customerUuid, data)
// customer_uuid and data_source_uuid may be omitted to create a contact that is not linked to a customer
ChartMogul.Contact.create(config, data)
ChartMogul.Contact.retrieve(config, contactUuid)
ChartMogul.Contact.retrieve(config, contactUuid, {
  with_overrides: true,
  // attributes_with_history must be a comma-separated string, not an array
  attributes_with_history: 'title'
})
ChartMogul.Contact.modify(config, contactUuid, data)
// Contact custom attribute overrides are not yet implemented by the API, so
// contact overrides cover standard fields only; custom attribute keys in
// contact overrides are rejected with a 422
ChartMogul.Contact.modify(config, contactUuid, {
  title: 'CEO',
  overrides: { title: true }
})
ChartMogul.Contact.destroy(config, contactUuid)
ChartMogul.Contact.merge(config, intoUuid, fromUuid);
ChartMogul.Contact.all(config, {
  per_page: 10,
  cursor: 'cursor==',
  customer_uuid: customerUuid,
  data_source_uuid: dataSourceUuid,
  email: 'jane@example.com',
  customer_external_id: 'customer_001',
  external_id: 'contact_001'
})

ChartMogul.Contact.tasks(config, contactUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Contact.createTask(config, contactUuid, data)

ChartMogul.Contact.entityNotes(config, contactUuid, { per_page: 10, cursor: 'cursor==' })
ChartMogul.Contact.createEntityNote(config, contactUuid, data)

Notes and call logs can be attached to a customer or a contact. Pass customer_uuid for a customer, or associated_object_identifier for either.

ChartMogul.EntityNote.create(config, { customer_uuid: customerUuid, type: 'note', text: 'Text' })
ChartMogul.EntityNote.create(config, {
  associated_object_identifier: { associated_object: 'contact', method: 'uuid', value: contactUuid },
  type: 'call',
  call_duration: 120
})
ChartMogul.EntityNote.retrieve(config, noteUuid)
ChartMogul.EntityNote.patch(config, noteUuid, data) // resolves to {} when the API answers 304 Not Modified
ChartMogul.EntityNote.destroy(config, noteUuid)
ChartMogul.EntityNote.all(config, {
  per_page: 10,
  cursor: 'cursor==',
  customer_uuid: customerUuid,
  contact_uuid: contactUuid,
  type: 'note',
  author_email: 'jane@example.com'
})

Customer Notes (deprecated)

// DEPRECATED: Use ChartMogul.EntityNote instead
ChartMogul.CustomerNote.create(config, data)
ChartMogul.CustomerNote.retrieve(config, noteUuid)
ChartMogul.CustomerNote.patch(config, noteUuid, data)
ChartMogul.CustomerNote.destroy(config, noteUuid)
ChartMogul.CustomerNote.all(config, { per_page: 10, cursor: 'cursor==', customer_uuid: customerUuid})

// Recommended: Use the Notes and Call Logs API above
ChartMogul.EntityNote.create(config, data)
ChartMogul.EntityNote.all(config, { per_page: 10, cursor: 'cursor==', customer_uuid: customerUuid })
ChartMogul.Opportunity.create(config, data)
ChartMogul.Opportunity.retrieve(config, opportunityUuid)
ChartMogul.Opportunity.patch(config, opportunityUuid, data)
ChartMogul.Opportunity.destroy(config, opportunityUuid)
ChartMogul.Opportunity.all(config, { per_page: 10, cursor: 'cursor==', customer_uuid: customerUuid})
ChartMogul.Task.create(config, { customer_uuid: customerUuid, task_details: 'Text', assignee: 'jane@example.com', due_date: '2025-04-30T00:00:00Z' })
ChartMogul.Task.create(config, {
  associated_object_identifier: { associated_object: 'contact', method: 'uuid', value: contactUuid },
  task_details: 'Text',
  assignee: 'jane@example.com',
  due_date: '2025-04-30T00:00:00Z'
})
ChartMogul.Task.retrieve(config, taskUuid)
ChartMogul.Task.patch(config, taskUuid, data) // resolves to {} when the API answers 304 Not Modified
ChartMogul.Task.destroy(config, taskUuid)
ChartMogul.Task.all(config, {
  per_page: 10,
  cursor: 'cursor==',
  customer_uuid: customerUuid,
  contact_uuid: contactUuid,
  assignee: 'jane@example.com',
  due_date_on_or_after: '2025-04-01',
  due_date_on_or_before: '2025-04-30',
  completed: false
})
ChartMogul.Plan.create(config, data)
ChartMogul.Plan.retrieve(config, uuid)
ChartMogul.Plan.modify(config, uuid, { name: 'new name' })
ChartMogul.Plan.destroy(config, uuid)
ChartMogul.Plan.all(config, query)
ChartMogul.PlanGroup.create(config, data)
ChartMogul.PlanGroup.retrieve(config, planGroupUuid)
ChartMogul.PlanGroup.modify(config, planGroupUuid, data)
ChartMogul.PlanGroup.destroy(config, planGroupUuid)
ChartMogul.PlanGroup.all(config, query)
ChartMogul.PlanGroup.all(config, planGroupUuid, query)
// DEPRECATED: Use ChartMogul.Metrics.Customer.subscriptions instead
ChartMogul.Subscription.all(config, customerUuid, query)
ChartMogul.Subscription.cancel(config, subscriptionUuid, { cancelled_at: '' })
ChartMogul.Subscription.modify(config, subscriptionUuid, { cancellation_dates: [] })

// Recommended: Use Metrics API for subscriptions
ChartMogul.Metrics.Customer.subscriptions(config, customerUuid, query)
ChartMogul.Metrics.Customer.connectSubscriptions(config, dataSourceUuid, customerUuid, {
  subscriptions: [{ uuid: 'sub_uuid', data_source_uuid: 'ds_uuid' }]
})
ChartMogul.Metrics.Customer.disconnectSubscriptions(config, dataSourceUuid, customerUuid, {
  subscriptions: [{ uuid: 'sub_uuid', data_source_uuid: 'ds_uuid' }]
})
ChartMogul.Invoice.create(config, customerUuid, data)
ChartMogul.Invoice.retrieve(config, invoiceUuid)
ChartMogul.Invoice.destroy(config, invoiceUuid)
ChartMogul.Invoice.all(config, customerUuid, query)
ChartMogul.Invoice.all(config, query)
ChartMogul.Invoice.destroy_all(config, dataSourceUuid, customerUuid)
ChartMogul.Transaction.create(config, invoiceUuid, data)
ChartMogul.SubscriptionEvent.create(config, {
  subscription_event: {
    event_type: 'subscription_cancelled',
    event_date: '2022-06-13',
    effective_date: '2022-06-13',
    subscription_external_id: 'sub_ex_id_1',
    customer_external_id: 'cus_ex_id_1',
    data_source_uuid: '4175177b-3e4b-45b6-b6fb-426d6fd79028'
  }
})
ChartMogul.SubscriptionEvent.updateWithParams(config, {
  subscription_event: {
    id: 1,
    plan_external_id: 'gazillion_monthly'
  }
})
ChartMogul.SubscriptionEvent.deleteWithParams(config, {
  subscription_event: {
    id: 1
  }
})
ChartMogul.SubscriptionEvent.all(config, query)

Enrichment API

Available methods in Enrichment API:

ChartMogul.Customer.search(config, { email: 'bob@example.com' })
ChartMogul.Customer.attributes(config, customerUuid)
ChartMogul.Customer.attributes(config, customerUuid, {
  with_overrides: true,
  // attributes_with_history must be a comma-separated string, not an array
  attributes_with_history: 'custom.channel'
})
ChartMogul.Tag.add(config, customerUuid, {
  'tags': ['important', 'Prio1']
});
ChartMogul.Tag.add(config, {
  'email': 'adam@smith.com',
  'tags': ['important', 'Prio1']
})'
ChartMogul.Tag.remove(config, customerUuid, {
  'tags': ['Prio1', 'discountable']
});
ChartMogul.CustomAttribute.add(config, customerUuid, {
  'custom': [
    { 'type': 'Integer', 'key': 'age', 'value': 8 }
  ]
});
ChartMogul.CustomAttribute.add(config, {
  'email': 'adam@example.com',
  'custom': [
    { 'type': 'Integer', 'key': 'age', 'value': 8 }
  ]
});
ChartMogul.CustomAttribute.update(config, customerUuid, {
  'custom': {
    'age': 20,
    'channel': 'Twitter'
  }
});
ChartMogul.CustomAttribute.remove(config, customerUuid, {
  'custom': ['CAC']
});

// Note the overrides shape here is { custom: { ... } } — on Customer.create and
// Customer.modify the same flags nest under attributes:
// { attributes: { custom: { ... } } }
ChartMogul.CustomAttribute.add(config, customerUuid, {
  'custom': [
    { 'type': 'String', 'key': 'channel', 'value': 'Facebook' }
  ],
  'overrides': { 'custom': { 'channel': true } }
});

// By email the request uses the same direct shape, while each entries[].overrides
// in the response uses the customer shape with the attributes wrapper
ChartMogul.CustomAttribute.add(config, {
  'email': 'adam@example.com',
  'custom': [
    { 'type': 'String', 'key': 'channel', 'value': 'Facebook' }
  ],
  'overrides': { 'custom': { 'channel': true } }
});

// A flag set to false removes an override: on update the attribute is restored
// to its most recent edit-history value; on create it is a no-op. Overridden
// attributes are kept on remove unless the same call removes their override —
// the response `message` names any that were kept
ChartMogul.CustomAttribute.remove(config, customerUuid, {
  'custom': ['channel'],
  'overrides': { 'custom': { 'channel': false } }
});

Available methods in Metrics API:

ChartMogul.Metrics.all(config, {
  'start-date': '2015-01-01',
  'end-date': '2015-11-24',
  'interval': 'month',
  'geo': 'GB',
  'plans': 'Bronze Plan',
  'filters': "currency~ANY~'USD'"
})
ChartMogul.Metrics.mrr(config, query)
ChartMogul.Metrics.arr(config, query)
ChartMogul.Metrics.arpa(config, query)
ChartMogul.Metrics.asp(config, query)
ChartMogul.Metrics.customerCount(config, query)
ChartMogul.Metrics.customerChurnRate(config, query)
ChartMogul.Metrics.mrrChurnRate(config, query)
ChartMogul.Metrics.ltv(config, query)
ChartMogul.Metrics.Customer.activities(config, customerUuid, query)
ChartMogul.Metrics.Customer.subscriptions(config, customerUuid, query)
ChartMogul.Metrics.Customer.connectSubscriptions(config, dataSourceUuid, customerUuid, data)
ChartMogul.Metrics.Customer.disconnectSubscriptions(config, dataSourceUuid, customerUuid, data)
ChartMogul.Metrics.Activity.all(config, query)
ChartMogul.Metrics.ActivitiesExport.create(config, query)
ChartMogul.Metrics.ActivitiesExport.retrieve(config, id)

Account

Available methods:

ChartMogul.Account.retrieve(config)

Errors

The library throws following error objects.

  • ChartMogul.ChartMogulError
  • ChartMogul.ConfigurationError
  • ChartMogul.ForbiddenError
  • ChartMogul.NotFoundError
  • ChartMogul.ResourceInvalidError
  • ChartMogul.SchemaInvalidError
  • ChartMogul.DeprecatedParamError

The following table describes the properties of the error object.

Property Type Description
message string The error message
httpStatus number When the error occurs during an HTTP request, the HTTP status code.
response object or string HTTP response as JSON, or raw response if not parsable to JSON

Rate Limits & Exponential Backoff

The library will keep retrying if the request exceeds the rate limit or if there's any network related error. By default, the request will be retried for 20 times (approximated 15 minutes) before finally giving up.

You can change the retry count using Config object:

const ChartMogul = require('chartmogul-node');
const config = new ChartMogul.Config(process.env.CHARTMOGUL_API_KEY);
config.retries = 15; // 0 disables retrying

Development

To work on the library:

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Install dependencies: npm install
  • Fix bugs or add features. Make sure the changes pass the coding guidelines by runing: npm run lint
  • Write tests for your new features. For HTTP mocking nock library is used. Run tests with npm test and check test coverage with npm run cover
  • If all tests are passed, push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chartmogul/chartmogul-node.

Releasing

See RELEASING.md for the full release process.

Security

Verifying Releases

All releases of this library are published as immutable GitHub Releases with protected tags and as a package on npmjs.com.

To maximize supply chain security:

  • Commit your package-lock.json or yarn.lock to version control — it records integrity hashes (SHA-512) for all dependencies, ensuring reproducible and tamper-evident installs
  • Use npm ci in CI environments for deterministic builds from the lockfile

License

The library is available as open source under the terms of the MIT License.

The MIT License (MIT)

Copyright (c) 2016 ChartMogul Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

ChartMogul API Node.js Client

Resources

Stars

27 stars

Watchers

30 watching

Forks

Releases

Packages

Used by

Contributors

Languages