<UnitPayProvider> context — list them, resolve the live one, create a new subscription, change plans, attach addons, and cancel or resume.
Every hook returns loading/error state — reads via isLoading: boolean, mutations via isPending: boolean — plus error: Error | null. See Introduction. The sections below list each hook’s specific returns.
The four mutation hooks that move money (useCreateSubscription, useChangePlan, useAttachAddon) resolve to a SettleOutcome — the server decides whether to charge inline, mint an inline <PaymentForm>, send an invoice, or take no action — which you handle via the on* settlement callbacks in options. useCancelSubscription and useUncancelSubscription move no money, so they return a plain SubscriptionMutationResult instead.
useSubscriptions
Lists every subscription belonging to the customer — across every product, plan, and status — as one cached list. Takes no arguments; the customer comes from<UnitPayProvider> context.
Returns — subscriptions: Subscription[] — every subscription on the customer (up to 50), or [] while loading or when none exist.
useActiveSubscription
Answers “what is the customer billing on right now?” It filtersuseSubscriptions (no extra request) to live statuses — active, trialing, pending, past_due — returning both the first match for single-sub portals and the full filtered list for multi-sub customers.
Parameters
Restrict to subscriptions whose
planId is in this list. Use when the portal is product-scoped — pass the product’s plan ids (e.g. from usePricing(productId)). Omit to consider every plan the customer has.subscription—Subscription | null— the first live subscription matching the filter, ornullwhile loading or when none are live.subscriptions—Subscription[]— every live subscription matching the filter. Use this when a customer legitimately has more than one (different products, currencies, addons).
useSubscription
A read-only hook that fetches one subscription by id and exposespreviewChange, a parameterized read that previews what switching to another plan would cost. Mutations live in the dedicated hooks below.
Parameters
The subscription to fetch. The query stays disabled until an id is provided, so it is safe to pass
undefined while the id is still loading.subscription—Subscription | null— the subscription, ornullwhile loading or when no id is set.previewChange(newPlanId)—(newPlanId: string) => Promise<SubscriptionPreviewChange>— previews switching tonewPlanId. Returns the full change breakdown:direction(upgrade/downgrade/lateral),timing(immediate/deferred),dueNow,effectiveAt, entitlement diffs, and credit deltas. Memoized, so it is safe in auseEffectdependency array.
useCreateSubscription
Mutation that subscribes the customer in context to a plan.salesMotion defaults to self_serve server-side. useCreateSubscription(options?) takes no id; options is a HandleSettlementOptions object of settlement callbacks (onChargedInline, onRequiresForm, onInvoiceSent, onCreated, onDeferred, onNoAction, …).
The returned createSubscription(input) alias accepts:
The plan to subscribe to.
Override the billing currency. Defaults to the plan’s currency.
Arbitrary metadata to attach to the subscription.
Force the inline
<PaymentForm> even when a payment method is already on file.Override the auto-generated
Idempotency-Key.createSubscription(input) => Promise<SettleOutcome> (an ergonomic alias for mutateAsync; the on* callbacks have already been dispatched against the outcome).
useChangePlan
Mutation that moves an existing subscription to a new plan (upgrade, downgrade, or lateral). The server dispatches inline charge (PLG with a payment method), recovery checkout (no PM or decline), an SLG invoice, or a deferred change. Preview the change first withuseSubscription’s previewChange.
useChangePlan(subscriptionId, options?) — the subscription id is the first argument; options is a HandleSettlementOptions object. The returned changePlan(input) alias accepts:
The plan to switch to.
Force the inline
<PaymentForm> even when a payment method is already on file.Override the auto-generated
Idempotency-Key.changePlan(input) => Promise<SettleOutcome>.
useAttachAddon
Mutation that attaches an addon plan to an existing subscription. Portal callers typically setforceForm so addon purchases always confirm through the inline <PaymentForm> rather than silently debiting the saved card.
useAttachAddon(subscriptionId, options?) — the parent subscription id is the first argument; options is a HandleSettlementOptions object (also supports onInvoiceAdded). The returned attachAddon(input) alias accepts:
The addon plan to attach.
Force the dispatcher to mint
requires_form even when a payment method is on file, so the purchase always shows the inline <PaymentForm> and is explicitly confirmed — no silent off-session debit of the default card.Override the auto-generated
Idempotency-Key.attachAddon(input) => Promise<SettleOutcome>.
useCancelSubscription
Mutation that cancels a subscription. Cancellation moves no money, so — unlike the mutations above — it does not return aSettleOutcome; it resolves to a plain SubscriptionMutationResult.
useCancelSubscription(subscriptionId, options?) — the subscription id is the first argument. options carries a single onSuccess(result: SubscriptionMutationResult) => void callback. The returned cancel(input?) alias accepts (all optional — call cancel() for a default period-end cancel):
Cancel right now instead of at the end of the current billing period.
Machine-readable reason code stored on the subscription.
Free-text note explaining the cancellation.
cancel(input?) => Promise<SubscriptionMutationResult>, resolving to { subscriptionId, status, cancelAtPeriodEnd, effectiveAt }.
useUncancelSubscription
Mutation that reverses a pending cancellation, keeping the subscription live. Like cancel, it moves no money and resolves to a plainSubscriptionMutationResult.
useUncancelSubscription(subscriptionId, options?) — the subscription id is the first argument. The uncancel alias takes no input (call uncancel()). options carries a single onSuccess(result: SubscriptionMutationResult) => void callback.
Returns — a React Query mutation plus uncancel() => Promise<SubscriptionMutationResult>, resolving to { subscriptionId, status, cancelAtPeriodEnd, effectiveAt }.
See also
Settlement model
The
SettleOutcome union and on* handlers the mutation hooks dispatch.Invoices
Preview renewals with
useUpcomingInvoice and collect payment.Entitlements & gates
What each subscription grants the customer.
Customer
The customer these subscriptions belong to.