Subscriptions
Subscriptions are used to manage recurring payment plans for customers. The Subscriptions API allows you to create, retrieve, update, cancel, and manage the lifecycle of these recurring billing arrangements.
For managing the individual items within a subscription, see the Subscription Items API.
Retrieve a Subscription#
Fetches the details of an existing subscription by its unique identifier.
Parameters
Name | Type | Description |
|---|---|---|
| string | The ID of the subscription to retrieve. |
Returns
Returns a subscription object if a valid ID was provided. The TSubscriptionExpanded object includes the following properties:
Name | Type | Description |
|---|---|---|
|
| Unique identifier for the subscription. |
|
| The status of the subscription (e.g., |
|
| The ID of the customer associated with this subscription. |
|
| The ID of the currency used for this subscription. |
|
| The start of the current billing period as a Unix timestamp. |
|
| The end of the current billing period as a Unix timestamp. |
|
| An array of subscription items associated with this subscription. |
|
| The expanded customer object. |
|
| The expanded payment currency object. |
|
| The expanded payment method object. |
|
| A set of key-value pairs that you can attach to an object. |
Example
Retrieving a subscription
async function getSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.retrieve(subscriptionId);
console.log('Subscription retrieved:', subscription);
return subscription;
} catch (error) {
console.error('Error retrieving subscription:', error.message);
}
}
// Example usage:
getProductDetails('sub_xxxxxxxxxxxxxx');Example Response
{
"id": "sub_xxxxxxxxxxxxxx",
"status": "active",
"customer_id": "cus_xxxxxxxxxxxxxx",
"currency_id": "curr_xxxxxxxxxxxxxx",
"current_period_start": 1672531200,
"current_period_end": 1675209600,
"items": [
{
"id": "si_xxxxxxxxxxxxxx",
"price_id": "price_xxxxxxxxxxxxxx",
"quantity": 1
}
],
"customer": { /* ... customer details ... */ },
"paymentCurrency": { /* ... currency details ... */ },
"paymentMethod": { /* ... method details ... */ },
"metadata": {}
}Update a Subscription#
Updates an existing subscription by setting the values of the parameters passed.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to update. |
|
| An object containing the fields to update. See details below. |
Data Object Properties
Name | Type | Description |
|---|---|---|
|
| An optional description for the subscription. |
|
| A set of key-value pairs to store with the subscription. |
|
| Determines how to handle payments. Can be |
|
| Determines how to handle prorations. Can be |
|
| Unix timestamp indicating the end of the trial period. Can also be |
|
| An array of items to update on the subscription. See details below. |
SubscriptionUpdateItem Object Properties
Name | Type | Description |
|---|---|---|
|
| The ID of an existing subscription item to update or delete. |
|
| The ID of the price for a new subscription item. |
|
| The quantity for the subscription item. |
|
| Set to |
|
| For metered billing, indicates whether to clear usage on deletion. |
Returns
Returns the updated subscription object.
Example
Updating a subscription's metadata
async function updateSubscriptionMetadata(subscriptionId) {
try {
const subscription = await payment.subscriptions.update(subscriptionId, {
metadata: {
order_id: 'order_12345'
}
});
console.log('Subscription updated:', subscription);
return subscription;
} catch (error) {
console.error('Error updating subscription:', error.message);
}
}
// Example usage:
updateSubscriptionMetadata('sub_xxxxxxxxxxxxxx');Example Response
{
"id": "sub_xxxxxxxxxxxxxx",
"status": "active",
"metadata": {
"order_id": "order_12345"
}
// ... other subscription properties
}List Subscriptions#
Returns a paginated list of subscriptions. You can filter the list based on various criteria.
Parameters
Name | Type | Description |
|---|---|---|
|
| Filter subscriptions by status (e.g., |
|
| Filter subscriptions by a specific customer ID. |
|
| Filter subscriptions by a specific customer DID. |
|
| Filter by custom metadata fields. |
|
| Sort order (e.g., |
|
| If |
|
| The page number for pagination (default: |
|
| The number of items per page (default: |
Returns
Returns a paginated list of subscription objects.
Name | Type | Description |
|---|---|---|
|
| An array of subscription objects for the current page. |
|
| The total number of subscriptions matching the query. |
|
| An object containing pagination details ( |
Example
Listing all active subscriptions
async function listActiveSubscriptions() {
try {
const response = await payment.subscriptions.list({
status: 'active',
pageSize: 10
});
console.log(`Found ${response.count} active subscriptions.`);
console.log('First page:', response.list);
return response;
} catch (error) {
console.error('Error listing subscriptions:', error.message);
}
}
// Example usage:
listActiveSubscriptions();Example Response
{
"count": 50,
"list": [
{
"id": "sub_xxxxxxxxxxxxxx",
"status": "active"
// ... other subscription properties
}
// ... more subscriptions
],
"paging": {
"page": 1,
"pageSize": 10
}
}Search Subscriptions#
Performs a search for subscriptions based on a query string.
Parameters
Name | Type | Description |
|---|---|---|
|
| The search string to match against subscription fields. |
|
| The page number for pagination (default: |
|
| The number of items per page (default: |
Returns
Returns a paginated list of subscription objects matching the search query.
Example
Searching for a subscription
async function searchForSubscription(query) {
try {
const response = await payment.subscriptions.search({ query });
console.log(`Search results for "${query}":`, response.list);
return response;
} catch (error) {
console.error('Error searching subscriptions:', error.message);
}
}
// Example usage:
searchForSubscription('some_customer_did');Cancel a Subscription#
Cancels a customer's subscription. The subscription can be canceled immediately or at the end of the current billing period.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to cancel. |
|
| An object containing cancellation details. |
Body Object Properties
Name | Type | Description |
|---|---|---|
|
| When to cancel. Can be |
|
| Required if |
|
| Specifies if a refund should be issued. Can be |
|
| Customer feedback for the cancellation. |
|
| Internal comment about the cancellation. |
|
| The reason for cancellation (e.g., |
Returns
Returns the updated subscription object with cancellation details.
Example
Canceling a subscription at period end
async function cancelSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.cancel(subscriptionId, {
at: 'current_period_end',
reason: 'cancellation_requested'
});
console.log('Subscription scheduled for cancellation:', subscription);
return subscription;
} catch (error) {
console.error('Error canceling subscription:', error.message);
}
}
// Example usage:
cancelSubscription('sub_xxxxxxxxxxxxxx');Recover a Subscription#
Reactivates a subscription that has been scheduled for cancellation but has not yet been canceled.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to recover. |
Returns
Returns the updated, active subscription object.
Example
Recovering a canceled subscription
async function recoverSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.recover(subscriptionId);
console.log('Subscription recovered:', subscription);
return subscription;
} catch (error) {
console.error('Error recovering subscription:', error.message);
}
}
// Example usage:
recoverSubscription('sub_xxxxxxxxxxxxxx');Pause a Subscription#
Pauses payment collection for a subscription.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to pause. |
|
| An object containing pause details. |
Body Object Properties
Name | Type | Description |
|---|---|---|
|
| The behavior of invoices created while paused. Can be |
|
| A Unix timestamp for when the subscription should automatically resume. |
Returns
Returns the updated subscription object with a paused status.
Example
Pausing a subscription
async function pauseSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.pause(subscriptionId, {
behavior: 'keep_as_draft'
});
console.log('Subscription paused:', subscription);
return subscription;
} catch (error) {
console.error('Error pausing subscription:', error.message);
}
}
// Example usage:
pauseSubscription('sub_xxxxxxxxxxxxxx');Resume a Subscription#
Resumes payment collection for a paused subscription.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to resume. |
Returns
Returns the updated, active subscription object.
Example
Resuming a subscription
async function resumeSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.resume(subscriptionId);
console.log('Subscription resumed:', subscription);
return subscription;
} catch (error) {
console.error('Error resuming subscription:', error.message);
}
}
// Example usage:
resumeSubscription('sub_xxxxxxxxxxxxxx');Delete a Subscription#
Permanently deletes a subscription and all its related data. This action is irreversible and should only be used in non-production environments.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription to delete. |
Returns
Returns the deleted subscription object.
Example
Deleting a subscription
async function deleteSubscription(subscriptionId) {
try {
const subscription = await payment.subscriptions.del(subscriptionId);
console.log('Subscription deleted:', subscription);
return subscription;
} catch (error) {
console.error('Error deleting subscription:', error.message);
}
}
// Example usage:
deleteSubscription('sub_xxxxxxxxxxxxxx');List Overdue Invoices#
Retrieves a list of overdue (uncollectible) invoices for a specific subscription, along with a summary of the amounts due.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the subscription. |
Returns
Returns an object containing the subscription, a list of overdue invoices, and a summary of the total amounts owed per currency.
Name | Type | Description |
|---|---|---|
|
| The subscription object. |
|
| An array of overdue invoice objects. |
|
| An object summarizing the total overdue amount for each currency. |
Example
Fetching overdue invoices
async function getOverdueInvoices(subscriptionId) {
try {
const result = await payment.subscriptions.overdueInvoices(subscriptionId);
console.log('Overdue Invoices Summary:', result.summary);
console.log('Invoices:', result.invoices);
return result;
} catch (error) {
console.error('Error fetching overdue invoices:', error.message);
}
}
// Example usage:
getOverdueInvoices('sub_xxxxxxxxxxxxxx');Return Stake#
Initiates the process of returning staked funds associated with a canceled subscription that used an ArcBlock payment method.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the canceled subscription. |
Returns
Returns an object indicating the success of scheduling the stake return job.
Name | Type | Description |
|---|---|---|
|
|
|
|
| The ID of the subscription for which the stake is being returned. |
Example
Returning a stake
async function returnSubscriptionStake(subscriptionId) {
try {
const result = await payment.subscriptions.returnStake(subscriptionId);
console.log('Stake return initiated:', result);
return result;
} catch (error) {
console.error('Error returning stake:', error.message);
}
}
// Example usage:
returnSubscriptionStake('sub_xxxxxxxxxxxxxx');