# Coupons, rewards and referrals

Redeem, claim and grant coupons, create referral codes and redeem rewards.

Last updated: 2026-11-06

[Web version](https://www.longplayai.com/docs/guides/offers)

<a id="redeem"></a>

## Redeem a coupon code

Call `redeemCoupon` in the browser after the user enters a code. When unavailable it returns a `reason` such as `outside_window`, `limit_reached` or `sold_out`.

**redeem.ts**

```ts
import { createLongplay } from "@tinkerlandai/longplay-sdk";

const longplay = createLongplay({
  baseUrl: "https://api.longplayai.com",
  scope: { projectId: "<APP_UUID>", environment: "sandbox" },
});

type UUID = ReturnType<typeof crypto.randomUUID>;

export async function redeem(customerToken: string, code: string, requestId: UUID) {
  longplay.identify(customerToken);
  const { offer, reason } = await longplay.redeemCoupon(code, requestId);
  if (!offer) return { ok: false as const, reason };
  const offers = await longplay.listOffers();
  return { ok: true as const, offers };
}
```

<a id="grant"></a>

## Grant a coupon from your backend

**grant.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function grantWelcomeCoupon(customerId: string, requestId: string) {
  const { offer, reason } = await longplay.grantOffer({
    customerId,
    couponId: process.env.LONGPLAY_WELCOME_COUPON_ID!,
    requestId,
  });
  return offer ?? reason;
}
```

Requires `offers:grant`. For self-claim, the browser calls `claimCoupon(couponId, requestId)`.

<a id="apply"></a>

## Apply an offer to a purchase

- New purchases: pass the claimed `offerId` to `previewAttach`.
- Existing subscriptions: call `setBillingCoupons`.
- Coupons used in your own checkout: report `coupon.redeemed` with `recordMerchantEvent` from your backend.

<a id="referrals"></a>

## Referral codes and rewards

The browser calls `createReferralCode({ programId, requestId })` to create a referral code; the referred user calls `redeemReward({ code, requestId })` to redeem the reward.

<a id="operations"></a>

## Related operations

- **Coupons, rewards and referrals (8)**:  [redeemCoupon](https://www.longplayai.com/docs/api/redeem-coupon.md); [listOffers](https://www.longplayai.com/docs/api/list-offers.md); [grantOffer](https://www.longplayai.com/docs/api/grant-offer.md); [grantModelBenefit](https://www.longplayai.com/docs/api/grant-model-benefit.md); [readModelBenefit](https://www.longplayai.com/docs/api/read-model-benefit.md); [redeemReward](https://www.longplayai.com/docs/api/redeem-reward.md); [createReferralCode](https://www.longplayai.com/docs/api/create-referral-code.md); [claimRewardProduct](https://www.longplayai.com/docs/api/claim-reward-product.md)

- [Paths, credentials and scopes in All operations](https://www.longplayai.com/docs/api/operations.md#offers)
