Subscription Items
Subscription items represent a specific product or service that a customer is subscribed to within a larger subscription. They link a price to a subscription and track details like quantity. This API allows you to manage these items individually, including reporting usage for metered billing.
For managing the overall subscription, please see the Subscriptions API documentation.
The Subscription Item Object#
A subscription item object contains the details of a product within a subscription.
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the subscription item. |
|
|
|
|
| The ID of the subscription this item belongs to. |
|
| The ID of the price associated with this item. |
|
| The quantity of the price subscribed to. |
|
| A set of key-value pairs that you can attach to an object. Useful for storing additional information. |
|
| Usage-based billing thresholds. Contains |
|
| Timestamp of when the object was created. |
|
| Timestamp of the last update. |
When retrieved, the price object will be expanded.
Create a Subscription Item#
Adds a new item to an existing subscription.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the subscription to add this item to. |
|
| Required. The ID of the price to subscribe to. |
|
| The quantity of the price. Defaults to |
|
| Key-value data to associate with the item. |
|
| Usage-based billing thresholds for this item. |
Example
import payment from '@blocklet/payment-js';
async function addSubscriptionItem() {
try {
const subscriptionItem = await payment.subscriptionItems.create({
subscription_id: 'sub_xxxxxxxxxxxxxx',
price_id: 'price_xxxxxxxxxxxxxx',
quantity: 2
});
console.log('Subscription Item created:', subscriptionItem);
} catch (error) {
console.error('Error creating subscription item:', error.message);
}
}
addSubscriptionItem();Example Response
{
"id": "si_xxxxxxxxxxxxxx",
"livemode": false,
"subscription_id": "sub_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"quantity": 2,
"metadata": {},
"billing_thresholds": null,
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z"
}Retrieve a Subscription Item#
Retrieves the details of an existing subscription item.
Parameters
Name | Type | Description |
|---|---|---|
|
| The unique identifier of the subscription item to retrieve. |
Example
import payment from '@blocklet/payment-js';
async function getSubscriptionItem(itemId) {
try {
const subscriptionItem = await payment.subscriptionItems.retrieve(itemId);
console.log('Retrieved Subscription Item:', subscriptionItem);
} catch (error) {
console.error('Error retrieving subscription item:', error.message);
}
}
getSubscriptionItem('si_xxxxxxxxxxxxxx');Example Response (Expanded)
{
"id": "si_xxxxxxxxxxxxxx",
"livemode": false,
"subscription_id": "sub_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"price": {
"id": "price_xxxxxxxxxxxxxx",
"product_id": "prod_xxxxxxxxxxxxxx",
"active": true,
"type": "recurring",
"unit_amount": "1000",
"currency_id": "usd",
"recurring": {
"interval": "month",
"interval_count": 1
}
},
"quantity": 2,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z"
}Update a Subscription Item#
Updates the parameters of a subscription item.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The identifier of the subscription item to update. |
|
| The new quantity for the item. |
|
| The new price ID for the item. |
|
| New set of key-value data. |
Example
import payment from '@blocklet/payment-js';
async function updateSubscriptionItem(itemId) {
try {
const updatedItem = await payment.subscriptionItems.update(itemId, {
quantity: 3
});
console.log('Subscription Item updated:', updatedItem);
} catch (error) {
console.error('Error updating subscription item:', error.message);
}
}
updateSubscriptionItem('si_xxxxxxxxxxxxxx');Example Response
{
"id": "si_xxxxxxxxxxxxxx",
"livemode": false,
"subscription_id": "sub_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"quantity": 3,
"metadata": {},
"billing_thresholds": null,
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:05:00.000Z"
}List Subscription Items#
Returns a list of subscription items for a given subscription.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the subscription whose items to retrieve. |
|
| The page number for pagination. Defaults to |
|
| The number of items per page. Defaults to |
|
| A string representing the order of the results (e.g., |
Example
import payment from '@blocklet/payment-js';
async function listSubscriptionItems(subscriptionId) {
try {
const items = await payment.subscriptionItems.list({
subscription_id: subscriptionId,
pageSize: 5
});
console.log(`Found ${items.count} items:`, items.list);
} catch (error) {
console.error('Error listing subscription items:', error.message);
}
}
listSubscriptionItems('sub_xxxxxxxxxxxxxx');Example Response
{
"count": 1,
"list": [
{
"id": "si_xxxxxxxxxxxxxx",
"livemode": false,
"subscription_id": "sub_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"quantity": 3,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:05:00.000Z"
}
]
}Delete a Subscription Item#
Deletes an item from a subscription. This action is often irreversible.
Parameters
Name | Type | Description |
|---|---|---|
|
| The identifier of the subscription item to delete. |
Example
import payment from '@blocklet/payment-js';
async function deleteSubscriptionItem(itemId) {
try {
const deletedItem = await payment.subscriptionItems.del(itemId);
console.log('Subscription Item deleted:', deletedItem);
} catch (error) {
console.error('Error deleting subscription item:', error.message);
}
}
deleteSubscriptionItem('si_xxxxxxxxxxxxxx');Example Response
{
"id": "si_xxxxxxxxxxxxxx",
"livemode": false,
"subscription_id": "sub_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"quantity": 3,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:05:00.000Z"
}Create a Usage Record#
Creates a usage record for a metered billing subscription item. This is used to report usage that will be billed at the end of a period.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the subscription item to report usage for. |
|
| Required. The quantity of usage to report. |
|
| Can be |
|
| The timestamp for the usage event. Defaults to the current time. |
Example
import payment from '@blocklet/payment-js';
async function reportUsage(itemId) {
try {
const usageRecord = await payment.subscriptionItems.createUsageRecord({
subscription_item_id: itemId,
quantity: 100,
action: 'increment'
});
console.log('Usage Record created:', usageRecord);
} catch (error) {
console.error('Error creating usage record:', error.message);
}
}
reportUsage('si_metered_xxxxxxxx');Example Response
{
"id": "ur_xxxxxxxxxxxxxx",
"livemode": false,
"quantity": 100,
"subscription_item_id": "si_metered_xxxxxxxx",
"timestamp": 1672531200
}List Usage Record Summaries#
For a given subscription item, returns a list of summary objects. Each object in the list contains a total quantity of usage reported for a specific period.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the subscription item. |
|
| The page number for pagination. Defaults to |
|
| The number of items per page. Defaults to |
|
| A string representing the order of the results. |
Example
import payment from '@blocklet/payment-js';
async function getUsageSummaries(itemId) {
try {
const summaries = await payment.subscriptionItems.listUsageRecordSummaries({
subscription_item_id: itemId
});
console.log('Usage Summaries:', summaries);
} catch (error) {
console.error('Error listing usage summaries:', error.message);
}
}
getUsageSummaries('si_metered_xxxxxxxx');Example Response
{
"count": 1,
"list": [
{
"id": "si_metered_xxxxxxxx",
"total_usage": 5000
}
]
}You now have the tools to manage individual items within a subscription. For handling payment-related events, proceed to the Webhooks documentation.