Credits API
Complete guide to managing credits, grants, and usage tracking via the API
Credits API
The Credits API allows you to create flexible credit systems for your customers, including usage-based credits, monetary credits, and automated renewal mechanisms. Credits can be scoped to merchants, plans, or specific features, providing granular control over customer entitlements.
Overview
The credits system consists of three main components:
- Credit Definitions: Templates that define how credits work (usage/monetary, amounts, renewal rules)
- Credit Grants: Individual credit allocations given to specific customers
- Credit Transactions: Historical record of credit usage and events
Credit Types
Usage Credits
- Applied against feature usage (API calls, storage, etc.)
- Can be linked to specific features or pricing components
- Automatically deducted when features are used
Monetary Credits
- Applied as discounts against invoice amounts
- Always billing-visible and require currency specification
- Can be merchant-wide or plan-specific
Credit Scopes
- Merchant: Credits applicable across all customer subscriptions
- Plan: Credits that only apply to specific subscription plans
Authentication
All API requests require authentication via merchant credentials.
Credit Definitions
Get All Credit Definitions
Get all credit definitions for the authenticated merchant.
GET /api/credits/definitions?scope={scope}&applicationType={applicationType}
Query Parameters
Parameter | Type | Description |
---|---|---|
scope | string | Filter by scope: merchant or plan |
applicationType | string | Filter by type: usage or monetary |
Response
{
"definitions": [
{
"id": "cd_123",
"merchantId": "merchant_123",
"name": "API Call Credits",
"description": "Credits for API usage",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"defaultAmount": 1000,
"refillAmount": 1000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 1,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": true,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"deletedAt": null
}
]
}
Get Credit Definition by ID
Get a specific credit definition by its ID.
GET /api/credits/definitions/{id}
Parameters
Parameter | Type | Description |
---|---|---|
id | string | Credit Definition ID |
Response
{
"definition": {
"id": "cd_123",
"merchantId": "merchant_123",
"name": "API Call Credits",
"description": "Credits for API usage",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"defaultAmount": 1000,
"refillAmount": 1000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 1,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": true,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"deletedAt": null
}
}
Create Credit Definition
Create a new credit definition.
POST /api/credits/definitions
Request Body
{
"name": "Storage Credits",
"description": "Credits for file storage",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_storage",
"featureSlug": "storage",
"defaultAmount": 5000,
"refillAmount": 5000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 2,
"billingVisible": false
}
Request Fields
Field | Type | Required | Description |
---|---|---|---|
name | string | ✓ | Display name for the credit definition |
scope | string | ✓ | Credit scope: merchant or plan |
applicationType | string | ✓ | Credit type: usage or monetary |
defaultAmount | number | ✓ | Default credit amount when granted |
description | string | Optional description | |
planId | string | * | Required when scope is plan |
featureId | string | * | Feature ID for usage credits |
featureSlug | string | * | Feature slug for usage credits |
priceId | string | * | Price ID for usage credits (paid plans) |
priceComponentId | string | * | Price component ID for usage credits |
refillAmount | number | Amount to refill during renewals | |
expiryDays | number | Days until credits expire | |
refillRrule | string | RRULE for automatic renewals | |
renewOnBilling | boolean | Whether to renew on billing cycle | |
priority | number | Priority for credit application (higher first) | |
billingVisible | boolean | Whether credits appear on invoices | |
billingDescription | string | Description for billing/invoices | |
currency | string | * | Required for monetary credits |
* Conditional requirements based on scope
and applicationType
Response
{
"definition": {
"id": "cd_456",
"merchantId": "merchant_123",
"name": "Storage Credits",
"description": "Credits for file storage",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_storage",
"featureSlug": "storage",
"defaultAmount": 5000,
"refillAmount": 5000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 2,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": true,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"deletedAt": null
}
}
Update Credit Definition
Update an existing credit definition.
POST /api/credits/definitions/{id}/update
Parameters
Parameter | Type | Description |
---|---|---|
id | string | Credit Definition ID |
Request Body
{
"name": "Updated Storage Credits",
"description": "Updated description",
"defaultAmount": 10000,
"refillAmount": 10000,
"expiryDays": 60,
"isActive": false
}
Request Fields
All fields are optional and only provided fields will be updated:
Field | Type | Description |
---|---|---|
name | string | Display name for the credit definition |
description | string | Optional description |
billingDescription | string | Description for billing/invoices |
applicationType | string | Credit type: usage or monetary |
scope | string | Credit scope: merchant or plan |
planId | string | Plan ID for plan-scoped credits |
featureId | string | Feature ID for usage credits |
featureSlug | string | Feature slug for usage credits |
priceId | string | Price ID for usage credits |
priceComponentId | string | Price component ID for usage credits |
defaultAmount | number | Default credit amount when granted |
refillAmount | number | Amount to refill during renewals |
expiryDays | number | Days until credits expire |
refillRrule | string | RRULE for automatic renewals |
renewOnBilling | boolean | Whether to renew on billing cycle |
priority | number | Priority for credit application (higher first) |
isActive | boolean | Whether the definition is active |
Response
{
"definition": {
"id": "cd_456",
"merchantId": "merchant_123",
"name": "Updated Storage Credits",
"description": "Updated description",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_storage",
"featureSlug": "storage",
"defaultAmount": 10000,
"refillAmount": 10000,
"expiryDays": 60,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 2,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": false,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"deletedAt": null
}
}
Delete Credit Definition
Soft delete a credit definition (preserves historical data).
POST /api/credits/definitions/{id}/delete
Parameters
Parameter | Type | Description |
---|---|---|
id | string | Credit Definition ID |
Response
{
"definition": {
"id": "cd_456",
"merchantId": "merchant_123",
"name": "Updated Storage Credits",
"description": "Updated description",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_storage",
"featureSlug": "storage",
"defaultAmount": 10000,
"refillAmount": 10000,
"expiryDays": 60,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 2,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": false,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"deletedAt": "2024-01-15T11:00:00Z"
}
}
Get Plan Credit Definitions
Get all credit definitions for a specific plan.
GET /api/credits/plans/{planId}/definitions?applicationType={applicationType}
Parameters
Parameter | Type | Description |
---|---|---|
planId | string | Plan ID |
Query Parameters
Parameter | Type | Description |
---|---|---|
applicationType | string | Filter by type: usage or monetary |
Response
{
"definitions": [
{
"id": "cd_123",
"merchantId": "merchant_123",
"name": "Plan API Credits",
"description": "API credits for this plan",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"defaultAmount": 1000,
"refillAmount": 1000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 1,
"billingVisible": false,
"billingDescription": null,
"currency": null,
"isActive": true,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"deletedAt": null
}
]
}
Credit Grants
Get Customer Credit Grants
Get all credit grants for a specific customer.
GET /api/credits/customers/{customerId}/grants?status={status}&applicationType={applicationType}
Parameters
Parameter | Type | Description |
---|---|---|
customerId | string | Customer ID |
Query Parameters
Parameter | Type | Description |
---|---|---|
status | string | Filter by status: ACTIVE , EXPIRED , REVOKED |
applicationType | string | Filter by type: usage or monetary |
Response
{
"grants": [
{
"id": "cg_123",
"merchantId": "merchant_123",
"customerId": "cust_456",
"creditDefinitionId": "cd_123",
"initialAmount": 1000,
"remainingAmount": 750,
"status": "ACTIVE",
"applicationType": "usage",
"scope": "plan",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"priceId": null,
"priceComponentId": null,
"currency": null,
"priority": 1,
"startDate": "2024-01-01T00:00:00Z",
"expiryDate": "2024-01-31T23:59:59Z",
"source": "ADMIN_GRANTED",
"referenceCode": "WELCOME_BONUS",
"notes": "Welcome bonus for new customer",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"deletedAt": null,
"creditDefinition": {
"id": "cd_123",
"name": "API Call Credits",
"description": "Credits for API usage"
}
}
]
}
Get Customer Credit Transactions
Get credit usage history for a specific customer.
GET /api/credits/customers/{customerId}/transactions?limit={limit}&fromDate={fromDate}&toDate={toDate}
Parameters
Parameter | Type | Description |
---|---|---|
customerId | string | Customer ID |
Query Parameters
Parameter | Type | Description |
---|---|---|
limit | number | Maximum number of transactions (max 1000) |
fromDate | string | Start date (ISO 8601 format) |
toDate | string | End date (ISO 8601 format) |
Response
{
"transactions": [
{
"id": "ct_123",
"merchantId": "merchant_123",
"customerId": "cust_456",
"creditGrantId": "cg_123",
"amount": 10,
"applicationType": "usage",
"eventName": "api_call",
"eventTimestamp": "2024-01-15T10:30:00Z",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"priceId": null,
"priceComponentId": null,
"currency": null,
"metadata": {
"endpoint": "/api/users",
"method": "GET"
},
"createdAt": "2024-01-15T10:30:00Z"
}
]
}
Create Credit Grant
Create a new credit grant for a customer.
POST /api/credits/grants
Request Body
{
"customerId": "cust_456",
"creditDefinitionId": "cd_123",
"initialAmount": 2000,
"startDate": "2024-01-01T00:00:00Z",
"expiryDate": "2024-12-31T23:59:59Z",
"priority": 5,
"source": "ADMIN_GRANTED",
"referenceCode": "PROMO_2024",
"notes": "Promotional credits for Q1 2024"
}
Request Fields
Field | Type | Required | Description |
---|---|---|---|
customerId | string | ✓ | Customer ID to grant credits to |
creditDefinitionId | string | ✓ | Credit definition ID to base grant on |
initialAmount | number | Override default amount from definition | |
startDate | string | When credits become available (ISO 8601) | |
expiryDate | string | When credits expire (ISO 8601) | |
priority | number | Override priority from definition | |
source | string | ✓ | Grant source: ADMIN_GRANTED , PLAN_BENEFIT , RENEWAL , PROMO |
referenceCode | string | Reference code for tracking | |
notes | string | Optional notes about the grant |
Response
{
"grant": {
"id": "cg_789",
"merchantId": "merchant_123",
"customerId": "cust_456",
"creditDefinitionId": "cd_123",
"initialAmount": 2000,
"remainingAmount": 2000,
"status": "ACTIVE",
"applicationType": "usage",
"scope": "plan",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"priceId": null,
"priceComponentId": null,
"currency": null,
"priority": 5,
"startDate": "2024-01-01T00:00:00Z",
"expiryDate": "2024-12-31T23:59:59Z",
"source": "ADMIN_GRANTED",
"referenceCode": "PROMO_2024",
"notes": "Promotional credits for Q1 2024",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"deletedAt": null
}
}
Revoke Credit Grant
Revoke an active credit grant.
POST /api/credits/grants/{id}/revoke
Parameters
Parameter | Type | Description |
---|---|---|
id | string | Credit Grant ID |
Request Body
{
"notes": "Revoked due to policy violation"
}
Request Fields
Field | Type | Description |
---|---|---|
notes | string | Optional reason for revocation |
Response
{
"grant": {
"id": "cg_789",
"merchantId": "merchant_123",
"customerId": "cust_456",
"creditDefinitionId": "cd_123",
"initialAmount": 2000,
"remainingAmount": 0,
"status": "REVOKED",
"applicationType": "usage",
"scope": "plan",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"priceId": null,
"priceComponentId": null,
"currency": null,
"priority": 5,
"startDate": "2024-01-01T00:00:00Z",
"expiryDate": "2024-12-31T23:59:59Z",
"source": "ADMIN_GRANTED",
"referenceCode": "PROMO_2024",
"notes": "Promotional credits for Q1 2024 | Revoked: Revoked due to policy violation",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-15T14:30:00Z",
"deletedAt": null
}
}
Get Grants for Definition
Get all grants for a specific credit definition.
GET /api/credits/definitions/{definitionId}/grants
Parameters
Parameter | Type | Description |
---|---|---|
definitionId | string | Credit Definition ID |
Response
{
"grants": [
{
"id": "cg_123",
"merchantId": "merchant_123",
"customerId": "cust_456",
"creditDefinitionId": "cd_123",
"initialAmount": 1000,
"remainingAmount": 750,
"status": "ACTIVE",
"applicationType": "usage",
"scope": "plan",
"planId": "plan_123",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"priceId": null,
"priceComponentId": null,
"currency": null,
"priority": 1,
"startDate": "2024-01-01T00:00:00Z",
"expiryDate": "2024-01-31T23:59:59Z",
"source": "ADMIN_GRANTED",
"referenceCode": "WELCOME_BONUS",
"notes": "Welcome bonus for new customer",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"deletedAt": null
}
]
}
Background Processing (Trigger Endpoints)
These endpoints are used for automated background processing and are typically called by scheduled jobs.
Process Grant Renewals
Process automatic renewals for credit grants based on RRULE configurations.
POST /trigger/credits/process-grant-renewals
Request Body
{
"timestamp": "2024-01-01T00:00:00Z"
}
Request Fields
Field | Type | Required | Description |
---|---|---|---|
timestamp | string | ✓ | Timestamp to process renewals for (ISO 8601) |
Response
{
"success": true,
"renewalCount": 25,
"errorCount": 2,
"timestamp": "2024-01-01T00:00:00Z"
}
Process Credit Expirations
Process credit expirations and mark expired grants as expired.
POST /trigger/credits/process-expirations
Request Body
{
"timestamp": "2024-01-01T00:00:00Z"
}
Request Fields
Field | Type | Required | Description |
---|---|---|---|
timestamp | string | ✓ | Timestamp to process expirations for (ISO 8601) |
Response
{
"success": true,
"expiredCount": 15,
"timestamp": "2024-01-01T00:00:00Z"
}
Process Renewal After Invoice
Process credit renewals that are triggered after invoice generation.
POST /trigger/credits/renewal-after-invoice
Request Body
{
"merchantId": "merchant_123",
"customerId": "cust_456"
}
Request Fields
Field | Type | Required | Description |
---|---|---|---|
merchantId | string | ✓ | Merchant ID |
customerId | string | ✓ | Customer ID |
Response
{
"success": true,
"renewalCount": 3,
"merchantId": "merchant_123",
"customerId": "cust_456",
"timestamp": "2024-01-01T00:00:00Z"
}
Examples
Creating Usage Credits for a Feature
curl -X POST /api/credits/definitions \
-H "Content-Type: application/json" \
-d '{
"name": "API Call Credits",
"description": "Credits for API usage",
"scope": "plan",
"applicationType": "usage",
"planId": "plan_basic",
"featureId": "feature_api_calls",
"featureSlug": "api-calls",
"defaultAmount": 1000,
"refillAmount": 1000,
"expiryDays": 30,
"refillRrule": "FREQ=MONTHLY;INTERVAL=1",
"renewOnBilling": true,
"priority": 1
}'
Creating Monetary Credits
curl -X POST /api/credits/definitions \
-H "Content-Type: application/json" \
-d '{
"name": "Account Credit",
"description": "Monetary credit for customer account",
"scope": "merchant",
"applicationType": "monetary",
"defaultAmount": 5000,
"currency": "USD",
"billingVisible": true,
"billingDescription": "Account Credit",
"priority": 10
}'
Granting Credits to a Customer
curl -X POST /api/credits/grants \
-H "Content-Type: application/json" \
-d '{
"customerId": "cust_customer123",
"creditDefinitionId": "cd_api_credits",
"initialAmount": 2000,
"source": "ADMIN_GRANTED",
"referenceCode": "WELCOME_BONUS",
"notes": "Welcome bonus for new customer"
}'
Getting Customer Credit Usage
curl -X GET "/api/credits/customers/cust_customer123/transactions?limit=50&fromDate=2024-01-01T00:00:00Z&toDate=2024-01-31T23:59:59Z" \
-H "Content-Type: application/json"
Revoking a Credit Grant
curl -X POST /api/credits/grants/cg_grant123/revoke \
-H "Content-Type: application/json" \
-d '{
"notes": "Credits revoked due to account suspension"
}'
Data Models
Credit Definition
Field | Type | Description |
---|---|---|
id | string | Unique credit definition identifier |
merchantId | string | Merchant that owns this definition |
name | string | Display name |
description | string | Optional description |
scope | string | merchant or plan |
applicationType | string | usage or monetary |
planId | string | Plan ID (required for plan-scoped credits) |
featureId | string | Feature ID for usage credits |
featureSlug | string | Feature slug for usage credits |
priceId | string | Price ID for usage credits |
priceComponentId | string | Price component ID for usage credits |
defaultAmount | number | Default amount when granting |
refillAmount | number | Amount to refill during renewals |
expiryDays | number | Days until credits expire |
refillRrule | string | RRULE string for automatic renewals |
renewOnBilling | boolean | Whether to renew on billing cycle |
priority | number | Priority for credit application |
billingVisible | boolean | Whether to show on invoices |
billingDescription | string | Description for billing |
currency | string | Currency code (required for monetary credits) |
isActive | boolean | Whether definition is active |
createdAt | string | Creation timestamp |
updatedAt | string | Last update timestamp |
deletedAt | string | Soft deletion timestamp |
Credit Grant
Field | Type | Description |
---|---|---|
id | string | Unique credit grant identifier |
merchantId | string | Merchant ID |
customerId | string | Customer ID |
creditDefinitionId | string | Credit definition ID |
initialAmount | number | Initial credit amount |
remainingAmount | number | Remaining credit amount |
status | string | ACTIVE , EXPIRED , REVOKED |
applicationType | string | usage or monetary |
scope | string | merchant or plan |
planId | string | Plan ID (for plan-scoped credits) |
featureId | string | Feature ID |
featureSlug | string | Feature slug |
priceId | string | Price ID |
priceComponentId | string | Price component ID |
currency | string | Currency code |
priority | number | Priority for application |
startDate | string | When credits become available |
expiryDate | string | When credits expire |
source | string | Grant source |
referenceCode | string | Reference code |
notes | string | Notes about the grant |
createdAt | string | Creation timestamp |
updatedAt | string | Last update timestamp |
deletedAt | string | Soft deletion timestamp |
Credit Transaction
Field | Type | Description |
---|---|---|
id | string | Unique transaction identifier |
merchantId | string | Merchant ID |
customerId | string | Customer ID |
creditGrantId | string | Credit grant ID |
amount | number | Credit amount used |
applicationType | string | usage or monetary |
eventName | string | Event that triggered usage |
eventTimestamp | string | When event occurred |
featureId | string | Feature ID |
featureSlug | string | Feature slug |
priceId | string | Price ID |
priceComponentId | string | Price component ID |
currency | string | Currency code |
metadata | object | Additional event metadata |
createdAt | string | Transaction creation timestamp |
Error Responses
400 Bad Request
{
"error": "Name is required"
}
{
"error": "Plan-scoped credits require planId"
}
{
"error": "Usage credits require either price linkage (priceId/priceComponentId) or feature linkage (featureId/featureSlug)"
}
{
"error": "Monetary credits require currency"
}
{
"error": "Limit must be less than 1000"
}
{
"error": "From date must be before to date"
}
404 Not Found
{
"error": "Credit definition not found"
}
{
"error": "Credit grant not found"
}
500 Internal Server Error
{
"error": "Failed to create credit definition"
}
{
"error": "Failed to create credit grant"
}
{
"error": "Failed to process grant renewals"
}
Important Notes
Credit Application Priority
Credits are applied in priority order (higher priority first). This allows you to control which credits are used first:
- Promotional credits (high priority) used before regular credits
- Expiring credits used before non-expiring credits
- Feature-specific credits used before general credits
Renewal Mechanisms
Credits support multiple renewal mechanisms:
- RRULE-based: Automatic renewals based on recurrence rules
- Billing-triggered: Renewals triggered by billing cycle events
- Manual: Admin-initiated renewals through API
Monetary vs Usage Credits
Monetary Credits:
- Applied as discounts on invoices
- Must be billing-visible
- Require currency specification
- Cannot be linked to features
Usage Credits:
- Applied against feature usage
- Can be linked to specific features or prices
- Not necessarily billing-visible
- Tracked through credit transactions
Soft Deletion
All credit entities use soft deletion to preserve historical data:
- Credit definitions can be deactivated instead of deleted
- Credit grants maintain transaction history even when revoked
- Deleted entities are excluded from active queries
Merchant Isolation
All credit operations are automatically scoped to the authenticated merchant:
- Credit definitions are merchant-specific
- Credit grants only apply to customers within the merchant
- Complete data isolation between merchants
Date Handling
All dates are in ISO 8601 format with UTC timezone:
startDate
: When credits become available for useexpiryDate
: When credits expire and become unusableeventTimestamp
: When credit usage events occurred
Validation Rules
Credit definitions have complex validation rules based on scope and type:
- Plan-scoped credits require
planId
- Usage credits require feature or price linkage
- Monetary credits require currency and must be billing-visible
- All amounts must be positive integers