> ## Documentation Index
> Fetch the complete documentation index at: https://docs.integrateledger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Referral programs

> Grow through your customers. Reward a customer who brings a new one, and the new customer too, at signup or when they first pay, with fraud checks built in.

> **Refer a friend:** you get 1,000 credits when they first pay for Pro or Team; they get 20% off for 3 months.

Turn customers into a sales channel. A referral program gives a reward to a customer who brings a new one (the **referrer**), and optionally one to the new customer (the **referee**). Ledger makes each referrer's code, checks every redemption, waits for the trigger you choose, checks the cards in Stripe, and pays out both sides.

Each reward is a [reward](https://docs.integrateledger.com/billing/discounts.md) from your catalog: a coupon that takes money off, or a [grant](https://docs.integrateledger.com/billing/discounts.md#grant-codes) of credits or units.

## The catalog

Add `referralPrograms` to your catalog beside the rewards they name, or write one with [`POST /v1/referral-programs`](https://docs.integrateledger.com/api-reference/referral-programs/write):

```json ledger.catalog.json theme={"system"}
{
  "referralPrograms": [
    {
      "id": "friends",
      "name": "Refer a friend",
      "referrerRewardId": "bonus_1000",
      "refereeRewardId": "launch20",
      "trigger": "first_payment",
      "planIds": ["pro", "team"],
      "maxRewardsPerReferrer": 10
    }
  ]
}
```

| Field | |
| - | - |
| `referrerRewardId` | The referrer's reward, a coupon or a grant. Required. |
| `refereeRewardId` | The new customer's reward. Left out: only the referrer is rewarded. |
| `trigger` | `signup`: both rewards when the new customer redeems the code. `first_payment`: when they first pay money for a plan. |
| `planIds` | `first_payment` only: the plans whose first payment counts, every version of each. Left out: any paid plan. |
| `maxRewardsPerReferrer` | The most rewards one referrer gets. Left out: no limit. |

A catalog without `referralPrograms` leaves your programs as they are; one with it states all of them, and a program left out is archived. A change to a program applies to referrals from then on; referrals already rewarded keep what they got.

## Give each referrer a code

[Get a customer's referral code](https://docs.integrateledger.com/api-reference/customers/referral-code.md) returns the referrer's code in a program: made on the first call, the same one on every call after. Show it on their account page or in a share link.

```typescript TypeScript theme={"system"}
const { code } = await billing.createReferralCode({
	customerId: "user_123",
	body: { programId: "friends" },
});
```

```python Python theme={"system"}
referral_code = billing.create_referral_code(customer_id="user_123", program_id="friends")
```

```bash cURL theme={"system"}
curl "https://api.integrateledger.com/v1/customers/referral-codes?customerId=user_123" \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"programId": "friends"}'
```

Codes are 8 characters from A-Z and 2-9, without the look-alikes 0, O, 1, I and L, and unique among all your codes, promotion codes included.

## Redeem a referral code

The new customer types the code where they'd type a promotion code, and your app sends it to [`POST /v1/customers/redemptions`](https://docs.integrateledger.com/api-reference/customers/redeem-promotion-code), the same route as any [promotion code](https://docs.integrateledger.com/billing/discounts.md#in-your-app):

```typescript TypeScript theme={"system"}
const result = await billing.redeemPromotionCode({
	customerId: "user_456",
	body: { code: "K7MQ3XPA" },
});
```

```python Python theme={"system"}
result = billing.redeem_promotion_code(customer_id="user_456", code="K7MQ3XPA")
```

```bash cURL theme={"system"}
curl "https://api.integrateledger.com/v1/customers/redemptions?customerId=user_456" \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"code": "K7MQ3XPA"}'
```

The reply has the `referral`: `rewarded` at once for a `signup` program, `pending` until the trigger for `first_payment`. When the program gives the new customer a reward at redeem, the reply also carries their redemption.

## What each side gets, and when

| | `signup` | `first_payment` |
| - | - | - |
| New customer's coupon | At redeem: it discounts their first attach of a plan it covers | Same |
| New customer's grant | At redeem | At the trigger |
| Referrer's coupon | At redeem: onto their current plan if it covers it, or their next attach | At the trigger, the same way |
| Referrer's grant | At redeem | At the trigger |

A coupon goes on the bill like any [discount](https://docs.integrateledger.com/billing/discounts.md#one-discount-per-bill), and a grant joins the customer's [balance](https://docs.integrateledger.com/concepts/balances.md) and sends `credits.granted`.

## The trigger

With `first_payment`, a referral pays out once it has turned into revenue: the new customer's first invoice with money paid for a covered plan. Ledger records the trigger in the same transaction as the payment, from every path that pays for a plan: a first invoice, a lifetime plan, a converted [trial](https://docs.integrateledger.com/pricing/trials.md), a renewal or an upgrade. A free plan, a trial's \$0 invoice or a 100%-off first month waits for the first invoice with money on it.

The referral worker then pays it out within seconds:

1. It reads the cards saved on both customers in Stripe. If the new customer has a card the referrer has saved (the same card fingerprint), the referral is `refused` with `same_payment_method`.
2. If the referrer already has `maxRewardsPerReferrer` rewards, it's `refused` with `referrer_limit_reached`.
3. Otherwise it's `rewarded`: both rewards are given and a `referral.rewarded` [webhook](https://docs.integrateledger.com/webhooks.md) is sent.

The new customer keeps their own reward when a referral is refused. If Stripe is unavailable, the worker retries, from 30 seconds apart up to once an hour, until it gets an answer.

## Fraud checks

Ledger refuses a referral code for:

* the referrer themselves, or a customer on the same Stripe customer (`422 self_referral`);
* a customer already referred in this program (`409 already_referred`);
* a customer who has paid for a plan or credit pack (`409 referee_not_new`), so referral rewards go to genuinely new customers;
* with `signup`, a referrer who already has `maxRewardsPerReferrer` rewards (`409 referrer_limit_reached`).

At the trigger, the card check stops a customer referring themselves with a second account.

## See a customer's referrals

[List a customer's referrals](https://docs.integrateledger.com/api-reference/customers/referrals.md) returns their `codes`, the customers they referred (`asReferrer`, newest first), and who referred them (`asReferee`), each with its `status` and `refusedReason`.

```typescript TypeScript theme={"system"}
const referrals = await billing.listCustomerReferrals({ customerId: "user_123" });
```

```python Python theme={"system"}
referrals = billing.list_customer_referrals(customer_id="user_123")
```

```bash cURL theme={"system"}
curl "https://api.integrateledger.com/v1/customers/referrals?customerId=user_123" \
  -H "Authorization: Bearer $LEDGER_API_KEY"
```

## Routes

| Route | Does |
| - | - |
| [`GET /v1/referral-programs`](https://docs.integrateledger.com/api-reference/referral-programs/list) | Lists the active programs |
| [`GET /v1/referral-programs/{programId}`](https://docs.integrateledger.com/api-reference/referral-programs/get) | Reads one program, archived or not |
| [`POST /v1/referral-programs`](https://docs.integrateledger.com/api-reference/referral-programs/write) | Creates or updates a program |
| [`DELETE /v1/referral-programs/{programId}`](https://docs.integrateledger.com/api-reference/referral-programs/archive) | Archives a program |
| [`POST /v1/customers/referral-codes`](https://docs.integrateledger.com/api-reference/customers/referral-code) | Gets a customer's code in a program |
| [`POST /v1/customers/redemptions`](https://docs.integrateledger.com/api-reference/customers/redeem-promotion-code) | Redeems a referral code, as any code |
| [`GET /v1/customers/referrals`](https://docs.integrateledger.com/api-reference/customers/referrals) | A customer's codes and referrals |

## Webhooks

| Event | When |
| - | - |
| `reward.redeemed` | Each reward given, with `role: "referrer"` or `"referee"` |
| `credits.granted` | A grant reward's credits, with `source: "reward"` and the `rewardId` |
| `referral.rewarded` | The referral paid out: `referralId`, `programId`, `referrerId`, `refereeId` |

## Rules

* A program's rewards are active rewards of the environment (`unknown_reward`).
* Archive a program before archiving a reward it gives (`409 reward_in_use`). Archiving a program stops new codes and redemptions; referrals already made keep their rewards.
* Each customer is referred once per program, and only before they've paid for a plan or credit pack.
* A refund of the new customer's payment leaves the referrer's reward in place.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.