Skip to main content

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.

AttributeTypeDescription
idstringUnique identifier for the credit transaction object.
customer_idstringID of the customer associated with this transaction.
credit_grant_idstringID of the credit grant from which this transaction originated.
meter_idstring(Optional) ID of the meter if the transaction is a consumption event.
subscription_idstring(Optional) ID of the subscription associated with this transaction.
meter_event_idstring(Optional) ID of the meter event that triggered a consumption.
typestringThe type of transaction. Can be grant, consumption, adjustment, or expiration.
amountstringThe 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).
running_balancestringThe customer's credit balance after this transaction occurred.
descriptionstring(Optional) An internal description of the transaction.
livemodebooleantrue if the transaction was created in live mode, false for test mode.
created_atstringTimestamp of when the object was created.
updated_atstringTimestamp of the last update to the object.
metadataobject(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

javascript
const transaction = await payment.creditTransactions.retrieve(
  'ct_xxxxxxxxxxxxxx'
);

console.log(transaction);

Parameters

NameTypeDescription
idstringRequired. 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

json
{
  "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": {
    "id": "sub_yz123456",
    "description": "Premium Plan"
  },
  "creditGrant": {
    "id": "cg_ijklmnop",
    "name": "Initial Grant"
  }
}

List Credit Transactions

Returns a paginated list of credit transactions. You can filter the list based on various parameters.

List Transactions

javascript
const transactions = await payment.creditTransactions.list({
  customer_id: 'cus_xxxxxxxxxxxxxx',
  limit: 10,
});

console.log(transactions);

Parameters

NameTypeDescription
pagenumberOptional. The page number for pagination, starting at 1. Defaults to 1.
pageSizenumberOptional. The number of items to return per page. Defaults to 20.
customer_idstringOptional. Filter transactions for a specific customer.
subscription_idstringOptional. Filter transactions for a specific subscription.
credit_grant_idstringOptional. Filter transactions originating from a specific credit grant.
meter_idstringOptional. Filter transactions related to a specific meter.
sourcestringOptional. Filter by the source of the transaction.
startnumberOptional. A Unix timestamp to filter for transactions created on or after this time.
endnumberOptional. A Unix timestamp to filter for transactions created on or before this time.
livemodebooleanOptional. Specify whether to fetch live or test mode transactions.
qstringOptional. A general search query string.

Returns

Returns a paginated list of TCreditTransactionExpanded objects.

Response Example

json
{
  "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

javascript
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

NameTypeDescription
customer_idstringOptional. The ID of the customer to summarize usage for.
subscription_idstringOptional. The ID of the subscription to summarize usage for.
currency_idstringOptional. Filter summary by a specific credit currency.
meter_idstringOptional. Filter summary by a specific meter.
startnumberOptional. A Unix timestamp for the beginning of the reporting period.
endnumberOptional. 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

json
{
  "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"
  }
}