---
name: manage-billstack-customers
description: "v2 (provider-neutral). Inspect customer billing and entitlements, reconcile workspace access across subscription lifecycle changes, read canonical customers, invoices and subscriptions, open the provider's own customer portal (Stripe), change the plan of, cancel, and resume subscriptions through the v2 subscription routes, and register external-billing customers, while preserving team, project, and customer identity."
---

# Manage BillStack customers

**Generation: v2** (provider-neutral, `/api/billstack/v2/...`, `BillStackV2Client`). This skill covers v2 only. If the app calls `/api/billstack/teams/...` (legacy v1), load [legacy-billstack-integration](https://billing.w3dev.app/.well-known/agent-skills/legacy-billstack-integration/SKILL.md) instead.

Use this skill for customer billing support or an application billing page. For initial integration, load [integrate-billstack](https://billing.w3dev.app/.well-known/agent-skills/integrate-billstack/SKILL.md); for read-only financial investigation load [inspect-billstack-billing](https://billing.w3dev.app/.well-known/agent-skills/inspect-billstack-billing/SKILL.md).

Use `https://billing.w3dev.app` with a server-side private Bearer key and JSON bodies. Start with a `bs_test_` key, which can reach only test customers, subscriptions, and entitlements. A `bs_live_` key reaches only live data and works only once the team is promoted to live; before that it gets `403 LIVE_ENVIRONMENT_NOT_ENABLED`. A legacy `bs_<hex>` key has no environment in its prefix and runs in the environment stored for it (live for keys issued before environments, test for teams moved to test-only). Every route below runs in the key's environment, and a v2 `mode` that names the other environment returns `403 ENVIRONMENT_MISMATCH`. Keep credentials, customer data, and temporary portal URLs out of public logs. V2 uses IAM permissions and project restrictions; key scope strings are ignored. A key created since 2026-12-01 is an app-runtime key and covers customer, subscription, portal and entitlement operations; an older "Full access (legacy)" key without IAM attachments receives an admin-equivalent fallback. Legacy admin-or-key routes use broader access helpers, so treat legacy mutation credentials as privileged.

Identify the team, project, billing generation, key environment, and authenticated app customer before acting. `externalId` is your app's stable customer identifier, not a provider customer ID. URL-encode it in paths. Confirm the customer belongs to the app session/tenant; do not expose an unrestricted billing proxy to browser-supplied identifiers.

## V2: read access and grants

Prefix `P = /api/billstack/v2/teams/{teamId}/projects/{projectId}`.

- `GET P/entitlements/{externalId}` requires `entitlement:GetEntitlement` and returns `{ data: { effective, grants }, meta }`.
- Inspect `effective.isEntitled`, plan, tier, status, startsAt, endsAt, contributors, and unresolvedGrantCount. `mode` is optional and defaults to the key's environment, so a test key reads test entitlements. Check `effective.mode` before acting on a result.
- `GET P/entitlements?limit=50` requires `entitlement:ListEntitlements` and lists `data.items` and `data.nextCursor`. Pass nextCursor as the URL-encoded cursor until null; filters include externalId and status (applied per fetched page, so pages can be short). Maximum limit is 200.

A missing/pending entitlement is not proof a charge failed. Signed provider events and persistence can lag; inspect unresolved grants, provider/mode, and data freshness before changing access. Do not grant access solely because a customer has a checkout success URL, invoice, or subscription record.

## Workspace plan synchronization and lifecycle

Use [sync-billstack-entitlements](https://billing.w3dev.app/.well-known/agent-skills/sync-billstack-entitlements/SKILL.md) for application plan/cache synchronization, subject identity, failure handling, and periodic pagination. For workspace billing, the externalId must be the workspace's persisted billing subject, consistently used in checkout, confirmation, entitlement reads, and portal. Keep test/live access separate.

Outbound webhook endpoints on payload version `2026-10-26` or later send `entitlement.updated` (and `subscription.*`, `purchase.*`) events; see sync-billstack-entitlements for how to use them. Reconcile periodically and after checkout/portal return regardless; the provider ingress and Inngest worker do not forward provider notifications to the application. Map server-owned canonical tierAlias values to app features and limits. V2 effective reads do not expose legacy feature/limit, version, or updatedAt snapshot fields.

Read the combined effective result before changing access: canceling and billing_issue grants can remain entitled before endsAt; canceled/expired/revoked grants do not grant access, but another valid grant may. invoice.payment_failed alone does not revoke a subscription entitlement, and invoice.paid alone does not update its period. Verify corresponding subscription events and worker processing. A missing row, API failure, unknown alias, or unresolved grant needs reconciliation; it is not permission to invent a paid or free snapshot.

## V2: invoices, subscriptions, and customer records

Financial reads use `/api/billstack/v2/teams/{teamId}/financials/{resource}?projectId={projectId}`, where resource can be customers, invoices, or subscriptions (IAM `customer:ListCustomers`, `invoice:ListInvoices`, `subscription:ListSubscriptions`). `mode` defaults to the key's environment. Include projectId for project-restricted credentials; they are refused without it. A `404 CANONICAL_READ_NOT_READY` means canonical financial reads are not enabled for that scope yet. Read `data.items` and `data.nextCursor`; preserve provider and accountIncarnationId when disambiguating IDs. The detail route appends a URL-encoded externalId and can filter by provider/accountIncarnationId/mode.

These are canonical financial reads, not mutation endpoints. They do not imply that `DELETE /financials/subscriptions/...` or refund/usage-write routes exist; change, cancel, and resume use the subscription routes above. Follow the investigation skill for pagination and freshness. Financial external IDs and legacy project subscription IDs are not interchangeable.

## V2: void an invoice or mark it uncollectible

`POST /api/billstack/v2/teams/{teamId}/invoices/{invoiceId}/void` and `.../mark-uncollectible`, or the same under `/projects/{projectId}/invoices/{invoiceId}/`, change an `open` invoice at the provider (Stripe only; another provider answers `409 PROVIDER_ACTION_UNSUPPORTED`, the native sandbox `409 NATIVE_SANDBOX_UNSUPPORTED`). It cannot be undone from BillStack, so confirm the exact invoice, amount and customer with the operator before calling. Read it first with `GET` on the invoice route (it reads the provider live). The permissions are `invoice:VoidInvoice` and `invoice:MarkUncollectible` in the key's environment; app-runtime keys are always denied them, so use an operator-approved key. The body needs `idempotencyKey` (reuse it to retry; `unchanged: true` means nothing was written). A `409 INVOICE_STATUS_CONFLICT` carries the live status in `error.currentStatus`, and a `504 PROVIDER_OUTCOME_UNKNOWN` is retried with the same key. Team scope needs a team-level key; project scope acts only on an invoice that project's list shows and a project-bound key may be refused with `403 TEAM_ACCESS_REQUIRED` where invoices come from the team's Stripe copy.

## V2: change, cancel, or resume a subscription

A customer does not have to use a portal: a server can read and manage a subscription directly. Use only for a request the authenticated app customer made (or an operator explicitly authorized); these routes spend money. Full reference: https://docs.billing.w3dev.app/api-reference/subscription-management

- `{subscriptionId}` is the provider id (`sub_123`) or a grant's `sourceExternalId` from `GET P/entitlements/{externalId}` (shaped like `stripe:<incarnation>:live:stripe_subscription:sub_123:<customer>`), URL-encoded. Every call names `customerExternalId` (query on GET, body on POST); a subscription of another customer, project, or environment is `404 SUBSCRIPTION_NOT_FOUND`. `mode` defaults to the key's environment and the POST routes act only there.
- `GET P/subscriptions/{subscriptionId}?customerExternalId=` (`subscription:GetSubscription`) reads the provider's live state plus `actions` saying what is possible now and why not.
- `POST .../preview-change` with `{ customerExternalId, priceVariantId, prorationBehavior?, quantity? }` (`subscription:UpdateSubscription`) changes nothing and returns `quote` with proration lines, `immediateAmountMinor`, `nextInvoiceAmountMinor`, `billingAnchor`, and a ten-minute `quoteToken`. Show the amounts to the customer before applying.
- `POST .../change` with `{ customerExternalId, priceVariantId, idempotencyKey, quoteToken?, prorationBehavior?, quantity?, metadata? }` applies it now. Always send the `quoteToken` from the preview the customer saw: it fixes the proration moment, and `409 QUOTE_STALE` / `410 QUOTE_EXPIRED` mean preview again. Reuse the same `idempotencyKey` when retrying; `504 PROVIDER_OUTCOME_UNKNOWN` means retry with it. `402 PAYMENT_FAILED` changed nothing. `change.unchanged: true` means the subscription was already on that plan.
- `POST .../cancel` with `{ customerExternalId, when: "period_end" | "immediately", prorate?, idempotencyKey? }` (`subscription:CancelSubscription`); `idempotencyKey` is required for `immediately`. `immediately` is financially consequential: require explicit intent, and never infer a refund (none is issued).
- `POST .../resume` with `{ customerExternalId }` (`subscription:UpdateSubscription`) undoes a scheduled cancel (for the period end, or a `cancelAt` date); an ended subscription is `409 SUBSCRIPTION_NOT_RESUMABLE` (start a new checkout).
- A switch needs an `active` or `trialing` single-item subscription, a recurring target variant in the same currency that is published to the subscription's own provider account (`422 PROVIDER_PRICE_LINK_UNAVAILABLE` otherwise); scheduled changes at the period end are not supported.
- Stripe supports everything. Razorpay supports reading and cancel with `period_end`; everything else is `409 PROVIDER_ACTION_UNSUPPORTED`, so send those customers to the BillStack-hosted portal, which is a legacy route documented in legacy-billstack-integration. RevenueCat is unsupported.
- The response is `{ data: { subscription, ... }, meta }`. Entitlement grants update only when the provider event is processed (`meta.warnings` carries `ENTITLEMENT_PROJECTION_PENDING`): re-read entitlements rather than writing a plan yourself. The Developer and ReadOnly policies can read but not change or cancel subscriptions.

## V2: open a supported portal

POST to P/portal with:

```json
{
  "customerExternalId": "<authenticated-app-customer-id>",
  "provider": "stripe",
  "returnUrl": "https://your-app.example/account/billing"
}
```

Requires `portal:CreateSession`. Response is `{ data: { sessionId, portalUrl, expiresAt, provider, supportedActions }, meta }`; `expiresAt` can be null. Return portalUrl only to this customer. BillStack resolves the stored provider account, and only customers of the key's environment are visible, so a test customer has no portal under a live key (`404 CUSTOMER_PROVIDER_ACCOUNT_NOT_FOUND`). `providerCustomerId` is rejected, and the route takes no `mode`. Always send `returnUrl`; when omitted it defaults to the BillStack origin.

This route opens the provider's own portal. Stripe supports it; Razorpay and RevenueCat currently do not (for Razorpay see the BillStack-hosted portal in legacy-billstack-integration). CUSTOMER_PROVIDER_ACCOUNT_AMBIGUOUS needs operator resolution of stored account affinity, not guessing an ID. PROVIDER_ACTION_UNSUPPORTED (or PORTAL_UNSUPPORTED for a Stripe account without portal capability) requires a provider-supported workflow. Portal actions depend on provider configuration; do not promise every subscription change is available. To change a plan or cancel from your own server, use the v2 subscription routes above.

## V2: register an external-billing customer

`PUT P/customers/{externalId}` with `{ email, name?, stripeCustomerId, metadata? }` (IAM `customer:UpdateCustomer`) records a customer whose Stripe customer lives in your own Stripe account. It never calls Stripe, returns `{ data: { customer, created } }` with 201 or 200, and conflicts are `409 STRIPE_CUSTOMER_CONFLICT` or `409 EXTERNAL_ID_CONFLICT`. It belongs to the external-billing referral flow; see [integrate-billstack-referrals](https://billing.w3dev.app/.well-known/agent-skills/integrate-billstack-referrals/SKILL.md). Do not use it to provision customers for v2 checkout.

## Errors and verification

Private route authorization failures commonly return 403. V2 errors carry error.code/message and meta.requestId (including the environment codes `ENVIRONMENT_MISMATCH` and `LIVE_ENVIRONMENT_NOT_ENABLED`); legacy errors are described in legacy-billstack-integration. Preserve the actual response shape. Do not blindly retry non-idempotent operations after a network error.

Report the sanitized customer identifier, target project, mode, requested operation, and read-back result. A successful request is distinct from webhook convergence or a completed customer action in the provider portal. Verify with a `bs_test_` key and a test customer before making a live change.
