Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chargebee

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chargebee

A library for integrating with ChargeBee.

  • 2.44.0
  • npm
  • Socket score

Version published
Weekly downloads
34K
decreased by-12.78%
Maintainers
1
Weekly downloads
 
Created
Source

[!WARNING]
This branch contains the code for Chargebee Node.js SDK v2 which is deprecated. v2 will continue to receive updates till September 30, 2025. If you are using v2, we request you to upgrade to v3 by following this migration guide before September 30, 2025.

Chargebee Node.js Client Library

npm npm

This is the node.js library for integrating with Chargebee. Sign up for a Chargebee account here.

Note If you’re using API V1, head to chargebee-v1 branch.

Requirements

Node 0.6 or higher.

Installation

Install the latest version of the library with:

npm install chargebee
# or
yarn add chargebee
# or
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.

The full documentation can be found on the Chargebee API Docs: https://apidocs.chargebee.com/docs/api?lang=node

const chargebee = require('chargebee');

chargebee.configure({
  site: '<YOUR_SITE_NAME>',
  api_key: '<YOUR_API_KEY>',
});

Or using ES modules,

import chargebee from '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',
      // other params
    })
    .request();
    // access customer as result.customer;
} catch (err) {
  // handle error
}

Using Promises

chargebee.customer
  .create({
    email: 'john@test.com',
    // other params
  })
  .request()
  .then((result) => {
    // handle result
    // access customer as result.customer;
  })
  .catch((err) => {
    // handle error
  });

Using callbacks

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

Usage with TypeScript

You can import the types as shown below.

import chargebee, { Customer } from 'chargebee';

chargebee.configure({
  site: '<YOUR_SITE_NAME>',
  api_key: '<YOUR_API_KEY>',
});

const createCustomer = async () => {
  const inputParams: Customer.CreateInputParam = {
    email: 'john@test.com',
    first_name: 'John',
    last_name: 'Doe',
  };

  const { customer } = await chargebee.customer.create(inputParams).request();
  console.log(customer);
};

createCustomer();

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

Using custom headers and custom fields:

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);

Creating 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.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;
  }
});

Passing API Keys at request level

const newCust = await chargebee.customer.create({
  email: 'john@test.com',
  first_name: 'John',
  last_name: 'Doe'
}).request({
  site: '<YOUR_SITE_NAME>',
  api_key: '<YOUR_API_KEY>',
});

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.

License

See the LICENSE file.

Keywords

FAQs

Package last updated on 14 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc