Subscription management
Read, change the plan of, cancel and resume a customer's subscription from your server, with Stripe, and cancel at period end with Razorpay.
These v2 routes let your server manage an existing subscription without sending the customer to the provider's portal: read it, preview and apply a switch to another price variant, cancel it, and undo a scheduled cancellation. Every route is project-scoped:
P = /api/billstack/v2/teams/{teamId}/projects/{projectId}
GET P/subscriptions/{subscriptionId}?customerExternalId=...&mode=...
POST P/subscriptions/{subscriptionId}/preview-change
POST P/subscriptions/{subscriptionId}/change
POST P/subscriptions/{subscriptionId}/cancel
POST P/subscriptions/{subscriptionId}/resumeResponses use the usual { data, meta } envelope. Calling them from a browser is not supported: use a server-side private API key and confirm the customer belongs to your signed-in user before you pass customerExternalId.
Name the subscription and the customer
{subscriptionId} is the provider subscription id (sub_123) or the sourceExternalId of the subscription's grant in the entitlement read, URL-encoded. Both name the same subscription:
sub_123
stripe:binc_9f2:live:stripe_subscription:sub_123:user_123customerExternalId is required on every call (a query parameter on GET, a body field on POST). It is an ownership check: a subscription that exists but belongs to another customer, another project or the other environment answers 404 SUBSCRIPTION_NOT_FOUND, the same as an unknown one.
The subscription is served by the provider account it was created in, whichever region the customer is pinned to now, including an account that has since been replaced. mode is optional (test or live) and defaults to the environment of the API key. The four POST routes act only in the key's own environment; a different mode is 403 ENVIRONMENT_MISMATCH. GET follows the same rule for an API key.
Read a subscription
curl "https://billing.w3dev.app/api/billstack/v2/teams/{teamId}/projects/{projectId}/subscriptions/sub_123?customerExternalId=user_123" \
-H "Authorization: Bearer <private-api-key>"{
"data": {
"id": "sub_123",
"grantSourceExternalId": "stripe:binc_9f2:live:stripe_subscription:sub_123:user_123",
"customerExternalId": "user_123",
"provider": "stripe",
"accountIncarnationId": "binc_9f2",
"regionId": "reg_1",
"mode": "live",
"status": "active",
"cancelAtPeriodEnd": false,
"currentPeriodEnd": "2026-11-23T00:00:00.000Z",
"canceledAt": null,
"cancelAt": null,
"billingMode": "classic",
"priceVariantId": "bpv_basic",
"quantity": 1,
"currency": "USD",
"amountMinor": 1000,
"interval": "month",
"actions": [
{ "key": "preview_change", "enabled": true, "reason": null },
{ "key": "change_price", "enabled": true, "reason": null },
{ "key": "cancel_at_period_end", "enabled": true, "reason": null },
{ "key": "cancel_immediately", "enabled": true, "reason": null },
{ "key": "resume", "enabled": false, "reason": "not_canceling" }
]
}
}The state is read from the provider at request time, so it can be newer than the entitlement grants. status is the provider's own word (active, trialing, past_due, canceled, ...). actions says what can be done now and why not (provider_unsupported, subscription_not_active, multiple_items, metered_price, already_canceling, subscription_ended, not_canceling).
Preview a plan change
curl -X POST ".../subscriptions/sub_123/preview-change" \
-H "Authorization: Bearer <private-api-key>" \
-H "Content-Type: application/json" \
-d '{
"customerExternalId": "user_123",
"priceVariantId": "bpv_pro",
"prorationBehavior": "always_invoice"
}'| Field | Required | Notes |
|---|---|---|
customerExternalId | Yes | The owner. |
priceVariantId | Yes | A recurring price variant of this project, published to the subscription's provider account in the same currency. |
prorationBehavior | No | create_prorations (default; the difference lands on the next invoice), always_invoice (settle it now) or none. |
quantity | No | A positive integer. Default: keep the current quantity. |
{
"data": {
"quote": {
"quoteToken": "eyJ2IjoxLC4uLn0.9d3c...",
"expiresAt": "2026-10-23T10:10:00.000Z",
"from": { "priceVariantId": "bpv_basic", "amountMinor": 1000, "currency": "USD", "interval": "month" },
"to": { "priceVariantId": "bpv_pro", "amountMinor": 2000, "currency": "USD", "interval": "month", "plan": "pro" },
"effectiveAt": "now",
"proration": {
"behavior": "always_invoice",
"prorationDate": "2026-10-23T10:00:00.000Z",
"lines": [
{ "description": "Unused time on Basic", "amountMinor": -400 },
{ "description": "Remaining time on Pro", "amountMinor": 1400 }
]
},
"immediateAmountMinor": 1000,
"nextInvoiceAmountMinor": 2000,
"nextInvoiceDate": "2026-11-23T00:00:00.000Z",
"requiresPayment": true,
"currency": "USD",
"billingAnchor": "unchanged"
}
}
}A preview changes nothing at the provider. immediateAmountMinor is what is charged when the change is applied (with always_invoice, or when the billing interval changes); amounts are before any tax the provider adds. When the interval changes (for example monthly to yearly) billingAnchor is reset: the new period starts now and is invoiced now, also with none. A trialing subscription, and a Stripe subscription in flexible billing mode, keeps its billing anchor (unchanged). The quote token is valid for ten minutes.
Apply a plan change
curl -X POST ".../subscriptions/sub_123/change" \
-H "Authorization: Bearer <private-api-key>" \
-H "Content-Type: application/json" \
-d '{
"customerExternalId": "user_123",
"priceVariantId": "bpv_pro",
"idempotencyKey": "upgrade-user_123-2026-10-23",
"quoteToken": "eyJ2IjoxLC4uLn0.9d3c..."
}'idempotencyKey is required. quoteToken is optional but recommended: the change then uses the proration moment of the quote, so the customer is charged what they were shown, and a quote whose subscription, seats or target price has changed since is refused (409 QUOTE_STALE). prorationBehavior and quantity default to the quote's values; if you send them they must match the quote. Your own metadata keys are added to the provider subscription; keys beginning with billstack_, and plan, tier and tier_alias, are ignored.
{
"data": {
"subscription": { "id": "sub_123", "priceVariantId": "bpv_pro", "amountMinor": 2000, "...": "..." },
"change": {
"fromPriceVariantId": "bpv_basic",
"toPriceVariantId": "bpv_pro",
"prorationBehavior": "always_invoice",
"invoiceId": "in_456",
"amountChargedMinor": 1000,
"unchanged": false
},
"entitlement": {
"projection": "pending",
"note": "Entitlement grants update when the provider's subscription event is processed"
}
},
"meta": {
"apiVersion": "2",
"requestId": "req_...",
"warnings": [{ "code": "ENTITLEMENT_PROJECTION_PENDING", "message": "..." }]
}
}The change takes effect at the provider in this call, and BillStack records the new plan on the subscription in the same provider request. The entitlement grants, and so effective.plan, follow when the provider's customer.subscription.updated event is processed: read the entitlement again afterwards rather than assuming it already changed. If the amount charged differs from the quote, the change stays applied and meta.warnings carries QUOTE_AMOUNT_DIFFERS.
What a switch needs:
- The subscription is
activeortrialing, has exactly one item, and is not billed by usage. - The target price is recurring, in the subscription's currency, and published to the subscription's own provider account (publish it with the catalog price variant links first). Scheduled changes at the end of the period are not supported.
- A payment that cannot be collected fails the whole change (
402 PAYMENT_FAILED); the subscription is left as it was. - Repeating a request that already succeeded, or asking for the plan and quantity the subscription already has, answers
200withchange.unchanged: trueand charges nothing. The sameidempotencyKeywith different parameters is409 IDEMPOTENCY_CONFLICT. - If the provider does not confirm the outcome you get
504 PROVIDER_OUTCOME_UNKNOWN; retry with the sameidempotencyKey. After a refusal such as402 PAYMENT_FAILED, fix the cause and use a newidempotencyKey: the provider remembers the outcome of a key for 24 hours.
Cancel
curl -X POST ".../subscriptions/sub_123/cancel" \
-H "Authorization: Bearer <private-api-key>" \
-H "Content-Type: application/json" \
-d '{ "customerExternalId": "user_123", "when": "period_end" }'| Field | Required | Notes |
|---|---|---|
customerExternalId | Yes | The owner. |
when | Yes | period_end keeps access until the paid period ends; immediately ends the subscription now. |
prorate | No | immediately only: credit the unused time. Default false. No refund is issued either way. |
idempotencyKey | For immediately | Makes a retry safe. |
The response is { "subscription": {...}, "unchanged": false }. A subscription already set to cancel at the period end, or already ended, answers 200 with unchanged: true. A scheduled cancel makes the entitlement grant canceling; it stays entitled until its end date. An immediate cancel ends access once the provider event is processed. Cancel at the period end needs an active or trialing subscription; an immediate cancel works on any subscription that has not ended.
Resume
curl -X POST ".../subscriptions/sub_123/resume" \
-H "Authorization: Bearer <private-api-key>" \
-H "Content-Type: application/json" \
-d '{ "customerExternalId": "user_123" }'Undoes a scheduled cancellation: one set for the end of the period (cancelAtPeriodEnd), or a date alone (cancelAt, for example a custom cancel date or a cancellation made in the Stripe portal of a flexible subscription). If the provider still reports a scheduled cancellation afterwards, the answer carries the warning CANCELLATION_STILL_SCHEDULED. A subscription that is not set to cancel answers 200 with unchanged: true. A subscription that has ended cannot be resumed (409 SUBSCRIPTION_NOT_RESUMABLE); start a new checkout instead.
Provider support
| Operation | Stripe | Razorpay | RevenueCat |
|---|---|---|---|
| Read | Yes | Yes | No |
| Preview and apply a plan change | Yes | No | No |
| Cancel at the period end | Yes | Yes | No |
| Cancel immediately | Yes | No | No |
| Resume | Yes | No | No |
An operation the subscription's provider does not support answers 409 PROVIDER_ACTION_UNSUPPORTED, without calling the provider.
Permissions
| Route | IAM action |
|---|---|
GET | subscription:GetSubscription |
preview-change, change, resume | subscription:UpdateSubscription |
cancel | subscription:CancelSubscription |
The built-in Owner, Admin and Billing policies allow all three. A project API key created since 2026-12-01 (app runtime) allows all three too, and an older full-access key with no IAM attachments keeps its admin-equivalent access. The Developer and ReadOnly policies can read subscriptions but not change or cancel them. These routes are not available through the MCP server.
Errors
| Code | Status | Meaning |
|---|---|---|
INVALID_REQUEST | 400 | Missing customerExternalId, priceVariantId, idempotencyKey or when, a bad mode, or malformed metadata. |
INVALID_PRORATION_BEHAVIOR | 400 | Not create_prorations, always_invoice or none. |
INVALID_QUANTITY | 400 | Not a positive integer. |
INVALID_QUOTE_TOKEN | 400 | The token is malformed, forged, or was issued for another change. |
PAYMENT_FAILED | 402 | The payment for the change failed; nothing changed. |
UNAUTHORIZED | 403 | The key may not perform the action. |
ENVIRONMENT_MISMATCH | 403 | mode names the other environment. |
SUBSCRIPTION_NOT_FOUND | 404 | No such subscription for this customer, project and environment. |
PRICE_VARIANT_NOT_FOUND | 404 | The price variant is not in this project. |
PROVIDER_ACTION_UNSUPPORTED | 409 | The provider cannot do this. |
PROVIDER_UNAVAILABLE | 409 | The provider account of the subscription cannot be used, or could not be reached. |
SUBSCRIPTION_NOT_ACTIVE | 409 | The subscription is not active or trialing. |
SUBSCRIPTION_NOT_SWITCHABLE | 409 | More than one item, or a usage-based price. |
SUBSCRIPTION_NOT_RESUMABLE | 409 | The subscription has ended or is not in a state that can be resumed. |
QUOTE_STALE | 409 | The subscription or the target price changed since the quote. |
IDEMPOTENCY_CONFLICT | 409 | The key was used with different parameters. |
QUOTE_EXPIRED | 410 | The quote is older than ten minutes. |
PROVIDER_PRICE_LINK_UNAVAILABLE | 422 | The price variant is not published to the subscription's provider account. |
PRICE_VARIANT_INACTIVE | 422 | The price variant is archived. |
CURRENCY_MISMATCH | 422 | The target price is in another currency. |
PRICE_NOT_RECURRING | 422 | The target price is not recurring. |
PROVIDER_REJECTED | 422 | The provider refused the request. |
PROVIDER_OUTCOME_UNKNOWN | 504 | The provider did not confirm; retry with the same idempotencyKey. |
See also Customer portal for the hosted page your customers can use themselves, and TypeScript SDK for client.subscriptions.