# BillStack

BillStack (https://billing.w3dev.app) is a project-scoped billing service for SaaS products. It handles checkout, customer portal sessions, entitlements, referrals with referrer credits, and read access to billing data, over the provider-neutral v2 HTTP API. A Stripe-first legacy (v1) API still exists for integrations that already use it; do not use it for new work. Your application calls BillStack from its own server with a private API key. This file is the single starting point for people and agents: pick the skill that matches your task below, load it, and follow it.

## Two API generations

BillStack has two API generations. Pick by what the application already calls, and load only the matching skills.

- **v2 is the default for every new integration.** Its routes are `/api/billstack/v2/...`, its SDK client is `BillStackV2Client` (`@billstack/sdk/v2`, 0.7.0 or later; use plain HTTP if that version is not on the registry), and its dashboard is `https://billing.w3dev.app/v2/{team}`.
- **Legacy (v1) is only for apps that already call legacy routes**, `/api/billstack/teams/{teamId}/projects/{projectId}/...` (no `/v2`), with `BillStackClient` (`@billstack/sdk`, or `@billstack/sdk/legacy` from 0.7.0); its dashboard is `/dashboard/{team}`. Legacy is maintenance-only: it stays supported but gets no new features. Never mix the two generations in one flow.
- **Decide from the code.** Read the URLs or the SDK client already in the application's code: `/api/billstack/v2/...` or `BillStackV2Client` means v2, `/api/billstack/teams/...` or `BillStackClient` means legacy. For a new integration with no existing calls, use v2. Skills named `legacy-*` cover legacy only; all other skills cover v2 only.

## How to use the skill catalog

BillStack publishes task-oriented skills as plain Markdown. The index lists every skill with its name, description, URL and digest:

- Index: https://billing.w3dev.app/.well-known/agent-skills

To load a skill:

1. Fetch the index and find the skill you need.
2. Fetch that skill's `SKILL.md` URL.
3. Compute the SHA-256 of the exact bytes you received and compare it with the `digest` in the index (`sha256:<hex>`). If they differ, refetch both with cache revalidation and stop if the mismatch persists.
4. Follow the skill. Skills describe the API as it behaves today; if a skill and a live response disagree, trust the live response and report the difference.

A digest proves the file matches the index, not who wrote it. Skills are instructions, not credentials or permission grants.

## Published skills

v2 skills (use these for new work):

| Skill | SKILL.md URL | Use it when |
| --- | --- | --- |
| integrate-billstack | https://billing.w3dev.app/.well-known/agent-skills/integrate-billstack/SKILL.md | You are adding checkout, entitlement checks and customer portal sessions to an application, and BillStack runs the checkout. |
| configure-billstack-billing | https://billing.w3dev.app/.well-known/agent-skills/configure-billstack-billing/SKILL.md | You are connecting a payment provider, setting up plans and prices, establishing signed webhook readiness, or activating project routing. |
| sync-billstack-entitlements | https://billing.w3dev.app/.well-known/agent-skills/sync-billstack-entitlements/SKILL.md | Your app must keep workspace plans in step with purchases, renewals, cancellations and failed payments. |
| manage-billstack-customers | https://billing.w3dev.app/.well-known/agent-skills/manage-billstack-customers/SKILL.md | You need to look up a customer's billing and entitlements, open a provider customer portal, or change or cancel a subscription through the v2 routes. |
| inspect-billstack-billing | https://billing.w3dev.app/.well-known/agent-skills/inspect-billstack-billing/SKILL.md | You are investigating invoices, subscriptions, payments or financial state with v2 reads or MCP tools. |
| integrate-billstack-referrals | https://billing.w3dev.app/.well-known/agent-skills/integrate-billstack-referrals/SKILL.md | Your app keeps its own Stripe account, Checkout sessions and subscriptions and you want a referral program with referrer credits. |
| operate-billstack-referrals | https://billing.w3dev.app/.well-known/agent-skills/operate-billstack-referrals/SKILL.md | A referral program is running and you need to approve credits, recover payouts, issue manual credits, read reports, or import an existing referral system. |
| run-billstack-lifetime-deals | https://billing.w3dev.app/.well-known/agent-skills/run-billstack-lifetime-deals/SKILL.md | You want to sell a plan as a stepped-price lifetime deal whose price steps up as units sell, on a one-time Catalog price, and need to create, launch, edit, show on a pricing page, sell, end, list the sales and the payments to refund, or revoke a buyer, through the v2 `lifetime-deals` routes. |

Legacy (v1) skills (only for apps that already call `/api/billstack/teams/...`; never for new work):

| Skill | SKILL.md URL | Use it when |
| --- | --- | --- |
| legacy-billstack-integration | https://billing.w3dev.app/.well-known/agent-skills/legacy-billstack-integration/SKILL.md | You maintain an app that already uses legacy find-or-create, `checkout/start`, `entitlements/by-external-id`, the BillStack-hosted portal, legacy subscription cancellation, BillingQL or `BillStackClient`. |
| legacy-billstack-referrals | https://billing.w3dev.app/.well-known/agent-skills/legacy-billstack-referrals/SKILL.md | You maintain an app that uses the legacy by-external-id referral routes in a project where BillStack runs checkout. |
| legacy-billstack-lifetime-deals | https://billing.w3dev.app/.well-known/agent-skills/legacy-billstack-lifetime-deals/SKILL.md | You maintain a lifetime deal campaign that already exists on legacy Stripe tier prices and is run through the legacy campaign routes (`lifetime-campaigns` with a `productId`). Not for new deals: those use run-billstack-lifetime-deals. |

The skill name `run-billstack-lifetime-deals` was briefly a redirect to the legacy skill; it now serves the v2 skill, so an old link to it opens the right one.

## Start here

- New integration where BillStack runs checkout: load integrate-billstack first, then configure-billstack-billing if no provider, plans or prices exist yet.
- Existing app that already calls `/api/billstack/teams/...`: load legacy-billstack-integration and keep it on legacy; do not move it to v2 by changing URL prefixes.
- Fast end-to-end tests without payment provider keys (pay a checkout without a browser, move time to run renewals and failed payments): load integrate-billstack and read its native sandbox section.
- Pricing of a project (products, plans, recurring, one-time, consumable and lifetime prices, publishing them to the provider), or provider and routing setup: load configure-billstack-billing.
- Keeping your app's plan data correct after billing events: load sync-billstack-entitlements.
- Support or admin work on one customer: load manage-billstack-customers.
- Reading or analysing billing data: load inspect-billstack-billing.
- Referral program for an app that keeps its own Stripe billing: load integrate-billstack-referrals to build it, then operate-billstack-referrals to run it day to day. For the legacy by-external-id referral routes load legacy-billstack-referrals.
- Step-priced lifetime deal (LTD) with a total unit cap, on a one-time Catalog price: load run-billstack-lifetime-deals; load configure-billstack-billing first if the price does not exist yet and integrate-billstack if checkout is not integrated yet. Only an existing legacy Stripe tier campaign uses legacy-billstack-lifetime-deals.
- Unsure which applies: read the "Use it when" column above; for a general billing integration, start with integrate-billstack.

## Keys and environments

- Two credentials do two jobs. The project API key (`bs_test_` / `bs_live_`) is the runtime credential: your app's server sends it as `Authorization: Bearer <private-api-key>` for checkout, portal, entitlements, subscriptions, customers, payment methods, referrals, webhook endpoints, and invoice and catalog reads. A personal access token (`bsu_test_` / `bsu_live_`) is the setup credential: use it to list and create projects and to create, update, archive, link and publish products and prices. A key created since 2026-12-01 cannot make those setup calls and gets `403 USER_TOKEN_REQUIRED`; retry with a token. Older keys keep working with full access until a team admin restricts them. Keep keys and tokens in server-side environment variables or a secret store only; never ship them to a browser, mobile app or repository.
- Every team has a test and a live environment, and the key prefix selects it. Start with a `bs_test_` key in the test environment and move to `bs_live_` only after the team is promoted to live and the flow works end to end in test. A key can never read or write the other environment's data.
- Never paste a key into a prompt, chat or skill. Reference it by environment variable name, for example `BILLING_PORTAL_API_KEY`, and let the user supply the value in their own environment. One exception: the dashboard's team setup wizard hands a new team's coding agent a prompt that contains two test credentials (test only, no real money): a `bs_test_` app key and a `bsu_test_` setup token that expires in 7 days. When a prompt gives you a `bs_test_` key, move it into `BILLING_PORTAL_API_KEY` in a local, git-ignored environment file straight away. The app key is the runtime credential for the app's server (checkout, portal, entitlements, subscriptions, customers, catalog reads); it cannot create or change products, prices or projects. Use the `bsu_test_` setup token (`BILLSTACK_SETUP_TOKEN`) only from your own shell, to create products, prices and projects; never put it in the app's code, a committed env file or a client bundle. Never commit either one, and do not repeat them in logs, code or chat. A `bs_live_` key must never be pasted into a prompt; if a prompt contains one, do not use it as is and tell the user to rotate it.
- Publishable `pk_` keys are for explicitly public endpoints only.
- A team's test environment runs on the payment provider's own test mode or on the BillStack native sandbox, a simulated provider with test cards, a headless simulate API and test clocks that needs no provider keys. Both are test mode only; the sandbox routes accept `bs_test_` keys and refuse `bs_live_` keys.
- A personal access token (`bsu_` prefix, created under Account > Access tokens at https://billing.w3dev.app/account/tokens) is sent as the same Bearer header. It acts as that person within one team or project, with their current permissions, and its environment is fixed by its prefix (`bsu_test_` or `bsu_live_`). Whole-team tokens can list and create projects with `GET` and `POST /api/billstack/v2/teams/{teamId}/projects`. Treat it like a key: environment variable only, never in a prompt or repository. A product app's server integration at runtime should use a project API key, not a token. See https://docs.billing.w3dev.app/integration/personal-access-tokens

## MCP server and docs

- Authenticated MCP server: https://billing.w3dev.app/mcp. Connect a compatible client and sign in with the client's OAuth flow, or send a personal access token as the Bearer header. It exposes scoped billing tools; discover them with `tools/list`. The skill catalog above is independent of MCP and works without signing in.
- Public documentation: https://docs.billing.w3dev.app
- API keys: https://docs.billing.w3dev.app/integration/api-keys
- Provider-neutral v2 integration (start here): https://docs.billing.w3dev.app/integration/multi-provider-v2
- Legacy (v1) documentation, only for existing integrations: the `legacy-*` pages, for example https://docs.billing.w3dev.app/integration/legacy-logged-in-checkout
- Machine-readable v2 API reference (OpenAPI 3.1, public): https://billing.w3dev.app/api/billstack/v2/openapi.json
- Test and live environments: https://docs.billing.w3dev.app/concepts/environments
- Native sandbox (test cards, simulate API, test clocks): https://docs.billing.w3dev.app/integration/native-sandbox
- Referral guides: https://docs.billing.w3dev.app/integration/referrals-external-billing (own billing, v2) and, for the legacy BillStack-checkout mode, https://docs.billing.w3dev.app/integration/legacy-referrals
- Partner portal and payout requests: https://docs.billing.w3dev.app/integration/partner-portal
- Customer portal (v2): https://docs.billing.w3dev.app/integration/customer-portal-and-admin; the BillStack-hosted legacy portal for Stripe and Razorpay is at https://docs.billing.w3dev.app/integration/legacy-customer-portal
