Skip to main content

Meter Events

Meter Events are records of usage that you report to a specific Meter. Each event represents a quantifiable action performed by a customer, which can

Meter Events are records of usage that you report to a specific Meter. Each event represents a quantifiable action performed by a customer, which can then be used to deduct credits from their balance in a usage-based or credit billing model. Before reporting events, you must first create a Meter.

This API allows you to create, retrieve, list, and analyze meter events.

The Meter Event Object

A Meter Event object represents a single reported usage event. The expanded form includes related objects like the customer and subscription.

AttributeTypeDescription
idstringUnique identifier for the meter event.
event_namestringThe name of the event, which corresponds to the event_name of an associated Meter.
payloadobjectContains the core details of the event, such as the customer ID and usage value.
identifierstringA unique identifier for the event provided by you to ensure idempotency. Subsequent create calls with the same identifier will not create a new event.
statusstringThe processing status of the event. Can be pending, processed, failed, requires_action, or requires_capture.
livemodebooleantrue if the event was created in live mode, false for test mode.
credit_consumedstringThe amount of credit consumed by this event after processing.
credit_pendingstringThe amount of credit that is pending consumption for this event.
timestampnumberThe Unix timestamp when the event occurred.
created_atstringThe timestamp when the event was created in the system.
metadataobjectA set of key-value pairs that you can attach to the event.
customerobject(Expanded) The full Customer object associated with this event.
subscriptionobject(Expanded) The full Subscription object if the event is linked to one.
meterobject(Expanded) The full Meter object associated with this event.

Create a Meter Event

Reports a usage event to the system. This action is idempotent; if an event with the same identifier already exists, the system will not create a new one.

Create a Meter Event

javascript
const event = await payment.meterEvents.create({
  event_name: 'api_calls',
  identifier: 'unique-event-id-12345',
  payload: {
    customer_id: 'cus_xxxxxxxxxxxxxx',
    value: '100',
    subscription_id: 'sub_xxxxxxxxxxxxxx' // Optional
  },
  timestamp: Math.floor(Date.now() / 1000),
  metadata: {
    region: 'us-west'
  }
});

Parameters

NameTypeDescription
event_namestringRequired. The name of the event. This must match the event_name of an active Meter.
identifierstringRequired. A unique string to identify this event, used for idempotency. Maximum 255 characters.
payloadobjectRequired. An object containing the event's core data. See details below.
timestampnumberOptional. A Unix timestamp representing when the event occurred. If not provided, it defaults to the time of the API call.
metadataRecord<string, any>Optional. A set of key-value pairs to store additional information about the event.

Payload Object Properties

NameTypeDescription
customer_idstringRequired. The ID of the customer who triggered the event.
valuestringRequired. The amount of usage to report. This should be a positive number represented as a string.
subscription_idstringOptional. The ID of the subscription to associate this usage with. The subscription must consume credits.

Returns

Returns the created Meter Event object. Upon creation, the event is queued for asynchronous processing. The returned object includes a processing field that indicates its queued status.

Response

json
{
  "id": "mevt_xxxxxxxxxxxxxx",
  "event_name": "api_calls",
  "payload": {
    "customer_id": "cus_xxxxxxxxxxxxxx",
    "value": "100",
    "subscription_id": "sub_xxxxxxxxxxxxxx"
  },
  "identifier": "unique-event-id-12345",
  "status": "pending",
  "livemode": false,
  "credit_consumed": "0",
  "credit_pending": "100",
  "timestamp": 1678886400,
  "created_at": "2023-03-15T12:00:00.000Z",
  "metadata": {
    "region": "us-west"
  },
  "processing": {
    "queued": true,
    "message": "Credit consumption will be processed asynchronously"
  }
}

Retrieve a Meter Event

Retrieves the details of a specific meter event by its unique ID.

Retrieve a Meter Event

javascript
const eventId = 'mevt_xxxxxxxxxxxxxx';
const event = await payment.meterEvents.retrieve(eventId);

Parameters

NameTypeDescription
idstringRequired. The unique identifier of the meter event to retrieve.

Returns

Returns the Meter Event object, including expanded customer, subscription, and meter details if available.

Response

json
{
  "id": "mevt_xxxxxxxxxxxxxx",
  "event_name": "api_calls",
  "payload": {
    "customer_id": "cus_xxxxxxxxxxxxxx",
    "value": "100"
  },
  "identifier": "unique-event-id-12345",
  "status": "processed",
  "livemode": false,
  "credit_consumed": "100",
  "credit_pending": "0",
  "timestamp": 1678886400,
  "created_at": "2023-03-15T12:00:00.000Z",
  "metadata": {},
  "customer": { /* Customer object */ },
  "subscription": { /* Subscription object */ },
  "meter": { /* Meter object */ }
}

List Meter Events

Returns a paginated list of meter events. You can filter the list using various parameters.

List Meter Events

javascript
const events = await payment.meterEvents.list({
  customer_id: 'cus_xxxxxxxxxxxxxx',
  start: 1672531200, // January 1, 2023
  limit: 10
});

Parameters

NameTypeDescription
customer_idstringOptional. Filter events by a specific customer ID.
meter_idstringOptional. Filter events associated with a specific meter ID.
event_namestringOptional. Filter events by their name.
startnumberOptional. A Unix timestamp to filter events created on or after this time.
endnumberOptional. A Unix timestamp to filter events created on or before this time.
livemodebooleanOptional. Filter events by their livemode status.
qstringOptional. A general search query string.
pagenumberOptional. The page number for pagination, starting from 1.
pageSizenumberOptional. The number of events to return per page. Defaults to 20.

Returns

Returns a paginated object containing a list of Meter Event objects.

Response

json
{
  "count": 15,
  "list": [
    {
      "id": "mevt_xxxxxxxxxxxxxx",
      "event_name": "api_calls",
      // ... other event properties
    }
    // ... more events
  ],
  "paging": {
    "page": 1,
    "pageSize": 10
  }
}

Get Meter Event Stats

Retrieves aggregated statistics for a given meter over a specified time period.

Get Meter Event Stats

javascript
const stats = await payment.meterEvents.stats({
  meter_id: 'mtr_xxxxxxxxxxxxxx',
  start: 1672531200, // January 1, 2023
  end: 1675209600,   // February 1, 2023
  granularity: 'day'
});

Parameters

NameTypeDescription
meter_idstringRequired. The ID of the meter to retrieve statistics for.
startnumberRequired. A Unix timestamp for the beginning of the time range.
endnumberRequired. A Unix timestamp for the end of the time range.
customer_idstringOptional. Filter statistics for a specific customer.
granularitystringOptional. The temporal granularity of the statistics. Can be 'minute', 'hour', or 'day'. Defaults to 'day'.

Returns

Returns a list of statistical data points, aggregated by the specified granularity.

Response

json
{
  "count": 31,
  "list": [
    {
      "date": "2023-01-01",
      "timestamp": "2023-01-01T00:00:00.000Z",
      "event_count": 500,
      "total_value": "5000"
    },
    {
      "date": "2023-01-02",
      "timestamp": "2023-01-02T00:00:00.000Z",
      "event_count": 750,
      "total_value": "7500"
    }
    // ... more data points
  ]
}

Get Pending Amount

Calculates the total value of meter events that are pending processing and require action (e.g., have not yet consumed credit). This is useful for understanding outstanding usage.

Get Pending Amount

javascript
const pending = await payment.meterEvents.pendingAmount({
  customer_id: 'cus_xxxxxxxxxxxxxx'
});

Parameters

NameTypeDescription
subscription_idstringOptional. Filter by a specific subscription ID.
customer_idstringOptional. Filter by a specific customer ID.
currency_idstringOptional. Filter by a specific currency ID.

Returns

Returns an object summarizing the total pending amounts, grouped by currency.

Response

json
{
  "currency_id": "ccy_xxxxxxxxxxxxxx",
  "total_pending_amount": "12500",
  "currency": {
    "id": "ccy_xxxxxxxxxxxxxx",
    "name": "Credit",
    "decimal": 2
  }
}