Skip to main content

Meters

Meters are used to define and track usage for credit-based billing. They represent a specific feature or resource that you bill for, such as API calls, data storage, or compute time. Each meter aggregates usage events sent to it.

Once a meter is created, you can report usage against it using Meter Events. This is a core component of implementing a Credit-Based Billing model.

The Meter Object

A Meter object contains all the information about a specific usage tracker.

AttributeTypeDescription
idstringUnique identifier for the meter.
namestringThe display name of the meter.
event_namestringA unique name for the event this meter tracks. This is used when reporting usage.
aggregation_methodstringThe method used to aggregate usage. Currently, only sum is supported.
unitstringThe unit of measurement for the usage (e.g., 'requests', 'gb', 'credits').
statusstringThe current status of the meter. Can be active or inactive. Inactive meters do not accept new events.
livemodebooleantrue if the meter was created in live mode, false for test mode.
currency_idstringThe ID of the payment currency associated with this meter.
descriptionstringAn optional description of the meter.
metadataobjectA set of key-value pairs that you can attach to an object. Useful for storing additional information.
paymentCurrencyobjectThe expanded payment currency object associated with the meter.

The paymentCurrency Object Properties

AttributeTypeDescription
idstringUnique identifier for the currency.
namestringThe name of the currency (e.g., 'API Credits').
symbolstringThe symbol for the currency (e.g., 'AC').
decimalnumberThe number of decimal places for the currency.
typestringThe type of currency.

Create a Meter

Creates a new meter to track usage of a specific feature.

Parameters

AttributeTypeDescription
namestringRequired. The display name of the meter. Maximum 64 characters.
event_namestringRequired. A unique name for the event this meter tracks. This is used when reporting usage. Maximum 64 characters.
unitstringRequired. The unit of measurement for the usage (e.g., 'requests', 'gb', 'credits'). Maximum 32 characters.
aggregation_methodstringOptional. The method to aggregate usage. Defaults to sum. Currently, this is the only supported value.
currency_idstringOptional. The ID of the payment currency to associate with this meter. If not provided, a new credit currency will be created automatically.
descriptionstringOptional. A description of what this meter tracks. Maximum 255 characters.
metadataobjectOptional. A set of key-value pairs to store additional information about the meter.

Returns

Returns the newly created Meter object.

Create a Meter

javascript
import payment from '@blocklet/payment-js';

async function createMeter() {
  try {
    const meter = await payment.meters.create({
      name: 'API Calls',
      event_name: 'api.calls.v1',
      unit: 'requests',
      description: 'Tracks the number of API calls made.',
    });
    console.log('Meter created:', meter);
  } catch (error) {
    console.error('Error creating meter:', error.message);
  }
}

createMeter();

Example Response

json
{
  "id": "mtr_1J7kL2jQ6F9o3vXbY9t8rGcE",
  "name": "API Calls",
  "event_name": "api.calls.v1",
  "aggregation_method": "sum",
  "unit": "requests",
  "status": "active",
  "livemode": false,
  "currency_id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
  "description": "Tracks the number of API calls made.",
  "metadata": {},
  "created_at": "2023-10-27T10:00:00.000Z",
  "updated_at": "2023-10-27T10:00:00.000Z",
  "paymentCurrency": {
    "id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
    "name": "API Calls Credit",
    "symbol": "ACC",
    "decimal": 0,
    "type": "credit"
  }
}

Retrieve a Meter

Retrieves the details of an existing meter. You can retrieve a meter by its unique ID or its event_name.

Parameters

AttributeTypeDescription
idstringRequired. The ID or event_name of the meter to retrieve.

Returns

Returns the Meter object if found.

Retrieve a Meter

javascript
import payment from '@blocklet/payment-js';

async function retrieveMeter(meterId) {
  try {
    const meter = await payment.meters.retrieve(meterId);
    console.log('Retrieved meter:', meter);
  } catch (error) {
    console.error('Error retrieving meter:', error.message);
  }
}

retrieveMeter('mtr_1J7kL2jQ6F9o3vXbY9t8rGcE');

Example Response

json
{
  "id": "mtr_1J7kL2jQ6F9o3vXbY9t8rGcE",
  "name": "API Calls",
  "event_name": "api.calls.v1",
  "aggregation_method": "sum",
  "unit": "requests",
  "status": "active",
  "livemode": false,
  "currency_id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
  "description": "Tracks the number of API calls made.",
  "metadata": {},
  "created_at": "2023-10-27T10:00:00.000Z",
  "updated_at": "2023-10-27T10:00:00.000Z",
  "paymentCurrency": {
    "id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
    "name": "API Calls Credit",
    "symbol": "ACC",
    "decimal": 0,
    "type": "credit"
  }
}

Update a Meter

Updates an existing meter by setting the values of the parameters passed.

Parameters

AttributeTypeDescription
idstringRequired. The ID of the meter to update.
namestringOptional. A new display name for the meter.
descriptionstringOptional. An updated description for the meter.
statusstringOptional. The new status. Can be active or inactive.
unitstringOptional. A new unit of measurement for the usage.
metadataobjectOptional. A set of key-value pairs to update on the meter.

Returns

Returns the updated Meter object.

Update a Meter

javascript
import payment from '@blocklet/payment-js';

async function updateMeter(meterId) {
  try {
    const meter = await payment.meters.update(meterId, {
      name: 'API Calls (V2)',
      description: 'Tracks V2 API calls.',
    });
    console.log('Meter updated:', meter);
  } catch (error) {
    console.error('Error updating meter:', error.message);
  }
}

updateMeter('mtr_1J7kL2jQ6F9o3vXbY9t8rGcE');

Example Response

json
{
  "id": "mtr_1J7kL2jQ6F9o3vXbY9t8rGcE",
  "name": "API Calls (V2)",
  "event_name": "api.calls.v1",
  "aggregation_method": "sum",
  "unit": "requests",
  "status": "active",
  "livemode": false,
  "currency_id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
  "description": "Tracks V2 API calls.",
  "metadata": {},
  "created_at": "2023-10-27T10:00:00.000Z",
  "updated_at": "2023-10-27T10:01:00.000Z",
  "paymentCurrency": {
    "id": "pc_1J7kL2jQ6F9o3vXbY9t8rGcF",
    "name": "API Calls Credit",
    "symbol": "ACC",
    "decimal": 0,
    "type": "credit"
  }
}

List all Meters

Returns a paginated list of your meters. You can filter the list based on various criteria.

Parameters

AttributeTypeDescription
pagenumberOptional. The page number for pagination, starting at 1. Defaults to 1.
pageSizenumberOptional. The number of items to return per page. Defaults to 20.
event_namestringOptional. Filter meters by a specific event name.
livemodebooleanOptional. Filter meters by their live mode status.
qstringOptional. A search query string that filters results based on matching name or description.

Returns

Returns a paginated object containing a list of Meter objects.

AttributeTypeDescription
countnumberThe total number of meters matching the query.
listarrayAn array of Meter objects for the current page.
pagingobjectAn object containing pagination information (page, pageSize).

List all Meters

javascript
import payment from '@blocklet/payment-js';

async function listMeters() {
  try {
    const result = await payment.meters.list({
      pageSize: 5,
      livemode: false,
    });
    console.log(`Found ${result.count} meters.`);
    console.log('Meters on this page:', result.list);
  } catch (error) {
    console.error('Error listing meters:', error.message);
  }
}

listMeters();

Example Response

json
{
  "count": 20,
  "list": [
    {
      "id": "mtr_1J7kL2jQ6F9o3vXbY9t8rGcE",
      "name": "API Calls",
      "event_name": "api.calls.v1",
      "status": "active",
      "livemode": false
    },
    {
      "id": "mtr_2K8mN3kR7G0p4wYcZ0u9sHdF",
      "name": "Data Storage",
      "event_name": "data.storage.gb",
      "status": "active",
      "livemode": false
    }
  ],
  "paging": {
    "page": 1,
    "pageSize": 5
  }
}

Activate a Meter

Activates an inactive meter. Once activated, the meter will start accepting usage events.

Parameters

AttributeTypeDescription
idstringRequired. The ID of the meter to activate.

Returns

Returns the activated Meter object.

Activate a Meter

javascript
import payment from '@blocklet/payment-js';

async function activateMeter(meterId) {
  try {
    const meter = await payment.meters.activate(meterId);
    console.log(`Meter ${meter.id} is now ${meter.status}.`);
  } catch (error) {
    console.error('Error activating meter:', error.message);
  }
}

activateMeter('mtr_some_inactive_meter_id');

Deactivate a Meter

Deactivates a meter. The meter will stop accepting new usage events until it is reactivated.

Parameters

AttributeTypeDescription
idstringRequired. The ID of the meter to deactivate.

Returns

Returns the deactivated Meter object.

Deactivate a Meter

javascript
import payment from '@blocklet/payment-js';

async function deactivateMeter(meterId) {
  try {
    const meter = await payment.meters.deactivate(meterId);
    console.log(`Meter ${meter.id} is now ${meter.status}.`);
  } catch (error) {
    console.error('Error deactivating meter:', error.message);
  }
}

deactivateMeter('mtr_1J7kL2jQ6F9o3vXbY9t8rGcE');

After creating and configuring your meters, the next step is to report usage. See the Meter Events documentation to learn how.