Developers
TypeScript

Leal TypeScript SDK

Typed client for Node, Deno, Bun and the browser. Enrol customers, add stamps, redeem rewards and read a card's Apple Wallet and Google Wallet links, without writing a single HTTP request.

Package
@lealhq/leal
Registry
npm
Requires
Node 18 or newer, Deno, Bun, or any modern browser.
Licence
MIT

Install

npm install @lealhq/leal

Your first call

Create a token at app.getleal.com/api_tokens, keep it in an environment variable, and hand it to the client once. Every request after that carries it for you.

import { LealClient } from "@lealhq/leal";

const leal = new LealClient({ token: process.env.LEAL_API_TOKEN });

const stores = await leal.stores.list();
console.log(stores[0].name);

A token reaches every store its owner belongs to. There is no separate sandbox yet, so build against a store you do not mind changing.

Common tasks

Enrol a customer

Create the customer and put them straight onto a card. The response carries the Apple Wallet and Google Wallet links, so you can send them yourself.

const customer = await leal.customers.create({
  account_id: storeId,
  customer: { first_name: "Ada", email: "ada@example.com" },
  card_id: cardId,
});

const card = customer.customer_cards[0];
console.log(card.apple_wallet_url, card.google_wallet_url);

Add a stamp

The call most integrations need. Leal records the stamp, updates the wallet pass and sends the notification.

const card = await leal.customerCards.stamp({
  account_id: storeId,
  customer_id: customerId,
  id: customerCardId,
  stamps: 1,
});

console.log(`${card.stamps_remaining} stamps to the next reward`);

Redeem a reward

Spend the stamps a customer has earned. Leal checks the reward is available before recording the redemption.

await leal.customerCards.redeem({
  account_id: storeId,
  customer_id: customerId,
  id: customerCardId,
  reward_id: rewardId,
});

Handle a failure

Every failure is a typed error carrying the status code, so you can tell a missing record from a rate limit without reading strings.

import { LealError } from "@lealhq/leal";

try {
  await leal.customerCards.stamp({ account_id: storeId, customer_id: customerId, id: customerCardId, stamps: 1 });
} catch (error) {
  if (error instanceof LealError && error.statusCode === 429) {
    // Wait for the number of seconds in the Retry-After header, then try again.
  }
  throw error;
}

Every endpoint is available, not just these. The TypeScript repository lists them all, and the API reference documents each field.

Rate limits and retries

Leal allows 300 requests per minute per API token. The client retries a failed request on its own, backing off between attempts, so a brief wobble does not need handling in your code. Go past the limit and you get a 429 carrying Retry-After in seconds. If you need a higher limit, ask us.

Other languages

The same API, the same methods, in eight other languages.