# Chargebee Library
# Overview
This page documents the top-level methods available in the Chargebee global object.
# Prerequisite
Before using these methods, ensure you have set up Chargebee.js in your application.
# init()
Initializes the Chargebee library with your site configuration, creating a Chargebee instance that you can use throughout your application.
WARNING
Call tearDown() to clean up before calling init() again within the same session.
# Syntax
const chargebee = Chargebee.init({
site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN",
domain: "https://your-domain.com" // optional
});
2
3
4
# Parameters
# Return value
Returns a Chargebee instance object that provides access to all Chargebee.js features.
# Example
// Initialize Chargebee with your site.
const chargebee = Chargebee.init({
site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN", // Your Chargebee site identifier.
domain: "https://your-domain.com" // Optional: custom domain
});
// Use the instance to access features
const pricingTable = await chargebee.pricingTable();
2
3
4
5
6
7
8
# getInstance()
Retrieves the existing Chargebee instance that was created using the init() method. This is useful when you need to access the instance from different parts of your application.
# Syntax
const chargebee = Chargebee.getInstance();
# Return value
Returns the Chargebee instance object that was created during initialization.
Returns an error if no instance has been created yet.
# Example
// Initialize Chargebee
Chargebee.init({
site: "your-site-name"
});
// Later in your code, get the instance
const chargebee = Chargebee.getInstance();
// Use the instance
const pricingTable = await chargebee.pricingTable();
2
3
4
5
6
7
8
9
10
# registerAgain()
Rebinds event listeners for dynamically inserted DOM elements. This is primarily used for drop-in script integrations where you add new Chargebee elements to the page after the initial load.
# Syntax
Chargebee.registerAgain();
# Return value
None.
# Example
// Create a new checkout link dynamically.
const checkoutLink = document.createElement('a');
checkoutLink.href = "javascript:void(0)";
checkoutLink.dataset.cbType = "checkout";
checkoutLink.dataset.cbPlanId = "cbdemo_scale";
checkoutLink.textContent = "Subscribe Now";
document.body.appendChild(checkoutLink);
// Rebind listeners to recognize the new element.
Chargebee.registerAgain();
2
3
4
5
6
7
8
9
10
# tearDown()
Resets the state of the Chargebee object and removes any Chargebee component from the DOM. Call tearDown() to clean up before invoking init() again within the same session.
# Syntax
Chargebee.tearDown();
# Return value
None.
# Example
// Clean up before reinitializing
Chargebee.tearDown();
// Now safe to initialize again
const chargebee = Chargebee.init({
site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN"
});
2
3
4
5
6
7
# getPortalSections()
Returns an object containing all available sections in the Chargebee customer portal. You can use these section identifiers to navigate users to specific portal sections or open them as individual components.
# Syntax
const portalSections = Chargebee.getPortalSections();
# Return value
An object with key-value pairs mapping section names to their identifiers.
# Available sections
{
"SUBSCRIPTION_DETAILS": "sub_details",
"SUBSCRIPTION_CANCELLATION": "sub_cancel",
"EDIT_SUBSCRIPTION": "edit_subscription",
"VIEW_SCHEDULED_CHANGES": "scheduled_changes",
"ACCOUNT_DETAILS": "account_details",
"EDIT_ACCOUNT_DETAILS": "portal_edit_account",
"ADDRESS": "portal_address",
"EDIT_BILLING_ADDRESS": "portal_edit_billing_address",
"EDIT_SHIPPING_ADDRESS": "portal_edit_shipping_address",
"EDIT_SUBSCRIPTION_CUSTOM_FIELDS": "portal_edit_subscription_cf",
"PAYMENT_SOURCES": "portal_payment_methods",
"ADD_PAYMENT_SOURCE": "portal_add_payment_method",
"EDIT_PAYMENT_SOURCE": "portal_edit_payment_method",
"VIEW_PAYMENT_SOURCE": "portal_view_payment_method",
"CHOOSE_PAYMENT_METHOD_FOR_SUBSCRIPTION": "portal_choose_payment_method",
"BILLING_HISTORY": "portal_billing_history"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Example
// Get all available portal sections.
const sections = Chargebee.getPortalSections();
// Use a section identifier to open a specific portal section.
const chargebee = Chargebee.getInstance();
const chargebeePortal = chargebee.createChargebeePortal();
chargebeePortal.open({
// Optional callbacks can be added here.
}, {
sectionType: sections.ACCOUNT_DETAILS
});
2
3
4
5
6
7
8
9
10
11
12