![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
chargebee
Advanced tools
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.
Node 0.6 or higher.
Install the latest version of the library with:
npm install chargebee
# or
yarn add chargebee
# or
pnpm install chargebee
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',
});
try {
const result = await chargebee.customer
.create({
email: 'john@test.com',
// other params
})
.request();
// access customer as result.customer;
} catch (err) {
// handle error
}
chargebee.customer
.create({
email: 'john@test.com',
// other params
})
.request()
.then((result) => {
// handle result
// access customer as result.customer;
})
.catch((err) => {
// handle error
});
chargebee.customer
.create({
email: 'john@test.com',
// other params
})
.request(function (error, result) {
if (error) {
// handle error
} else {
// handle result
// access customer as result.customer;
}
});
The response object returned by the request()
method is generic response wrapper. You need to access the resource from it. For example,
const result = await chargebee.customer.create({ email: 'john@test.com' }).request();
console.log(result.customer);
Other resources can be accessed by the same approach. For subscription, it will be result.subscription
const result = await chargebee.subscription
.list({
/* params */
})
.request();
// A list of Subscription objects
console.log(result.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
// ['list', 'next_offset']
console.log(Object.keys(result));
// ['1', '2', '3'], e.g. `result.list` is an array with 3 entries
console.log(Object.keys(result.list));
// ['activated_at', 'base_currency_code', ...]
// ['activated_at', 'base_currency_code', ...]
// ['activated_at', 'base_currency_code', ...]
// Which means we've reached the bottom and should have all the information available from this request
console.log(result.list.map((obj) => obj.subscription));
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.list.map((obj) => obj.customer),
next_offset: result.next_offset,
};
};
const getCustomers = async () => {
const { customers, next_offset } = await fetchCustomers();
console.log('Offset:', next_offset); // Print the offset value
// Fetching next set of customers
await fetchCustomers(next_offset);
};
getCustomers().catch((err) => {
console.log(err);
});
const result = await chargebee.customer
.create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' }) //Add custom field in payload
.headers({
'chargebee-event-email': 'all-disabled', // To disable webhooks
'chargebee-request-origin-ip': '192.168.1.2',
})
.setIdempotencyKey("safeKey")
.request();
const customer = result.customer;
console.log(customer.cf_host_url);
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.customer;
const headers = result.headers;
const isIdempotencyReplayed = result.isIdempotencyReplayed;
OR
chargebee.customer.create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' })
.setIdempotencyKey("safeKey")
.request(function(error,result) {
if(error){
//handle error
}else{
const customer = result.customer;
const headers = result.headers;
const isIdempotencyReplayed = result.isIdempotencyReplayed;
}
});
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.
The full documentation can be found on the Chargebee API Docs:
https://apidocs.chargebee.com/docs/api?lang=node
See the LICENSE file.
FAQs
A library for integrating with Chargebee.
We found that chargebee demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.