Legacy TypeScript SDK
Use the BillStackClient legacy client from @billstack/sdk for integrations that already call the Stripe-first API.
Legacy (v1) API. This page describes the Stripe-first legacy client, BillStackClient. It stays supported for existing integrations but is frozen: new features ship only in v2. Prefer v2 for new work: TypeScript SDK.
This page covers BillStackClient, which calls /api/billstack/teams/{teamId}/projects/{projectId}/.... Import it from the package root, @billstack/sdk, which works on every version; from @billstack/sdk 0.7.0 you can also import it from @billstack/sdk/legacy. Install the package as described in the TypeScript SDK guide.
Configure the client
import { BillStackClient } from "@billstack/sdk";
const client = new BillStackClient({
baseUrl: process.env.BILLING_PORTAL_URL!,
apiKey: process.env.BILLING_PORTAL_API_KEY!,
teamId: process.env.BILLING_PORTAL_TEAM_ID!,
projectId: process.env.BILLING_PORTAL_PROJECT_ID!,
});Common operations
// Idempotent customer creation keyed on your app user ID
const { customer, created } = await client.customers.findOrCreate({
externalId: user.id,
email: user.email,
name: user.name,
});
// Subscription status for the internal customer ID
const status = await client.customers.getSubscriptionStatus(customer.id);
// Resolve a price by tier alias
const price = await client.prices.resolve({ tier: "PRO", interval: "month" });
// Customers with an active subscription and a paid latest invoice
const payingCustomers = await client.customers.list({
query: "is_paying:true",
});is_paying:true returns only customers with at least one active, unpaused
subscription in its current billing period whose latest corresponding invoice
is paid. An active subscription with an unpaid latest invoice does not qualify.
Use is_cash_paying:true for the stricter cash-backed recurring-revenue
population. This qualifier is team-scoped because it relies on invoice-line
and payment-adjustment evidence in the team's Stripe mirror.
getSubscriptionStatus returns a SubscriptionStatus shape (tier, status, currentPeriodEnd, lifetimeEntitlement, etc.) — it is not the same as the entitlement snapshot described in the Entitlements guide, and it is not a drop-in replacement for it. The /entitlements/by-external-id endpoint has no SDK wrapper yet; call it with raw fetch as shown in that guide.
Resources
SDK coverage gap: checkout and portal below call the internal-ID endpoints (POST /checkout, POST /portal), which expect a BillStack customerId. The externalId-first flows this documentation set otherwise teaches (/checkout/start, /portal/by-external-id) currently have no SDK wrapper — use raw fetch for those, as shown in the Logged-in Checkout and Customer Portal and Admin Links guides.
customers— CRUD,findOrCreate,getByExternalId,getSubscriptionStatus, payment methodsproducts,prices,subscriptions,invoices— CRUDcheckout—create()posts to/checkout(internalcustomerId), returns{ sessionId, url }portal—create()posts to/portal(internalcustomerId), returns{ sessionId, token, portalUrl, expiresAt }(urlis a deprecated alias ofportalUrl; before 0.4.1 the type declared onlyurl, which the server never returned). See Customer portalanalytics— project metricsreferrals— referral codes and configteam.getPublishableKey()— Stripe publishable key for the teamtiers.listPublic()— public tier pricing (publishable key); eachPublicTier'slifetimeCampaignfield is a typedPublicLifetimeCampaign | null— see Lifetime Deal Campaigns
Error handling
import { BillStackError } from "@billstack/sdk";
try {
await client.customers.get("missing");
} catch (err) {
if (err instanceof BillStackError && err.status === 404) {
// handle not-found
}
}Environment
The legacy client reads the environment from the key prefix: client.environment is "test" or "live" (null for a legacy bs_ key, which is live) and client.isTestMode is true for a test key. Pass environment: "live" in the client config as a guard if production code must never run with a test key; the constructor throws when the key contradicts it. The key is sent unchanged and the server decides the environment from it. The v2 client has no such guard.
See also API Keys and Authentication.