Skip to content
Longplay
Docs
Browse docs / Coupons, rewards and referrals

Coupons, rewards and referrals

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

Markdown
API v1Updated

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.tsTypeScript
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 };}

Grant a coupon from your backend#

grant.tsTypeScript
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).

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.

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.

Related operations#