Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
API Reference

Credit Transactions


Credit Transactions are immutable records tracking the grant, consumption, adjustment, and expiration of credits. They provide a detailed ledger of all credit-related activities for each customer, which is essential for auditing and understanding usage patterns.

Each transaction is linked to a customer and a credit_grant. For more information on how credits are issued, see the Credit Grants documentation. To understand how usage is tracked, refer to Meters and Meter Events.

The Credit Transaction Object#

A credit transaction object contains detailed information about a single credit event.

Attribute

Type

Description

id

string

Unique identifier for the credit transaction.

customer_id

string

The ID of the customer associated with this transaction.

credit_grant_id

string

The ID of the credit grant from which this transaction originates.

meter_id

string

(Optional) The ID of the meter that triggered a consumption transaction.

subscription_id

string

(Optional) The ID of the subscription related to this transaction.

meter_event_id

string

(Optional) The ID of the meter event that led to this consumption.

type

string

The type of transaction. Can be grant, consumption, adjustment, or expiration.

amount

string

The amount of credits transacted. This is positive for grants and adjustments that add credit, and negative for consumptions and expirations.

running_balance

string

The credit balance of the grant after this transaction occurred.

description

string

(Optional) An internal description of the transaction.

livemode

boolean

true if the transaction was created in live mode, false for test mode.

created_at

string

The timestamp when the transaction was created (ISO 8601 format).

updated_at

string

The timestamp of the last update (ISO 8601 format).

metadata

object

(Optional) A set of key-value pairs that you can attach to the object.

Retrieve a Transaction#

Fetches the details of a specific credit transaction by its unique ID.

Parameters

Name

Type

Description

id

string

The unique identifier of the credit transaction to retrieve.

Returns

A TCreditTransactionExpanded object containing full details of the transaction.

Example

async function getCreditTransaction(transactionId) {
try {
const transaction = await payment.creditTransactions.retrieve(transactionId);
console.log('Retrieved Transaction:', transaction);
} catch (error) {
console.error('Error retrieving transaction:', error.message);
}
}

// Replace 'crtrx_xxxxxxxx' with a valid transaction ID
getCreditTransaction('crtrx_xxxxxxxx');

Example Response

{
"id": "crtrx_xxxxxxxxxxxxxx",
"customer_id": "cust_xxxxxxxxxxxxxx",
"credit_grant_id": "crg_xxxxxxxxxxxxxx",
"meter_id": "mtr_xxxxxxxxxxxxxx",
"subscription_id": "sub_xxxxxxxxxxxxxx",
"meter_event_id": "mevt_xxxxxxxxxxxxxx",
"type": "consumption",
"amount": "-100",
"running_balance": "900",
"description": "API Call Usage",
"livemode": false,
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z",
"metadata": {}
}

List Transactions#

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

Parameters

Name

Type

Description

customer_id

string

(Optional) Filter transactions by a specific customer ID.

subscription_id

string

(Optional) Filter transactions by a specific subscription ID.

credit_grant_id

string

(Optional) Filter transactions by a specific credit grant ID.

start

number

(Optional) A Unix timestamp to filter for transactions created after this time.

end

number

(Optional) A Unix timestamp to filter for transactions created before this time.

livemode

boolean

(Optional) Set to true to only return live mode objects, or false for test mode objects.

q

string

(Optional) A search query string for free-text search on relevant fields.

page

number

(Optional) The page number for pagination, starting from 1.

pageSize

number

(Optional) The number of items to return per page. Defaults to 20.

order

string[] or string

(Optional) The order in which to sort the results (e.g., ['created_at', 'DESC']).

Returns

A paginated object containing a list of credit transaction objects.

Name

Type

Description

count

number

The total number of transactions matching the query.

list

TCreditTransactionExpanded[]

An array of credit transaction objects for the current page.

Example

async function listCustomerTransactions(customerId) {
try {
const transactions = await payment.creditTransactions.list({
customer_id: customerId,
pageSize: 5
});
console.log(`Found ${transactions.count} transactions.`);
transactions.list.forEach(tx => {
console.log(`- ID: ${tx.id}, Type: ${tx.type}, Amount: ${tx.amount}`);
});
} catch (error) {
console.error('Error listing transactions:', error.message);
}
}

// Replace 'cust_xxxxxxxx' with a valid customer ID
listCustomerTransactions('cust_xxxxxxxx');

Example Response

{
"count": 25,
"list": [
{
"id": "crtrx_xxxxxxxxxxxxxx",
"customer_id": "cust_xxxxxxxx",
"credit_grant_id": "crg_xxxxxxxxxxxxxx",
"type": "consumption",
"amount": "-50",
"running_balance": "850",
"livemode": false,
"created_at": "2023-10-27T11:00:00.000Z",
"updated_at": "2023-10-27T11:00:00.000Z"
},
{
"id": "crtrx_yyyyyyyyyyyyyy",
"customer_id": "cust_xxxxxxxx",
"credit_grant_id": "crg_xxxxxxxxxxxxxx",
"type": "grant",
"amount": "1000",
"running_balance": "1000",
"livemode": false,
"created_at": "2023-10-01T09:00:00.000Z",
"updated_at": "2023-10-01T09:00:00.000Z"
}
]
}

Get Usage Summary#

Provides a summarized view of credit usage over a specified period for a customer or subscription.

Parameters

Name

Type

Description

customer_id

string

(Optional) The ID of the customer for whom to generate the summary.

subscription_id

string

(Optional) The ID of the subscription for which to generate the summary.

currency_id

string

(Optional) Filter summary by a specific currency ID.

start

number

(Optional) A Unix timestamp defining the beginning of the summary period.

end

number

(Optional) A Unix timestamp defining the end of the summary period.

Returns

A UsageSummary object with aggregated credit data.

Name

Type

Description

total_consumption

string

The total amount of credits consumed during the period.

total_grants

string

The total amount of credits granted during the period.

net_balance

string

The net change in credit balance (total_grants - total_consumption).

transaction_count

number

The total number of transactions in the period.

breakdown

object

An object providing a more detailed breakdown of usage.

breakdown.by_type

object

Usage broken down by transaction type (grant, consumption, etc.).

breakdown.by_meter

object

(Optional) Usage broken down by meter, if applicable.

Example

async function getCreditSummary(customerId) {
try {
const summary = await payment.creditTransactions.summary({
customer_id: customerId
});
console.log('Usage Summary:', summary);
} catch (error) {
console.error('Error getting usage summary:', error.message);
}
}

// Replace 'cust_xxxxxxxx' with a valid customer ID
getCreditSummary('cust_xxxxxxxx');

Example Response

{
"customer_id": "cust_xxxxxxxx",
"total_consumption": "150",
"total_grants": "1000",
"net_balance": "850",
"transaction_count": 12,
"breakdown": {
"by_type": {
"grant": {
"amount": "1000",
"count": 1
},
"consumption": {
"amount": "150",
"count": 11
}
},
"by_meter": {
"mtr_xxxxxxxxxxxxxx": {
"amount": "100",
"count": 8,
"meter_name": "API Calls"
},
"mtr_yyyyyyyyyyyyyy": {
"amount": "50",
"count": 3,
"meter_name": "Data Storage"
}
}
}
}


With these tools, you can effectively monitor and analyze credit activity within your application. For a higher-level overview of implementing a credit-based system, please see our Credit Billing guide.