More Tutorials
Published on

Show existing customers a personalized pricing table

Growth
Pricing Tables

Overview

In this tutorial, you'll learn how to display a Chargebee pricing table for existing customers in your application using Chargebee Growth Plays. This approach lets you create pricing page sessions securely on the server and render them in a dedicated container using Chargebee.js.

This tutorial is intended for developers building a web app (e.g. JavaScript, React, Vue, or Angular) with a backend that can call Chargebee's API.

By the end of this tutorial, you'll be able to:

  • Include and initialize Chargebee.js on the frontend with the correct site configuration.
  • Add a container <div> with the parameters required for API-driven pricing tables.
  • Call the Pricing Page Session API from your backend for existing subscriptions.
  • Pass a custom attribute so Growth can target the right pricing table—for example, splitting users between a control and a test table in a pricing experiment.
  • Request a pricing table session from the frontend and render it using Chargebee.js open().

Background

  • Chargebee.js is a client-side library that lets you embed Chargebee components (such as pricing tables and Checkout) in your frontend. You load the script, initialize it with your site, and use methods like pricingTable.open() to display a pricing table.
  • Pricing Page Sessions are created via the Chargebee REST API on the server for existing subscriptions (plan changes, upgrades, downgrades). The API returns a pricing table object based on Play configurations in Chargebee Growth. Chargebee.js uses this object to render the table.
  • By keeping session creation on the backend, you never expose your Chargebee API key to the browser; the frontend only receives the session payload and passes it to Chargebee.js.

Before you start

Before you start the tutorial, you should:

  • Have a Chargebee Billing account and a Chargebee Growth account.
  • Have one or more pricing tables created in Chargebee Growth and associated them with one or more Plays in Chargebee Growth.
  • Obtain a full-access API key from your Chargebee Billing test site (required for the Pricing Page Session API).
  • Have a running frontend and backend so you can add the script, a backend endpoint, and a frontend call to fetch the session and open the pricing table.

Step 1: Integrate the Chargebee REST API (Backend)

Use the Chargebee REST API from your backend to create pricing page sessions. Chargebee provides client libraries (e.g. PHP, Ruby, Python, Java, .NET, Node, Go) that wrap the HTTP API; use the one that matches your stack.

Configure your client with your Chargebee Billing site and full-access API key. Never expose the API key to the frontend.

Then implement a POST endpoint that:

  1. Accepts a request from the frontend with the subscription_id.
  2. Computes the targeting attribute on the server (for example, the subscriber's plan_usage_percent) and builds the custom object.
  3. Calls the Create pricing page for existing subscription API, passing the custom attribute so Growth can evaluate your Play's audience rules and return the matching pricing table.
  4. Returns the pricing page session object to the frontend as JSON.

The custom parameter is a JSON object of custom attributes (key-value pairs) used for pricing page targeting. For the pricing experiment for free users, compute the signal your audience matches on—say, how close the free user is to their plan limit, as a percentage (plan_usage_percent)—and send it so Growth can place them in the experiment and show the control or test table. Map this field in Growth before you use it in audience rules; unmapped fields are ignored.

// Example: Express route that creates a pricing page session for an existing subscription
app.post("/api/create_pricing_table", async (req, res) => {
  const chargebee = require("chargebee");
  chargebee.configure({ site: "YOUR_SITE", api_key: "YOUR_API_KEY" });

  const subscriptionId = req.body.subscription_id; // Required: existing subscription ID
  if (!subscriptionId) {
    return res.status(400).json({ error: "subscription_id is required" });
  }

  try {
    // Compute the targeting attribute on the server, then pass it as `custom`.
    const custom = { plan_usage_percent: await getPlanUsagePercent(subscriptionId) };

    const result = await chargebee.pricing_page_session
      .create_for_existing_subscription({
        subscription: { id: subscriptionId },
        custom, // Growth uses this attribute to match the Play audience and pick the pricing table.
      })
      .request();

    res.json(result.pricing_page_session);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: error.message });
  }
});

// Stub: replace with a real lookup of the subscriber's usage relative to their plan limit.
async function getPlanUsagePercent(subscriptionId) {
  // e.g. read metered usage / entitlements for this subscription and return a 0-100 value
  return 95;
}

Ensure your backend returns the pricing page session object and that the response is JSON. The frontend will send this payload to Chargebee.js in the next step.

Step 2: Request a pricing table and open it (Frontend)

From the frontend, request a pricing table session from your backend and pass the result to Chargebee.js so it can render the table inside the container <div>.

1. Load the Chargebee.js script – Load the Chargebee.js script and initialize it with your Chargebee Billing site.

2. Call your backend – When the user should see the pricing table (e.g. on page load or when clicking “View plans”), send a POST request to your endpoint (e.g. /api/create_pricing_table) with only the subscription_id in the request body. The backend computes the targeting attribute(s) (plan_usage_percent) and adds it as custom before calling Chargebee.

3. Return a Promise that resolves to the session – The pricingTable.open() method expects a pricingTable option that is a function returning a Promise that resolves to the pricing page session object. Typically this function calls your backend and returns response.json().

4. Call pricingTable.open() – Get the pricing table component from your Chargebee instance, then call open() with that function. Chargebee.js will use the resolved session to render the table in the div with id="chargebee-pricing-table".

<script src="https://js.chargebee.com/v2/chargebee.js" onload="onLoad()"></script>

<script>
async function onLoad() {
const chargebee = Chargebee.init({
    site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN",
})
const pricingTable = await chargebee.pricingTable();
pricingTable.open({
  pricingTable: function() {
    return fetch('https://YOUR_DOMAIN/api/create_pricing_table', {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        subscription_id: "SUBSCRIPTION_ID",  // Required
      }),
    }).then(response => response.json());
  },
});
</script>

<div
  id="chargebee-pricing-table"
  data-pricing-table-integration-type="api"
></div>

After this step, the pricing table should appear inside the <div> you configured.

Was this tutorial helpful ?
Need more help?

We're always happy to help you with any questions you might have! Click here to reach out to us.