Quickstart
Connect a SaaS app to BillStack with project-scoped checkout and entitlements.
This guide shows the provider-neutral v2 server-side integration for a product app, which is the one to use for every new integration. If your app already calls /api/billstack/teams/..., it uses the legacy (v1) API: see Legacy logged-in checkout and the Legacy TypeScript SDK. Agents can load the public skill catalog for task-oriented API instructions.
Fastest path: the team setup wizard
Creating a team in the dashboard walks you through three steps:
- Team & provider. Name the team, pick its URL, choose where to test and choose its default payment provider, Stripe or Razorpay. The team starts in the test environment. The question "Where do you want to test?" offers the provider sandbox (Stripe or Razorpay test mode, the default) or the BillStack native sandbox, which needs no keys. On the native sandbox the default provider is optional: you can connect a real provider before going live.
- Test keys. On the provider sandbox, connect the default provider's test keys; once it has keys you can connect the other provider's test keys too. One provider is enough to continue. You can skip this and connect keys later under Integrations, where Regions sets which countries each provider sells to. On the native sandbox this step reads "No keys needed": it lists what the sandbox covers, links to Integrations > Sandbox for switching to a provider sandbox later, and you just continue.
- Hand off to your agent. BillStack makes sure the team has a default project (named after the team), creates a
bs_test_API key for it, and shows a prompt to paste into your coding agent (Claude Code, Codex, Cursor and similar). The prompt names the team and project, the default payment provider, every provider with test keys connected, and the test key, and points the agent at billing.w3dev.app/skill.md. For a team on the native sandbox the prompt also lists the simulate API, the test cards and the test clock endpoints, so the agent can run end-to-end tests without a dashboard.
Steps 2 and 3 live at /team/{teamSlug}/setup, so a page refresh keeps your place. The prompt contains two credentials and is shown once, only after you click Generate prompt: an app key (bs_test_, for your app's server: checkout, portal, entitlements and catalog reads) and a setup token (bsu_test_, valid for 7 days, for your agent to create products and prices from its own shell). Generating it again creates a new pair and revokes the earlier unused ones. Test credentials move no real money. Never paste a bs_live_ key into a prompt. The sections below describe the same setup done by hand.
No provider keys yet? Choose the BillStack sandbox for the team's test environment (Integrations > Sandbox). It simulates the payment provider, so you can run checkout, renewals and failed payments end to end with a bs_test_ key alone: pay a checkout with the simulate API and move time with test clocks. Switch to the provider sandbox when you want to see your provider's own test mode. Going live always needs a real payment provider (Stripe or Razorpay) with live keys, even for a team that started on the sandbox.
1. Create a team and project
In BillStack:
- Create or open a team. When you create a team you choose its payment provider (Stripe or Razorpay).
- Connect that provider's keys for the team under Integrations (
/v2/{team}/~/integrations). This guide uses Stripe. - Create a project for your SaaS app.
- Create products and prices in the project's Catalog (
/v2/{team}/{project}/catalog) and publish each price to your provider. - Copy the team ID, the project ID and the price variant ID of the price you want to sell.
One team can own multiple projects. Each project has isolated customers, prices, referrals, subscriptions, invoices, and entitlements.
The payment provider (Stripe or Razorpay) is chosen when the team is created and can be changed until you connect keys. A team connects at most one account per provider, and its provider setup is shared by every project under it. A team can take payments through Stripe and Razorpay at once: each provider account and the countries it serves form a region. Country routing is off by default for every project, so the checkout in this guide goes to the team's default payment connection and needs no country; turn it on to route by country (see step 4 and Architecture). Each team also has a test and a live environment: start in test with bs_test_ keys and promote to live when you are ready (see Test and live environments).
2. Add app environment variables
BILLING_PORTAL_URL=https://billing.w3dev.app
BILLING_PORTAL_TEAM_ID=team_...
BILLING_PORTAL_PROJECT_ID=proj_...
BILLING_PORTAL_API_KEY=<private-api-key> # bs_test_... for development, bs_live_... for production
NEXT_PUBLIC_APP_URL=https://your-app.exampleThe following dashboard/slug variables are deprecated compatibility variables. They are not read by the current BillStack app or SDK; remove them from new integrations. If an existing integration still needs an admin-panel deep link, configure either:
BILLING_PORTAL_DASHBOARD_URL=https://billing.w3dev.app/v2/team-slug/project-slugor:
BILLING_PORTAL_WORKSPACE_SLUG=team-slug
BILLING_PORTAL_PROJECT_SLUG=project-slugBILLING_PORTAL_WORKSPACE_SLUG and BILLING_PORTAL_PROJECT_SLUG are deprecated compatibility variables. The variables shown here belong in the integrating app's environment, not BillStack's own .env.local.
Do not keep provider secret keys in your product app once BillStack owns checkout and webhooks.
3. Add a server helper
const baseUrl = process.env.BILLING_PORTAL_URL!.replace(/\/$/, "");
const teamId = process.env.BILLING_PORTAL_TEAM_ID!;
const projectId = process.env.BILLING_PORTAL_PROJECT_ID!;
const apiKey = process.env.BILLING_PORTAL_API_KEY!;
const projectPath = `/api/billstack/v2/teams/${teamId}/projects/${projectId}`;
async function billing(path: string, body?: unknown) {
const res = await fetch(`${baseUrl}${path}`, {
method: body === undefined ? "GET" : "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: body === undefined ? undefined : JSON.stringify(body),
cache: "no-store",
});
const result = await res.json().catch(() => ({}));
if (!res.ok) {
throw new Error(result.error?.code ?? "Billing request failed");
}
return result.data; // v2 wraps every success in { data, meta }
}You can use the typed BillStackV2Client from @billstack/sdk/v2 (0.7.0 or later; keep this helper if that version is not on the registry yet) instead of writing this helper.
4. Start logged-in checkout
Create the checkout for the logged-in customer. The price variant is the canonical price you published in step 1. Country routing is off by default, so the checkout goes to the team's default payment connection and needs no country.
const checkout = await billing(`${projectPath}/checkout`, {
externalId: `checkout-${user.id}-${order.id}`,
customerExternalId: user.id,
priceVariantId,
idempotencyKey: `checkout-${order.id}`,
successUrl: `${process.env.NEXT_PUBLIC_APP_URL}/account/billing?checkout=success`,
cancelUrl: `${process.env.NEXT_PUBLIC_APP_URL}/pricing?checkout=cancelled`,
});Redirect the browser to checkout.url. A retry of the same checkout with the same idempotencyKey returns the stored result. v2 never silently falls back to another provider; see Multi-provider v2 for regions and the error codes.
If you enabled country routing
Only a project that opted in (PATCH .../routing-settings with { "countryRoutingEnabled": true }, see Multi-provider v2) routes by country. For such a project, the customer confirms their billing country, you mint a confirmation for that customer, and you create the checkout with it:
const { confirmation } = await billing(`${projectPath}/country-confirmations`, {
country: confirmedCountry, // ISO alpha-2, chosen by the customer
scope: "checkout",
customerExternalId: user.id,
});
const checkout = await billing(`${projectPath}/checkout`, {
externalId: `checkout-${user.id}-${order.id}`,
customerExternalId: user.id,
priceVariantId,
countryConfirmation: confirmation,
idempotencyKey: `checkout-${order.id}`,
successUrl: `${process.env.NEXT_PUBLIC_APP_URL}/account/billing?checkout=success`,
cancelUrl: `${process.env.NEXT_PUBLIC_APP_URL}/pricing?checkout=cancelled`,
});A confirmation belongs to one customer and pays for one checkout, so mint a new one for every attempt. A bs_test_ key may send an unsigned country instead.
5. Read entitlements
const entitlement = await billing(
`${projectPath}/entitlements/${encodeURIComponent(user.id)}`,
);
if (!entitlement.effective.isEntitled) {
// Show upgrade UI or enforce free limits.
}Use externalId as the durable bridge between your app user and BillStack customer. A success URL is not proof of payment: signed provider webhooks drive billing state, so recheck entitlements with bounded backoff after the customer returns. See Entitlements for the grants behind effective and how to cache it.