Chargebee Node Client Library - API V2

This is the node.js library for integrating with Chargebee. Sign up for a Chargebee account here.
Note
Chargebee now supports two API versions - V1 and V2, of which V2 is the latest release and all future developments will happen in V2. This library is for API version V2. If you’re looking for V1, head to chargebee-v1 branch.
Requirements
Node 0.6 or higher.
Installation
Install the latest version of the library with:
npm install chargebee
yarn add chargebee
pnpm install chargebee
Usage
The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer here for more details.
var chargebee = require('chargebee');
chargebee.configure({
site: 'YOUR_SITE_NAME',
api_key: 'YOUR_API_KEY',
});
Using Async / Await
try {
const result = await chargebee.customer
.create({
email: 'john@test.com',
})
.request();
} catch (err) {
}
Using Promises
chargebee.customer
.create({
email: 'john@test.com',
})
.request()
.then((result) => {
})
.catch((err) => {
});
Using callbacks
chargebee.customer
.create({
email: 'john@test.com',
})
.request(function (error, result) {
if (error) {
} else {
}
});
Accessing the response object
The response object returned by the request()
method is generic response wrapper. You need to access the resource from it. For example,
- To access customer object.
const result = await chargebee.customer.create({ email: 'john@test.com' }).request();
console.log(result.response.customer);
Other resources can be accessed by the same approach. For subscription, it will be result.subscription
const result = await chargebee.subscription
.list({
})
.request();
console.log(result.response.list.map((obj) => obj.subscription));
Note
If you have a result
(or children further down the line) and are unsure what properties are available, you can use Object.keys
to get a list of available accessor properties. Using Object.keys
in the previous example would yield
console.log(Object.keys(result.response));
console.log(Object.keys(result.response.list));
console.log(result.response.list.map((obj) => obj.subscription));
Using filters in the List API
For pagination: offset
is the parameter that is being used. The value used for this parameter must be the value returned for next_offset
parameter in the previous API call.
const fetchCustomers = async (offset) => {
const result = await chargebee.customer.list({
limit: 2,
offset: offset,
first_name: { is: 'John' },
}).request();
return {
customers: result.response.list.map((obj) => obj.customer),
next_offset: result.response.next_offset,
};
};
const getCustomers = async () => {
const { customers, next_offset } = await fetchCustomers();
console.log('Offset:', next_offset);
await fetchCustomers(next_offset);
};
getCustomers().catch((err) => {
console.log(err);
});
Using custom headers and custom fields:
const result = await chargebee.customer
.create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' })
.headers({
'chargebee-event-email': 'all-disabled',
'chargebee-request-origin-ip': '192.168.1.2',
})
.setIdempotencyKey("safeKey")
.request();
const customer = result.response.customer;
console.log(customer.cf_host_url);
Create an idempotent request
Idempotency keys are passed along with request headers to allow a safe retry of POST requests.
const result = await chargebee.customer
.create({ email: 'john@test.com' })
.setIdempotencyKey("safeKey")
.request();
const customer = result.response.customer;
console.log(result.headers);
console.log(result.isIdempotencyReplayed);
isIdempotencyReplayed()
method can be accessed to differentiate between original and replayed requests.
Processing Webhooks - API Version Check
An attribute, api_version, is added to the Event resource, which indicates the API version based on which the event content is structured. In your webhook servers, ensure this _api_version* is the same as the API version used by your webhook server's client library.
Documentation
The full documentation can be found on the Chargebee API Docs:
https://apidocs.chargebee.com/docs/api?lang=node
License
See the LICENSE file.