Credit Transactions
Credit Transaction objects record every change to a customer's credit balance. This includes grants, consumptions, adjustments, and expirations. You can use this API to retrieve a detailed history of a customer's credit activities.
Each transaction provides a snapshot of the credit balance at the time of the event, making it a reliable ledger for auditing and tracking credit usage. For a higher-level overview of credit management, see the Credit-Based Billing guide.
The Credit Transaction Object#
A CreditTransaction object contains detailed information about a single credit event.
Attribute | Type | Description |
|---|---|---|
| string | Unique identifier for the credit transaction object. |
| string | ID of the customer associated with this transaction. |
| string | ID of the credit grant from which this transaction originated. |
| string | (Optional) ID of the meter if the transaction is a consumption event. |
| string | (Optional) ID of the subscription associated with this transaction. |
| string | (Optional) ID of the meter event that triggered a consumption. |
| string | The type of transaction. Can be |
| string | The amount of credit transacted, represented as a string. A positive value indicates credits added (e.g., a grant), and a negative value indicates credits removed (e.g., consumption). |
| string | The customer's credit balance after this transaction occurred. |
| string | (Optional) An internal description of the transaction. |
| boolean |
|
| string | Timestamp of when the object was created. |
| string | Timestamp of the last update to the object. |
| object | (Optional) A set of key-value pairs that you can attach to an object. |
Retrieve a Credit Transaction#
Retrieves the details of a specific credit transaction by its unique ID.
Retrieve a Transaction
const transaction = await payment.creditTransactions.retrieve(
'ct_xxxxxxxxxxxxxx'
);
console.log(transaction);Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The unique identifier of the credit transaction to retrieve. |
Returns
Returns a TCreditTransactionExpanded object if a valid ID is provided. Otherwise, this call returns an error.
Response Example
{
"id": "ct_123456789",
"customer_id": "cus_abcdefgh",
"credit_grant_id": "cg_ijklmnop",
"meter_id": "mtr_qrstuvwx",
"subscription_id": "sub_yz123456",
"meter_event_id": "me_7890abcd",
"type": "consumption",
"amount": "-100.00",
"running_balance": "900.00",
"description": "API Call Usage",
"livemode": true,
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z",
"metadata": {},
"customer": {
"id": "cus_abcdefgh",
"name": "John Doe",
"email": "john.doe@example.com"
},
"meter": {
"id": "mtr_qrstuvwx",
"name": "API Calls"
},
"subscription": {See all 8 lines
List Credit Transactions#
Returns a paginated list of credit transactions. You can filter the list based on various parameters.
List Transactions
const transactions = await payment.creditTransactions.list({
customer_id: 'cus_xxxxxxxxxxxxxx',
limit: 10,
});
console.log(transactions);Parameters
Name | Type | Description |
|---|---|---|
| number | Optional. The page number for pagination, starting at 1. Defaults to |
| number | Optional. The number of items to return per page. Defaults to |
| string | Optional. Filter transactions for a specific customer. |
| string | Optional. Filter transactions for a specific subscription. |
| string | Optional. Filter transactions originating from a specific credit grant. |
| string | Optional. Filter transactions related to a specific meter. |
| string | Optional. Filter by the source of the transaction. |
| number | Optional. A Unix timestamp to filter for transactions created on or after this time. |
| number | Optional. A Unix timestamp to filter for transactions created on or before this time. |
| boolean | Optional. Specify whether to fetch live or test mode transactions. |
| string | Optional. A general search query string. |
Returns
Returns a paginated list of TCreditTransactionExpanded objects.
Response Example
{
"count": 15,
"list": [
{
"id": "ct_123456789",
"customer_id": "cus_abcdefgh",
"credit_grant_id": "cg_ijklmnop",
"type": "consumption",
"amount": "-100.00",
"running_balance": "900.00",
"livemode": true,
"created_at": "2023-10-27T10:00:00.000Z",
// ... other fields and expanded objects
}
// ... more transactions
],
"paging": {
"page": 1,
"pageSize": 10
}
}Get Usage Summary#
Retrieves a summary of credit consumption over a specified period for a customer, subscription, or meter.
Get Usage Summary
const summary = await payment.creditTransactions.summary({
customer_id: 'cus_xxxxxxxxxxxxxx',
start: Math.floor(new Date('2023-01-01').getTime() / 1000),
end: Math.floor(new Date('2023-12-31').getTime() / 1000),
});
console.log(summary);Parameters
Name | Type | Description |
|---|---|---|
| string | Optional. The ID of the customer to summarize usage for. |
| string | Optional. The ID of the subscription to summarize usage for. |
| string | Optional. Filter summary by a specific credit currency. |
| string | Optional. Filter summary by a specific meter. |
| number | Optional. A Unix timestamp for the beginning of the reporting period. |
| number | Optional. A Unix timestamp for the end of the reporting period. |
Returns
Returns a UsageSummary object detailing the total consumption and transaction count within the specified filters.
Response Example
{
"total_quantity": "1500",
"total_credit_amount": "-1500.00",
"transaction_count": 42,
"filters": {
"customer_id": "cus_xxxxxxxxxxxxxx",
"start_time": "2023-01-01T00:00:00.000Z",
"end_time": "2023-12-31T00:00:00.000Z"
}
}