> ## 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.

# Discounts and promotion codes

> Run launch deals, partner codes, one-off discounts and codes that grant free credits from your catalog. Ledger creates them in Stripe, checks and counts every code, and customers still get the full plan.

> **LAUNCH20:** 20% off Pro or Team for the first 3 months.

Run launch deals, partner codes and one-off offers straight from your catalog. A discount lowers what the customer pays, never what they get: Ledger grants the plan's full credits and allowances on every discounted invoice, even one discounted to \$0.

Ledger keeps your coupons and their promotion codes in the catalog as `rewards`, makes them in your Stripe account, and checks and counts every code. Stripe takes the discount off the customer's invoices.

A reward is a **coupon** or a **grant**. A coupon takes money off the plan. A [grant code](#grant-codes) hands out credits or a metered feature's units the moment it's redeemed, such as `WELCOME` for 500 free credits. The same rewards pay out [referral programs](https://docs.integrateledger.com/billing/referrals.md).

## The catalog

```ts ledger.config.ts theme={"system"}
import { defineCatalog, plan } from "integrateledger-cli";

export default defineCatalog({
	features: [],
	plans: [
		plan({ id: "pro", name: "Pro", monthlyCredits: 1000, priceCents: 2900 }),
		plan({ id: "team", name: "Team", monthlyCredits: 5000, priceCents: 9900 }),
	],
	creditPacks: [],
	rewards: [
		{
			id: "launch20",
			name: "Launch: 20% off for 3 months",
			coupon: {
				percentOff: 20,
				duration: "repeating",
				durationMonths: 3,
				planIds: ["pro", "team"],
			},
			maxRedemptions: 500,
			redeemBy: "2027-03-31T00:00:00Z",
			promotionCodes: [
				{ code: "LAUNCH20" },
				{ code: "PH20", maxRedemptions: 100, firstTimeOnly: true },
			],
		},
	],
});
```

```json ledger.catalog.json theme={"system"}
{
  "schemaVersion": 1,
  "features": [],
  "plans": [
    { "id": "pro", "name": "Pro", "monthlyCredits": 1000, "priceCents": 2900 },
    { "id": "team", "name": "Team", "monthlyCredits": 5000, "priceCents": 9900 }
  ],
  "addOns": [],
  "rewards": [
    {
      "id": "launch20",
      "name": "Launch: 20% off for 3 months",
      "coupon": { "percentOff": 20, "duration": "repeating", "durationMonths": 3, "planIds": ["pro", "team"] },
      "maxRedemptions": 500,
      "redeemBy": "2027-03-31T00:00:00Z",
      "promotionCodes": [
        { "code": "LAUNCH20" },
        { "code": "PH20", "maxRedemptions": 100, "firstTimeOnly": true }
      ]
    }
  ]
}
```

You can also write one reward at a time with [`POST /v1/rewards`](https://docs.integrateledger.com/api-reference/rewards/write).

| Field | |
| - | - |
| `coupon.percentOff` or `coupon.amountOffCents` | Exactly one: a percentage above 0 and at most 100 (two decimals), or US cents off each discounted invoice. |
| `coupon.duration` | `once`, `repeating` with `durationMonths`, or `forever`. |
| `coupon.planIds` | The paid plans it discounts, every version of each. `null`: every paid plan, now and later. |
| `maxRedemptions`, `redeemBy` | Limits across every code of the reward. |
| `oncePerCustomer` | A customer redeems the reward at most once, through any code. Defaults to `true`. |
| `promotionCodes` | What customers type: 3 to 40 letters, digits, `_` or `-`, matched without case. Each takes its own `maxRedemptions`, `expiresAt` and `firstTimeOnly` (only customers who have never paid for a plan or pack). |

A catalog without `rewards` leaves your rewards as they are. A catalog with `rewards` states all of them: one left out is archived and its codes deactivated, while discounts already on customers' bills run to their end.

## Durations

| `duration` | Discounts |
| - | - |
| `once` | The next invoice. |
| `repeating` | Every invoice for `durationMonths`, 1 to 36. |
| `forever` | Every invoice until the subscription ends. |

## What a coupon covers

A coupon discounts the price and the [seats](https://docs.integrateledger.com/pricing/per-seat.md) of the plans it names. Usage bills under the plan's own `<Plan> – overage` product, so a Ledger coupon leaves overage and credit packs at their full price, and a [coupon you add in Stripe](#coupons-added-in-stripe) for every product discounts overage too. To give a customer more usage, give them [credits](https://docs.integrateledger.com/customers/balance-adjustments.md).

A percentage coupon discounts a plan in any [currency](https://docs.integrateledger.com/pricing/currencies.md), and a fixed-amount coupon is in US dollars.

## Changing a reward

Changing a reward's amount, duration or plans makes a new Stripe coupon, since Stripe fixes a coupon's terms when it's made. Customers who already have the discount keep its terms, as plan versions keep their price. Every active code moves to the new coupon with its remaining uses.

`GET /v1/rewards` shows each coupon's `stripeSyncStatus`: `synced`, `pending` while its plans wait for their Stripe products, or `failed` with `stripeSyncError`. Ledger retries every hour. A grant lives in Ledger alone, so its status is `null`.

## Grant codes

A grant reward gives the customer credits, or a metered feature's units, the moment they redeem its code: a welcome bonus, a support make-good, or a partner's free usage.

```json ledger.catalog.json theme={"system"}
{
  "rewards": [
    {
      "id": "welcome",
      "name": "500 free credits",
      "grant": {
        "items": [
          { "featureId": "credits", "amount": 500, "expiresAfter": { "months": 1 } },
          { "featureId": "messages", "amount": 1000 }
        ]
      },
      "promotionCodes": [{ "code": "WELCOME" }]
    }
  ]
}
```

| Field | |
| - | - |
| `grant.items` | 1 to 20 items, one per feature. `featureId` is `credits` for the shared credit pool, or a metered feature's ID. |
| `amount` | Credits, or the feature's units, above 0. |
| `expiresAfter` | `{ "days": n }` or `{ "months": n }` from the redemption. Left out: they never expire. |

The customer redeems a grant code [in your app](#in-your-app): the redemption is `active` at once, with what it gave in `grants`. The same checks and limits apply as for coupons: one use per customer of a reward, `maxRedemptions` across customers, `firstTimeOnly` and the rest.

Each item is its own grant, starting at the customer's now (their test clock's time on a test clock). Credits join the customer's balance and send `credits.granted` with `source: "reward"` and the `rewardId`. Granted units add to the feature's allowance. Both are spent with the customer's other grants in the usual [spend order](https://docs.integrateledger.com/concepts/balances.md#spend-order-for-credits): whatever would be lost soonest first, and among credits that never expire, a reward's before credit packs and adjustments, so refunding a pack takes back its own credits. Changing a grant's items changes what later redemptions get; grants already given stay with the customer, through archiving the reward and through refunds.

## Promotion codes

A code is unique among your active codes.

[Add a code through the API](https://docs.integrateledger.com/api-reference/rewards/create-promotion-code.md) for one deal, with `customerId` to limit it to that customer. A catalog push leaves API codes alone. [Deactivate a code](https://docs.integrateledger.com/api-reference/rewards/deactivate-promotion-code.md) to stop new redemptions.

Before a code is used, Ledger checks it exists and is active, hasn't expired, is for this customer if it names one, meets `firstTimeOnly`, and hasn't been redeemed by this customer before (or, with `oncePerCustomer`, any code of the reward). Each use counts against the code's and the reward's `maxRedemptions` the moment it's redeemed.

## Redeem a code

A customer gets a coupon in one of three ways.

### In your app

Send the code from a field in your app to [`POST /v1/customers/redemptions`](https://docs.integrateledger.com/api-reference/customers/redeem-promotion-code):

```typescript TypeScript theme={"system"}
const redemption = await billing.redeemPromotionCode({
	customerId: "user_123",
	body: { code: "LAUNCH20" },
});
```

```python Python theme={"system"}
redemption = billing.redeem_promotion_code(customer_id="user_123", code="LAUNCH20")
```

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

On a paid plan the coupon covers, Ledger puts it on the customer's Stripe subscription and it discounts from the next invoice (`appliesFrom: "next_invoice"`). Otherwise it waits for their next attach of a plan it covers (`appliesFrom: "next_attach"`), and that attach applies it without a code. An `Idempotency-Key` header replays the first redemption.

### On attach

Send the code as `promotionCode` with [attach](https://docs.integrateledger.com/api-reference/billing/attach.md#promotion-codes). Checkout opens with the discount on, or a saved card is charged the discounted price. A lifetime plan with a code always goes through Checkout.

```typescript TypeScript theme={"system"}
const result = await billing.attach({
	customerId: "user_123",
	planId: "pro",
	promotionCode: "LAUNCH20",
});
```

```python Python theme={"system"}
result = billing.attach(
    customer_id="user_123",
    plan_id="pro",
    promotion_code="LAUNCH20",
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/attach \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "planId": "pro", "promotionCode": "LAUNCH20"}'
```

An abandoned Checkout gives the code's use back.

### At Checkout

Send `allowPromotionCodes: true` with attach to show Stripe's promotion code field in Checkout, for plans (lifetime ones too) and credit packs.

```typescript TypeScript theme={"system"}
const result = await billing.attach({
	customerId: "user_123",
	planId: "pro",
	allowPromotionCodes: true,
});
```

```python Python theme={"system"}
result = billing.attach(customer_id="user_123", plan_id="pro", allow_promotion_codes=True)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/attach \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "planId": "pro", "allowPromotionCodes": true}'
```

* Any code active in your Stripe account works: your Ledger codes, on the plans they cover, and codes you made in the Dashboard.
* A Ledger code typed in Checkout is recorded and counted from Stripe's events, like any other redemption.
* A paid plan always goes through Checkout with the field on, even for a customer with a saved card, so they have somewhere to type the code.
* A pack or lifetime plan bought with a code that takes 100% off completes without a payment and still grants in full.
* The field applies when Checkout opens. Repeating an attach while its Checkout is open returns the same session.

## One discount per bill

A subscription carries one Ledger coupon at a time. A second one waits as `queued` and goes on when the first ends, so three "1 month free" coupons give three months in a row. Coupons you add in Stripe stay on the subscription beside Ledger's.

Ledger puts redeemed coupons on subscriptions in the background, and waits while an upgrade or downgrade is in flight. A coupon attached with a [trial](https://docs.integrateledger.com/pricing/trials.md) waits through it: Ledger puts it on the subscription in the trial's last two days, so a coupon for 2 months discounts the first two paid invoices. A `once` coupon goes on the next renewal invoice rather than the subscription, so it discounts the renewal even when another invoice comes first, such as a seat change charged now.

## The customer's discounts

[`GET /v1/customers`](https://docs.integrateledger.com/api-reference/customers/get) returns the customer's pending, queued and active Ledger coupons as `discounts`, each with its `status`, `appliesFrom`, `discount` and `endsAt`. [List a customer's redemptions](https://docs.integrateledger.com/api-reference/customers/redemptions.md) returns every one, with `endedReason` once it ends: `duration`, `subscription_ended` or `removed`. `discounts` holds Ledger's coupons; coupons added in Stripe live on the Stripe subscription, and `nextChargeCents` shows the list price.

Each redemption sends a [`reward.redeemed` webhook](https://docs.integrateledger.com/webhooks) with `redemptionId`, `customerId`, `rewardId` and `status`.

## Coupons added in Stripe

A coupon you add in Stripe, on the subscription or on an invoice before it's finalized, works the same way: it lowers the price, never the grants. Ledger checks the plan's line at your full price before the discount, and [tax](https://docs.integrateledger.com/billing/tax.md) is calculated on the discounted amount. A Dashboard coupon for every product discounts overage too.

## What a discount changes

| Changes | Stays the same |
| - | - |
| The price the customer pays, on Checkout, renewals and upgrade invoices | The credits, metered allowances, limits and features granted |
| The tax charged, which is on the discounted amount | Overage, for Ledger coupons and Stripe coupons limited to the plan's product |
| A prorated downgrade refund, which is on what was paid | |

## Plan changes

A discount stays on the subscription through [upgrades and downgrades](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md#discounts), with its end date unchanged. It discounts the new plan when its coupon covers it; otherwise it stays on the bill and covers again on a move back within its duration. A period-end downgrade keeps the discount on both phases of its schedule.

The [upgrade](https://docs.integrateledger.com/api-reference/billing/upgrade.md) and [downgrade](https://docs.integrateledger.com/api-reference/billing/downgrade.md) replies say what happened to a Ledger coupon in `discount`: `{ rewardId, covered, endsAt }`, or `null` when the bill has none.

## Refunds

A prorated downgrade refund is on what the customer paid. A customer on 50% off gets half the list-price refund, never more than they paid.

## Errors

| Status | `error` | Meaning |
| - | - | - |
| `404` | `promotion_code_not_found` | No active code with this string. |
| `409` | `promotion_code_expired` | The code's `expiresAt` or the reward's `redeemBy` has passed. |
| `409` | `promotion_code_not_for_customer` | The code is limited to another customer. |
| `409` | `promotion_code_first_time_only` | The code is `firstTimeOnly` and the customer has paid for a plan or pack. |
| `409` | `promotion_code_already_redeemed` | The customer already redeemed this code, or the reward with `oncePerCustomer`. |
| `409` | `promotion_code_exhausted` | The code or the reward has reached `maxRedemptions`. |
| `409` | `reward_not_synced` | Attach with a code whose coupon is still syncing to Stripe. Retry once the reward shows `synced`. |
| `422` | `promotion_code_not_for_plan` | Attach with a code that doesn't cover the plan. |
| `422` | `promotion_code_not_a_discount` | Attach with a grant code. Redeem it in your app instead. |
| `409` | `promotion_code_taken` | Writing a reward: a code is already active, in another reward or in your Stripe account. |
| `400` | `invalid_request` | Attach sent both `promotionCode` and `allowPromotionCodes`. |

## Rules

* A coupon covers up to 100 Stripe Products: each paid plan it names, plus their seat Products. One that would cover more syncs as `failed` with `stripeSyncError: "too_many_products"`; name the plans it's for in `planIds`.
* A code must be unique across your Ledger rewards and your Stripe account; a code already active in the Stripe Dashboard stops the reward syncing (`promotion_code_taken`) until you deactivate one of the two.
* Send `promotionCode` or `allowPromotionCodes` on an attach, not both.
* Overage bills under its own `<Plan> – overage` product: a Ledger coupon, or a Stripe coupon limited to the plan's product, leaves it at full price; a Stripe coupon for every product discounts it too.
* A reward is a `coupon` or a `grant` and keeps its kind; give the other kind a new ID.
* Grant codes give credits and metered units, and are redeemed through `POST /v1/customers/redemptions`; attach and Checkout take coupon codes (`422 promotion_code_not_a_discount`).
* Granted metered units count while the customer's plan or an add-on includes the feature, even with 0 included (`feature_not_in_plan` otherwise).
* A `promotionCode` on attach covers the plan (`422 promotion_code_not_for_plan`).
* Discount with coupons, not Stripe customer balances: an invoice paid from a balance grants nothing, and attach answers `409 stripe_customer_balance` for a customer with one. See [Renewals and lifecycle](https://docs.integrateledger.com/billing/renewals-and-lifecycle.md#coupons).


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