Credit Grants
Credit Grants are objects that represent a credit amount issued to a customer. These credits can be used to offset the cost of future invoices. This API allows you to create, retrieve, update, list, and summarize credit grants for your customers.
For a higher-level guide on implementing a credit-based system, see the Credit Billing guide. To view the history of credit usage, refer to the Credit Transactions API.
The Credit Grant Object#
A Credit Grant object contains the full details of a credit issued to a customer. It includes information about the amount, status, and applicability.
Attribute | Type | Description |
|---|---|---|
| string | Unique identifier for the credit grant. |
| string | The type of object, always |
| string | The total amount of the credit grant in the smallest currency unit. |
| string | The amount of credit remaining. |
| string | The ID of the currency for the credit. |
| string | The ID of the customer who received the grant. |
| string | The category of the grant. Can be |
| string | The current status of the grant. Can be |
| string | An optional internal name for the credit grant. |
| number | A positive integer between 1 and 99999 that determines the order in which credits are applied. Lower numbers have higher priority. |
| number | Unix timestamp indicating when the grant becomes active. |
| number | Unix timestamp indicating when the grant expires. |
| object | Rules that specify where this credit grant can be applied. |
| boolean |
|
| object | A set of key-value pairs that you can attach to an object. |
Create a Credit Grant#
Issues a new credit grant to a customer.
Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The value of the credit grant, specified in the smallest currency unit. |
| string | Required. The three-letter ISO currency code (or custom currency ID) for the grant. |
| string | Required. The ID of the customer to receive the credit. |
| 'paid' | 'promotional' | Required. Specifies whether the credit is from a paid source or a promotion. |
| string | An optional internal name for this credit grant. |
| number | A positive integer (1-99999) indicating the application priority. Lower numbers are applied first. |
| number | A Unix timestamp indicating when the credit grant becomes active. If not provided, it becomes active immediately. |
| number | A Unix timestamp indicating when the credit grant expires. |
| object | An object defining the scope of the credit's applicability. Contains a |
| object | A set of key-value pairs to store additional information. |
Example
async function createCreditGrant() {
try {
const creditGrant = await payment.creditGrants.create({
customer_id: 'cust_xxxxxxxxxxxxxx',
amount: '5000',
currency_id: 'usd',
category: 'promotional',
name: 'New User Welcome Bonus',
expires_at: Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60) // 30 days from now
});
console.log('Credit Grant created:', creditGrant);
} catch (error) {
console.error('Error creating credit grant:', error.message);
}
}
createCreditGrant();Example Response
{
"id": "cgrant_1A2b3C4d5E6f7G8h",
"object": "credit_grant",
"amount": "5000",
"currency_id": "usd",
"applicability_config": null,
"category": "promotional",
"customer_id": "cust_xxxxxxxxxxxxxx",
"effective_at": 1672531200,
"expires_at": 1675123200,
"livemode": false,
"metadata": {},
"name": "New User Welcome Bonus",
"priority": 50000,
"status": "granted",
"remaining_amount": "5000",
"created_via": "api",
"created_at": "2023-01-01T00:00:00.000Z",
"updated_at": "2023-01-01T00:00:00.000Z"
}Retrieve a Credit Grant#
Fetches the details of a specific credit grant by its ID.
Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The unique identifier of the credit grant to retrieve. |
Example
async function retrieveCreditGrant(grantId) {
try {
const creditGrant = await payment.creditGrants.retrieve(grantId);
console.log('Retrieved Credit Grant:', creditGrant);
} catch (error) {
console.error('Error retrieving credit grant:', error.message);
}
}
retrieveCreditGrant('cgrant_1A2b3C4d5E6f7G8h');Example Response
{
"id": "cgrant_1A2b3C4d5E6f7G8h",
"object": "credit_grant",
"amount": "5000",
"currency_id": "usd",
"category": "promotional",
"customer_id": "cust_xxxxxxxxxxxxxx",
"status": "granted",
"remaining_amount": "5000",
// ... other properties
}Update a Credit Grant#
Updates the specified credit grant by setting the metadata parameter. Other properties of a credit grant cannot be updated.
Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The identifier of the credit grant to update. |
| object | A set of key-value pairs to store with the credit grant. |
Example
async function updateCreditGrant(grantId) {
try {
const result = await payment.creditGrants.update(grantId, {
metadata: {
internal_ref: 'promo-campaign-fall2023'
}
});
console.log('Update successful:', result.success);
} catch (error) {
console.error('Error updating credit grant:', error.message);
}
}
updateCreditGrant('cgrant_1A2b3C4d5E6f7G8h');Example Response
{
"success": true
}List all Credit Grants#
Returns a paginated list of credit grants. You can filter the list based on various parameters.
Parameters
Name | Type | Description |
|---|---|---|
| string | Filters the list to grants for a specific customer. |
| string | Filters the list by currency. |
| string | Filters the list by status (e.g., |
| boolean | Filters by live or test mode. |
| string | A search query string that searches across multiple fields. |
| number | The page number for pagination. |
| number | The number of items per page. Defaults to |
| string | string[] | The order to sort the results. |
Example
async function listCustomerCredits() {
try {
const creditGrants = await payment.creditGrants.list({
customer_id: 'cust_xxxxxxxxxxxxxx',
status: 'granted',
pageSize: 5
});
console.log(`Found ${creditGrants.count} granted credits for customer.`);
creditGrants.list.forEach(grant => {
console.log(`- Grant ${grant.id}: ${grant.remaining_amount} ${grant.currency_id}`);
});
} catch (error) {
console.error('Error listing credit grants:', error.message);
}
}
listCustomerCredits();Example Response
{
"count": 1,
"list": [
{
"id": "cgrant_1A2b3C4d5E6f7G8h",
"object": "credit_grant",
"amount": "5000",
"currency_id": "usd",
"category": "promotional",
"customer_id": "cust_xxxxxxxxxxxxxx",
"status": "granted",
"remaining_amount": "5000",
// ... other properties
}
]
}Retrieve a Credit Summary#
Provides a summary of a customer's credit balances, grouped by currency.
Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The ID of the customer for whom to retrieve the summary. |
| string | An optional subscription ID to provide context for the credit summary, which may affect which applicable credits are returned. |
Example
async function getCreditSummary(customerId) {
try {
const summary = await payment.creditGrants.summary({ customer_id: customerId });
console.log('Customer Credit Summary:', summary);
} catch (error) {
console.error('Error retrieving credit summary:', error.message);
}
}
getCreditSummary('cust_xxxxxxxxxxxxxx');Example Response
{
"customer_id": "cust_xxxxxxxxxxxxxx",
"total_balance": {
"usd": {
"currency_id": "usd",
"total_amount": "15000",
"available_amount": "7500",
"pending_amount": "0",
"currency": {
"id": "usd",
"symbol": "$",
"decimal": 2
}
},
"eur": {
"currency_id": "eur",
"total_amount": "10000",
"available_amount": "10000",
"pending_amount": "0",
"currency": {
"id": "eur",
"symbol": "€",
"decimal": 2
}
}
}
}This section detailed how to manage Credit Grants programmatically. After issuing credits, you will want to track how they are applied to invoices. Proceed to the Credit Transactions API to learn more.