Docs Guides Coupons, rewards and referrals Coupons, rewards and referrals Redeem, claim and grant coupons, create referral codes and redeem rewards.
API v1 Updated Nov 6, 2026
On this page
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.
1 import { createLongplay } from "@tinkerlandai/longplay-sdk" ;2 3 const longplay = createLongplay ({4 baseUrl: "https://api.longplayai.com" , 5 scope: { projectId: "<APP_UUID>" , environment: "sandbox" }, 6 }); 7 8 type UUID = ReturnType <typeof crypto.randomUUID>;9 10 export async function redeem (customerToken: string, code: string, requestId: UUID ) {11 longplay.identify (customerToken); 12 const { offer, reason } = await longplay.redeemCoupon (code, requestId); 13 if (!offer) return { ok: false as const , reason }; 14 const offers = await longplay.listOffers (); 15 return { ok: true as const , offers }; 16 } Grant a coupon from your backend# 1 import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server" ;2 3 const longplay = createLongplayServer ({4 baseUrl: process.env.LONGPLAY_API_URL !, 5 apiKey: process.env.LONGPLAY_SERVER_KEY !, 6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID !, environment: "sandbox" }, 7 }); 8 9 export async function grantWelcomeCoupon (customerId: string, requestId: string) {10 const { offer, reason } = await longplay.grantOffer ({ 11 customerId, 12 couponId: process.env.LONGPLAY_WELCOME_COUPON_ID !, 13 requestId, 14 }); 15 return offer ?? reason; 16 } 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.