chartmogul-node
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/chartmogul-node package

    1.5.0 • Public • Published

    Official ChartMogul API Node.js Client

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

    npm Package


    Installation | Configuration | Usage | Development | Contributing | License



    Installation

    This library requires node.js 10.x or above.

    npm install --save chartmogul-node

    Configuration

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

    const ChartMogul = require('chartmogul-node');
    const config = new ChartMogul.Config(process.env.CHARTMOGUL_ACCOUNT_TOKEN, process.env.CHARTMOGUL_SECRET_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('accountToken', 'secretKey');
    
    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('accountToken', 'secretKey');
    
    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:

    Data Sources

    ChartMogul.DataSource.create(config, data)
    ChartMogul.DataSource.retrieve(config, uuid)
    ChartMogul.DataSource.all(config, query)
    ChartMogul.DataSource.destroy(config, dataSourceUuid)

    Customers

    ChartMogul.Customer.create(config, data)
    ChartMogul.Customer.all(config, {
      page: 2,
      per_page: 20
    })
    ChartMogul.Customer.destroy(config, customerUuid)

    Plans

    ChartMogul.Plan.create(config, data)
    ChartMogul.Plan.retrieve(config, uuid)
    ChartMogul.Plan.modify(config, uuid, {
        name: "new name"
    })
    ChartMogul.Plan.all(config, query)
    ChartMogul.Plan.destroy(config, uuid)

    Plan Groups

    ChartMogul.PlanGroup.create(config, data)
    ChartMogul.PlanGroup.retrieve(config, planGroupUuid)
    ChartMogul.PlanGroup.modify(config, planGroupUuid, data)
    ChartMogul.PlanGroup.all(config, query)
    ChartMogul.PlanGroup.all(config, planGroupUuid, query)
    ChartMogul.PlanGroup.destroy(config, planGroupUuid)

    Invoices

    ChartMogul.Invoice.create(config, customerUuid, data)
    ChartMogul.Invoice.all(config, customerUuid, query)
    ChartMogul.Invoice.all(config, query)
    ChartMogul.Invoice.retrieve(config, invoiceUuid)

    Transactions

    ChartMogul.Transaction.create(config, invoiceUuid, data)

    Subscriptions

    ChartMogul.Subscription.all(config, customerUuid, query)
    ChartMogul.Subscription.cancel(config, subscriptionUuid, {cancelled_at: ""})
    ChartMogul.Subscription.modify(config, subscriptionUuid, {cancellation_dates: []})

    Enrichment API

    Available methods in Enrichment API:

    Customers

    ChartMogul.Customer.retrieve(config, customerUuid)
    ChartMogul.Customer.all(config, query)
    ChartMogul.Customer.search(config, {
      email: 'adam@smith.com'
    })
    
    ChartMogul.Customer.merge(config, {
      "from": {"customer_uuid": "cus_5915ee5a-babd-406b-b8ce-d207133fb4cb"},
      "into": {"customer_uuid": "cus_2123290f-09c8-4628-a205-db5596bd58f7"}
    })
    
    ChartMogul.Customer.modify(config, "cus_5915ee5a-babd-406b-b8ce-d207133fb4cb", {
      "lead_created_at": "2015-01-01 00:00:00",
      "free_trial_started_at": "2015-06-13 15:45:13"
    })
    
    ChartMogul.Customer.connectSubscriptions(config, "cus_5915ee5a-babd-406b-b8ce-d207133fb4cb", {
        "subscriptions": [{
                "data_source_uuid": "ds_ade45e52-47a4-231a-1ed2-eb6b9e541213",
                "external_id": "d1c0c885-add0-48db-8fa9-0bdf5017d6b0"
            },
            {
                "data_source_uuid": "ds_ade45e52-47a4-231a-1ed2-eb6b9e541213",
                "external_id": "9db5f4a1-1695-44c0-8bd4-de7ce4d0f1d4"
            }
        ]
    })

    Customer Attributes

    ChartMogul.Customer.attributes(config, customerUuid)

    Tags

    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"]
    });

    Custom Attributes

    ChartMogul.CustomAttribute.add(config, customerUuid, {
      'custom': [
        {'type': 'Integer', 'key': 'age', 'value': 8}
      ]
    });
    ChartMogul.CustomAttribute.add(config, {
      'email': 'adam@smith.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']
    });

    Metrics API

    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'
    })
    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.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

    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_ACCOUNT_TOKEN, process.env.CHARTMOGUL_SECRET_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

    1. You need an authorized account on npmjs.com
    2. Bump up the version in package.json & tag on GitHub
    3. npm test
    4. npm publish

    Full Howto

    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.

    Install

    npm i chartmogul-node

    DownloadsWeekly Downloads

    1,626

    Version

    1.5.0

    License

    MIT

    Unpacked Size

    111 kB

    Total Files

    62

    Last publish

    Collaborators

    • hassansin
    • kopacpetr
    • ikanade