Introducing the Chargebee CLI

Prepaid included usage: Offer separate usage grant for each product with a hard cap

Overview

If your business uses fully prepaid pricing, where your customer gets a specific included usage limit for each product with the plan, this guide shows you how to build this model in Chargebee Billing. You define each product as a metered feature, link it to the plan, and set the included usage the customer is entitled to when they buy the plan. Your application can cut off access to that product or top up when the limit is reached.

This is the implementation companion to the Included usage with hard caps recipe for billing administrators and developers who are implementing it end-to-end in the Chargebee Billing app and with the API. See Usage-based billing use cases for other billing cadence combinations.

Throughout this guide, you configure the model for a single running example. Acme Inc. sells two monthly plans. Both grant included usage for API Calls, Storage, and Messages. The quantities differ by plan:

PlanAPI calls includedStorage includedMessages included
Growth50,00050 GB10,000
Enterprise200,000200 GB40,000

John Doe subscribes to Growth on January 1: $99 per month, with 50,000 API calls, 50 GB of storage, and 10,000 messages included. The $99 invoice is the full charge for that term. Crossing any quota does not create an overage invoice. Enterprise is a higher monthly flat fee, with 200,000 API calls, 200 GB of storage, and 40,000 messages.

How this billing model works

The illustration below explains how this billing model works:

Before you start

Confirm your access. You need an admin role to change site configuration and build the Product Catalog.

Advanced Usage-Based Billing is enabled by default for new Chargebee Billing sites. If you are an existing user, enable it before you build the model.

Validate before go-live

Before enabling this on a live site, validate one full monthly cycle on a test site with Time Machine. Create a Growth subscription, ingest usage below the included API calls, storage, and messages, then ingest usage that crosses 50,000 API calls, 50 GB, or 10,000 messages. Confirm that the usage summary shows the totals.

When you enter Time Machine, existing subscriptions and customer details on the test site are erased. Export the data first if you need a backup.

When you are ready to go live, copy the configuration and Product Catalog from the test site to the live site with Transfer Configurations. To transfer customer, subscription, or invoices data, you can import them on your live site.

Implementation steps

Follow these steps in order. Each step shows how to do the task in the Chargebee Billing app and with the API.

1. Send usage into Chargebee Billing

Start by streaming your product's usage into Chargebee Billing before you have finalized the included quotas. For Acme, a usage event is generated every time an API call is made, and it includes a number_of_api_calls property. A separate event records storage, and it includes a storage_gb property. A third event records messages sent, and it includes a number_of_messages property. For your business, this could be words processed, image generations, file uploads, or data stored.

Chargebee's ingestion is schemaless. This example meters API calls, storage, and messages. You can track additional properties on the same event and decide later which of them to price.

You can add any additional properties relevant to your product, such as request_id or user_id. Chargebee Billing stores them alongside the required attributes, deduplicates repeated events, and keeps the raw stream queryable.

You can send usage in one of the following ways, based on how your product emits usage:

Using the app

CSV bulk upload. Export a CSV from your data warehouse and upload it in Chargebee Billing. Each row must include a deduplication ID, subscription ID, and usage timestamp in milliseconds.

  1. Go to Usages > Usage Events > Add Usage Events.
  2. Select the Quick file upload tab, or go to Settings > Import & Export Data > Choose a Bulk Operation, then select Usages > Create a Usage.
  3. Upload the file and click Proceed to review.
  4. Review the data, then click Import Events.

You can also add a small number of events manually: go to Usages > Usage Events > Add Usage Events, select Enter usage events, and click Import Events.

Using the API

Send events in real time or in micro-batches as they happen. You can send both raw and aggregated usage events using Ingest a usage event and Ingest usage events in batch. Usage events use the ingest host ({site}.ingest.chargebee.com), not the standard Chargebee API host.

Ingest a single usage event

curl  https://{site}.ingest.chargebee.com/api/v2/usage_events \
     -u {site_api_key}:\
     --header 'Content-Type: application/json;charset=UTF-8' \
     --data '{
     "deduplication_id": "usage-api-0001",
     "subscription_id": "{subscription_id}",
     "usage_timestamp": "1737612931000",
     "properties": {
          "number_of_api_calls": 12,
          "request_id": "req_1001"
     }
}'

Ingest usage events in batch (up to 500 events per request)

curl  https://{site}.ingest.chargebee.com/api/v2/batch/usage_events \
     -u {site_api_key}:\
     --header 'Content-Type: application/json;charset=UTF-8' \
     --data '{
     "events": [
          {
               "deduplication_id": "usage-api-0001",
               "subscription_id": "{subscription_id}",
               "usage_timestamp": "1737612931000",
               "properties": {
                    "number_of_api_calls": 12
               }
          },
          {
               "deduplication_id": "usage-msg-0001",
               "subscription_id": "{subscription_id}",
               "usage_timestamp": "1737612991000",
               "properties": {
                    "number_of_messages": 3
               }
          }
     ]
}'
Using Amazon S3

Upload usage event files from an S3 bucket. Chargebee Billing picks them up and processes them automatically. See Ingesting usage from Amazon S3.

Backdated ingestion limits differ by method and site type. On a live site, the Usage Events API accepts events from the last 12 hours (configurable on request); CSV and S3 uploads accept events from the last 30 days. On a test site, the API window is 12 hours and file uploads accept events from the last year. See Usage-Based Billing limits.

2. Define the unit of value your product delivers

Now that usage is flowing, decide what you will include in the plan. In Chargebee Billing, you capture that unit by defining a metered feature.

A metered feature tells Chargebee how to aggregate raw events into a single number for each period. You set the aggregation rule on the feature, and from that point Chargebee rolls up matching events against it.

For Acme, create three metered features. API Calls uses the aggregation method SUM on the number_of_api_calls property. Storage uses SUM on the storage_gb property. Messages uses SUM on the number_of_messages property. The steps below walk through API Calls. Repeat them for Storage and Messages.

Using the app
  1. Go to Usages > Metered Features and click Create Metered Feature.
  2. Enter a name for the feature, such as API Calls.
  3. Under Usage Calculation, select Sum and select the number_of_api_calls event property.
  4. Review the usage calculation formula and preview, then click Create.
FieldValue
NameAPI Calls
Feature IDapi-calls
TypeMetered
Aggregation methodSUM. You can select Sum, Count, Min, Max, Average, or Count Distinct.
Event property to aggregatenumber_of_api_calls

For Storage, set the name to Storage, the feature ID to storage, the aggregation method to Sum, and the event property to storage_gb. For Messages, set the name to Messages, the feature ID to messages, the aggregation method to Sum, and the event property to number_of_messages.

Using the API

Use Create a metered feature.

The aggregation method is expressed as a SQL query over usage event properties. Chargebee Billing returns a meter whose id is also the feature ID you use in later entitlement and usage calls.

curl  https://{site}.chargebee.com/api/v2/metered_features \
     -u {site_api_key}:\
     -d name="API Calls" \
     -d feature_unit="api_call" \
     -d description="Number of API calls included in the plan." \
     -d query="SELECT SUM(number_of_api_calls) from events" \
     -d "column_definitions[column_name][0]"="number_of_api_calls" \
     -d "column_definitions[data_type][0]"="NUMBER"

Create Storage with the same request shape. Aggregate storage_gb, and use the returned id as the feature ID in later entitlement and usage calls.

curl  https://{site}.chargebee.com/api/v2/metered_features \
     -u {site_api_key}:\
     -d name="Storage" \
     -d feature_unit="gb" \
     -d description="Storage included in the plan, in gigabytes." \
     -d query="SELECT SUM(storage_gb) from events" \
     -d "column_definitions[column_name][0]"="storage_gb" \
     -d "column_definitions[data_type][0]"="NUMBER"

Create Messages the same way. Aggregate number_of_messages.

curl  https://{site}.chargebee.com/api/v2/metered_features \
     -u {site_api_key}:\
     -d name="Messages" \
     -d feature_unit="message" \
     -d description="Messages included in the plan." \
     -d query="SELECT SUM(number_of_messages) from events" \
     -d "column_definitions[column_name][0]"="number_of_messages" \
     -d "column_definitions[data_type][0]"="NUMBER"

3. Define the included quotas for your plans

This step creates the monthly plans, sets their prices, and defines how much API usage, storage, and messages each plan includes.

3a. Create the plans

Create Growth and Enterprise. Each plan carries its own monthly platform fee. You link pricing and the included quotas in the next steps.

Using the app
  1. Go to Product Catalog > Plans > + Create Plan.
  2. Select a product family, enter the plan details, and click Create. Repeat for the second plan.
Internal NamePlan ID
Growthgrowth
Enterpriseenterprise
Using the API

Create the plan item with Create an item. Replace {item_family_id} with your product family ID.

curl  https://{site}.chargebee.com/api/v2/items \
     -u {site_api_key}:\
     -d id="growth" \
     -d name="Growth" \
     -d type="PLAN" \
     -d item_family_id="{item_family_id}"

Create Enterprise with id="enterprise" and name="Enterprise".

3b. Define the plan price points

Once each plan is saved, create a monthly price point for it. Growth is $99. Enterprise uses the same flat-fee model at a higher monthly price.

Using the app
  1. Open the plan details page. In the Pricing section, click Set Price for the monthly frequency and currency you want.
  2. Configure the pricing as follows, then click Create.
FieldGrowthEnterprise
Pricing modelFlat feeFlat fee
Price$99$200
Billing frequencyMonthlyMonthly
Using the API

Create the monthly price point with Create an item price. price is in the minor unit of the currency: 9900 is $99.00.

curl  https://{site}.chargebee.com/api/v2/item_prices \
     -u {site_api_key}:\
     -d id="growth-USD-monthly" \
     -d item_id="growth" \
     -d name="Growth USD monthly" \
     -d pricing_model="FLAT_FEE" \
     -d price=9900 \
     -d currency_code="USD" \
     -d period_unit="MONTH" \
     -d period=1

Create the Enterprise monthly price the same way. Use id="enterprise-USD-monthly", item_id="enterprise", and the Enterprise price in the minor unit of the currency.

Link API Calls, Storage, and Messages to both plans and set the included quantity on each. These entitlements are the hard caps for the billing period.

Using the app
  1. Go to Usages > Metered Features and select API Calls.
  2. Click Link to items.
  3. Select the Growth plan and click Next: Set Included Usage.
  4. Set included usage to 50,000, then click Link to Items.
  5. Repeat for Storage on Growth (50 GB), Messages on Growth (10,000), and for all three features on Enterprise (200,000 API calls, 200 GB, and 40,000 messages).
Using the API

Grant the included quota with Manage entitlements for a feature. Use the metered feature ID returned when you created the feature.

curl  https://{site}.chargebee.com/api/v2/entitlements \
     -u {site_api_key}:\
     -d action="UPSERT" \
     -d "entitlements[feature_id][0]"="api-calls" \
     -d "entitlements[entity_id][0]"="growth" \
     -d "entitlements[entity_type][0]"="PLAN" \
     -d "entitlements[value][0]"="50000"

Repeat the request for the other included quantities: Storage on Growth (feature_id storage, value 50), Messages on Growth (feature_id messages, value 10000), API Calls on Enterprise (entity_id enterprise, value 200000), Storage on Enterprise (value 200), and Messages on Enterprise (value 40000).

4. Restrict usage within a cap using site-level thresholds

4a. Create a usage alert for the included quota

Create the usage alerts before customers can reach a ceiling. A usage alert is a site-level rule. Chargebee Billing evaluates it as usage events are processed. A percentage threshold follows the included usage on each subscription, so the same alert fires at 50,000 API calls on Growth and at 200,000 API calls on Enterprise.

Usage alerts deliver the webhook this model uses when a customer approaches or reaches an included quota. Configure a webhook endpoint under Settings > Configure Chargebee > API Keys and Webhooks, on the Webhooks tab. See Usage Alerts.

Using the app
  1. Go to Usages > Alerts and click Create Alert.
  2. Enter an alert name and description.
  3. Select the API Calls metered feature.
  4. Enter the percentage of included usage that should trigger the alert. Use 80 for an early warning and create a second alert at 100 for the exhausted quota.
  5. Under Apply alert to, choose All subscriptions. That applies the threshold across Growth and Enterprise. Filter to one plan price only when the alert should not cover every plan.
  6. Click Manage connected webhooks and select the endpoint that will receive alert_status_changed.
  7. Click Save Alert.
  8. Repeat for the Storage and Messages metered features.
Using the API

Create a percentage-based usage alert with Create an alert. This example fires when API calls reach 80 percent of included usage on any subscription:

curl  https://{site}.chargebee.com/api/v2/alerts \
     -u {site_api_key}:\
     -d type="usage_exceeded" \
     -d name="API calls 80 percent" \
     -d description="Notify when API calls cross 80 percent of included usage" \
     -d metered_feature_id="api-calls" \
     -d "threshold[mode]"="percentage" \
     -d "threshold[value]"=80

Create a second alert with "threshold[value]"=100 for the exhausted quota. Create the same pair for Storage with metered_feature_id="storage" and for Messages with metered_feature_id="messages". To limit an alert to one plan price, pass filter_conditions for plan_price_id. To scope an alert to one subscription, pass subscription_id.

4b. Take action when the threshold is reached

The included entitlement is a quota Chargebee Billing can measure and alert on. It does not, by itself, stop the customer from using your product. Your application enforces the commercial response. Pick one response, or combine them. For example, warn at 80 percent, offer an upgrade, and cut off access at 100 percent.

Use a usage_exceeded alert at 100 percent of included usage. When alert_status_changed reports in_alarm, your application rejects the next product action and stops sending usage events for that subscription.

Chargebee Billing keeps accepting usage events after the quota is exhausted and does not enforce this stop. Access control lives in your product. If the customer can still use the product, your application will keep ingesting events, and the running total will move past the included quota. On Growth, that quota is 50,000 API calls, 50 GB of storage, or 10,000 messages. Those additional units are not covered by the plan fee. If the subscription later has a metered addon, Chargebee Billing bills them as overages. If you intend a hard stop, block the product action first, then stop ingestion.

Check remaining usage with Retrieve usage summary for a subscription before you accept a request, so a single large request cannot land after the quota is already exhausted.

5. Create the customer and subscription

With the catalog in place, create the customer and their subscription. A customer record must exist before a subscription can be created. You can create it first, or create both together in the same flow. Create the subscription for Acme's contact, John Doe, on the Growth monthly plan. Do not attach a metered addon.

Using the app
  1. Go to Customers and create a customer record for John Doe (Acme Inc.) if one doesn't already exist.
  2. From the customer, create a subscription and add the Growth monthly plan item price.
  3. Review and create the subscription.
Using the API

Create the customer with Create a customer:

curl  https://{site}.chargebee.com/api/v2/customers \
     -u {site_api_key}:\
     -d first_name="John" \
     -d last_name="Doe" \
     -d company="Acme Inc." \
     -d email="jdoe@example.com"

Create the subscription with Create a subscription for items:

curl  https://{site}.chargebee.com/api/v2/customers/{customer_id}/subscription_for_items \
     -u {site_api_key}:\
     -d "subscription_items[item_price_id][0]"="growth-USD-monthly"

When you create the subscription, you can override the plan's default entitlement to give this customer a custom included quota. This is useful for negotiated deals where the included usage differs from the standard plan. For example, grant Acme 80,000 API calls for this term and leave the Growth plan at 50,000 for every other customer. You can set the override at creation or update it any time after.

curl  https://{site}.chargebee.com/api/v2/subscriptions/{subscription_id}/entitlement_overrides \
     -X POST \
     -u {site_api_key}:\
     -d action="UPSERT" \
     -d "entitlement_overrides[feature_id][0]"="api-calls" \
     -d "entitlement_overrides[value][0]"="80000"

See Upsert or remove entitlement overrides for a subscription.

Your active subscription looks like this:

Line itemBilling frequencyInvoicing
Growth (non-metered)Monthly$99 invoiced at the start of the term, granting 50,000 included API calls, 50 GB of storage, and 10,000 messages. Usage beyond any entitlement is recorded and is not invoiced.

6. Monitor included usage during the term

Check consumed usage against the included quotas during the term. On Growth, those quotas are 50,000 API calls, 50 GB of storage, and 10,000 messages.

Using the app

Open the subscription and view cumulative usage against the included quotas under the Usage summary section.

You can share Usage PDF with your customers after an invoice is raised for them to know usages accounted for.

Using the API

Retrieve the usage summary to show consumed usage for a feature over a reporting window. If you omit timeframe_start and timeframe_end, Chargebee Billing defaults to the start of the current subscription term through now.

curl  https://{site}.chargebee.com/api/v2/subscriptions/{subscription_id}/usage_summary \
     -G \
     -u {site_api_key}:\
     --data-urlencode feature_id="api-calls"

Query storage the same way with feature_id set to storage, and messages with feature_id set to messages. See Retrieve usage summary for a subscription.

You can allow your customers to see the remaining usage for each feature in your product portal using the same APIs.

Decide what happens when included usage is exhausted

This guide's primary path is a fully prepaid hard cap. When a usage alert at 100 percent of included usage changes to in_alarm, your application cuts off access and stops sending usage events. Chargebee Billing measures the quota and sends the alert_status_changed webhook. You can take an action to stop product usage with a webhook event.

You can offer another commercial response instead of, or alongside, that cut-off:

Summary

This guide showed you how to:

  • Turn raw usage into metered features and grant different included quantities on flat-fee plans.
  • Subscribe a customer without a metered addon, so each included quantity is a ceiling rather than the start of an overage invoice.
  • Use a site-level usage alert and the alert_status_changed webhook to cut off access when included usage is exhausted.

See also