---
name: legacy-billstack-referrals
description: "Legacy (v1, Stripe-first) BillStack referral routes only, for projects where BillStack runs checkout and entitlements (billingOwnership billstack): get or create a referral code by externalId, apply a code to a referee, read referral stats and process rewards under /api/billstack/teams/{teamId}/projects/{projectId}/referrals/.... Maintenance only; for a new referral program load integrate-billstack-referrals (v2)."
---

# Legacy BillStack referrals (v1, BillStack checkout mode)

This skill covers the **legacy (v1)** referral routes only. They apply to projects where BillStack runs checkout and entitlements (`billingOwnership: "billstack"`, the default of an unconfigured project). A referral starts `pending`, becomes `refund_period` after the referee's payment activates an entitlement, and is rewarded with bonus months after `refundWindowDays`. Do not use these routes for a project in external billing mode, and do not use them for new work: load [integrate-billstack-referrals](https://billing.w3dev.app/.well-known/agent-skills/integrate-billstack-referrals/SKILL.md) (v2, your own Stripe billing) and [operate-billstack-referrals](https://billing.w3dev.app/.well-known/agent-skills/operate-billstack-referrals/SKILL.md) instead. Human-readable reference: https://docs.billing.w3dev.app/api-reference/legacy-referrals

Prefix `L = https://billing.w3dev.app/api/billstack/teams/{teamId}/projects/{projectId}` (no `/v2`). Authenticate with a private project key (`bs_test_` or `bs_live_`) from a trusted backend, referenced by environment variable name (`BILLING_PORTAL_API_KEY`); never paste it. The key's environment is the environment of the data. Responses are plain JSON, not wrapped in `data`/`meta`. Start in test. Load [legacy-billstack-integration](https://billing.w3dev.app/.well-known/agent-skills/legacy-billstack-integration/SKILL.md) for the credentials, checkout and entitlement routes these referrals attach to.

## Get or create a code

`GET` or `POST L/referrals/code/by-external-id/{externalId}`. An optional `POST` body is `{ "email"?, "phone"?, "name"? }`. It answers `{ code }`; the shareable string is `code.code`. The code row is created on the first call and reused afterwards. `400 Referrals are not enabled` means the project's referral config is missing or disabled.

## Apply a code

`POST L/referrals/apply/by-external-id` with `{ "code", "refereeExternalId", "email"?, "phone"?, "name"? }` answers `201 { referral }` with the referral row (`status: "pending"`, credit amounts, `rewardMonths`, `expiresAt`). Errors are `400 code and refereeExternalId are required` and `400 Invalid referral code` (unknown, inactive, another project, or the referrer applying their own code). Call it when the referee signs up and an authenticated app user is known.

## Read stats

`GET L/referrals/stats/by-external-id/{externalId}` answers `{ totalReferrals, registeredReferrals, onTrial, referralsUsed, inRefundPeriod, canceledReferrals, monthsEarned }` for an ambassador page. `onTrial` is always `0` and kept for compatibility; `canceledReferrals` counts `cancelled` and `reversed` referrals.

## Process rewards

`POST L/referrals/rewards/process` with an optional `{ "limit": 100 }` (default 100, at most 500) scans referrals past their refund window and claims eligible rewards. It answers `{ scanned, rewarded }`. Rewards are not processed automatically: schedule this call periodically (for example daily). It needs a private API key or project-admin dashboard access.

## Verify

In a test project: get a code for user A, apply it for user B, complete a test checkout for B, wait past the refund window or use a short `refundWindowDays`, call `rewards/process`, then read A's stats. Report the sanitized identifiers, project, environment and the read-back counts.
