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 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.
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the meter event. |
|
| The name of the event, which corresponds to the |
|
| Contains the core details of the event, such as the customer ID and usage value. |
|
| A unique identifier for the event provided by you to ensure idempotency. Subsequent |
|
| The processing status of the event. Can be |
|
|
|
|
| The amount of credit consumed by this event after processing. |
|
| The amount of credit that is pending consumption for this event. |
|
| The Unix timestamp when the event occurred. |
|
| The timestamp when the event was created in the system. |
|
| A set of key-value pairs that you can attach to the event. |
|
| (Expanded) The full Customer object associated with this event. |
|
| (Expanded) The full Subscription object if the event is linked to one. |
|
| (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
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#
Name | Type | Description |
|---|---|---|
|
| Required. The name of the event. This must match the |
|
| Required. A unique string to identify this event, used for idempotency. Maximum 255 characters. |
|
| Required. An object containing the event's core data. See details below. |
|
| Optional. A Unix timestamp representing when the event occurred. If not provided, it defaults to the time of the API call. |
|
| Optional. A set of key-value pairs to store additional information about the event. |
Payload Object Properties
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the customer who triggered the event. |
|
| Required. The amount of usage to report. This should be a positive number represented as a string. |
|
| Optional. 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
{
"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
const eventId = 'mevt_xxxxxxxxxxxxxx';
const event = await payment.meterEvents.retrieve(eventId);Parameters#
Name | Type | Description |
|---|---|---|
|
| Required. 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
{
"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
const events = await payment.meterEvents.list({
customer_id: 'cus_xxxxxxxxxxxxxx',
start: 1672531200, // January 1, 2023
limit: 10
});Parameters#
Name | Type | Description |
|---|---|---|
|
| Optional. Filter events by a specific customer ID. |
|
| Optional. Filter events associated with a specific meter ID. |
|
| Optional. Filter events by their name. |
|
| Optional. A Unix timestamp to filter events created on or after this time. |
|
| Optional. A Unix timestamp to filter events created on or before this time. |
|
| Optional. Filter events by their |
|
| Optional. A general search query string. |
|
| Optional. The page number for pagination, starting from 1. |
|
| Optional. The number of events to return per page. Defaults to 20. |
Returns#
Returns a paginated object containing a list of Meter Event objects.
Response
{
"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
const stats = await payment.meterEvents.stats({
meter_id: 'mtr_xxxxxxxxxxxxxx',
start: 1672531200, // January 1, 2023
end: 1675209600, // February 1, 2023
granularity: 'day'
});Parameters#
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the meter to retrieve statistics for. |
|
| Required. A Unix timestamp for the beginning of the time range. |
|
| Required. A Unix timestamp for the end of the time range. |
|
| Optional. Filter statistics for a specific customer. |
|
| Optional. The temporal granularity of the statistics. Can be |
Returns#
Returns a list of statistical data points, aggregated by the specified granularity.
Response
{
"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
const pending = await payment.meterEvents.pendingAmount({
customer_id: 'cus_xxxxxxxxxxxxxx'
});Parameters#
Name | Type | Description |
|---|---|---|
|
| Optional. Filter by a specific subscription ID. |
|
| Optional. Filter by a specific customer ID. |
|
| Optional. Filter by a specific currency ID. |
Returns#
Returns an object summarizing the total pending amounts, grouped by currency.
Response
{
"currency_id": "ccy_xxxxxxxxxxxxxx",
"total_pending_amount": "12500",
"currency": {
"id": "ccy_xxxxxxxxxxxxxx",
"name": "Credit",
"decimal": 2
}
}