Skip to content
Loyvo

Get started

SDKs

Official libraries for Node.js and PHP. One client covers SMS, Loyalty and Wallet cards with your Loyvo API key, with typed errors and safe retries built in.

Install

LanguagePackageNeeds
Node.jsloyvoNode 18+. No dependencies; import or require; TypeScript types included.
PHPloyvo/loyvoPHP 7.4+ with curl. No dependencies, so it is safe inside WordPress plugins.

Source, changelog and issues: github.com/Loyvo/loyvo-sdks. Packages on npm and Packagist.

Using another language? Every endpoint in the API reference has cURL and Python samples too, and the APIs are plain HTTPS and JSON.

Install
npm install loyvo

Set up a client

Create a key in app.loyvo.lk → Settings → API keys and give it to the client. Keep it on your server: never in a website, mobile app or anything a customer can download.

whoami() shows which organisation the key belongs to, whether it is live or test, and what it may do. It is a handy first call to check your setup.

Test keys (lvo_test_…) work with SMS only: messages are never delivered and use no credits. Loyalty and Wallet need a live key.

Create a client
import Loyvo from "loyvo";

// Reads LOYVO_API_KEY when apiKey is left out.
const loyvo = new Loyvo({ apiKey: process.env.LOYVO_API_KEY });

const me = await loyvo.whoami();
console.log(me.org.name, me.env, me.scopes);

Send SMS

sms.send sends one message to one number or up to 100. sms.bulk sends a campaign of up to 10,000 recipients, filling {{placeholders}} from each recipient's vars. Both accept scheduleAt to send later.

Every send gets an Idempotency-Key automatically, so a retry never sends twice. Pass your own idempotencyKey (for example your order number) to make your own retries safe as well.

Then check delivery with sms.get or campaigns.get, list sender IDs with senderIds.list, and check credits with balance().

SMS
// One message (to one number or up to 100)
const sent = await loyvo.sms.send({
  from: "LOYVO",
  to: "0771234567",
  message: "Your order A-101 is ready for pickup.",
});

// Personalised campaign (up to 10,000 recipients)
await loyvo.sms.bulk({
  from: "LOYVO",
  message: "Hi {{name}}, your code is {{code}}",
  recipients: [
    { to: "0771234567", vars: { name: "Kamal", code: "K10" } },
    { to: "0712345678", vars: { name: "Nimal", code: "N10" } },
  ],
});

const { credits } = await loyvo.balance();

Points at the point of sale

pos awards and redeems points and keeps the customer's Apple or Google Wallet card up to date in the same call. Identify the customer by phone, or by the barcodeValue scanned from their card.

  • pos.resolve: is this customer enrolled, and do they have a card?
  • pos.enroll: enrol them and issue a card; send them the publicInstallUrl.
  • pos.transaction: record a purchase and award points.
  • pos.redemptions: initiate (Loyvo texts a PIN), confirm, resendPin, cancel.

Calls that change points are never retried after a server or network error, because a retry could award points twice. If one fails that way, check with pos.resolve before trying again.

The SDKs also cover wallet cards (passes) and Loyalty settings, branches and records (loyalty). See the API reference for every call: its Node.js and PHP tabs show the SDK.

POS
// Award points for a purchase (also updates the wallet card)
const t = await loyvo.pos.transaction({
  phone: "0771234567",
  amount: 2500,
  invoiceId: "INV-1055",
});
console.log(t.points_awarded, t.new_points_balance);

// Redeem: Loyvo texts the customer a PIN, then confirm it
const { redemption_id } = await loyvo.pos.redemptions.initiate({ phone: "0771234567", points: 100 });
await loyvo.pos.redemptions.confirm({ phone: "0771234567", redemptionId: redemption_id, pin: "4821" });

Errors and retries

Every failure is a LoyvoError (PHP: LoyvoException) with the HTTP status, a stable code, the field an input error is about, and the requestId to quote to support.

ErrorStatusWhen
ValidationError400 / 422An input is invalid (see field).
AuthenticationError401Missing, invalid or revoked key.
PaymentRequiredError402Not enough SMS credits, or the plan has ended.
PermissionDeniedError403The key may not do this, e.g. test_key_not_supported.
NotFoundError404No such resource.
RateLimitError429Too many requests (see retryAfter).
ServerError5xxSomething went wrong at Loyvo.
ConnectionErrornoneLoyvo could not be reached (timeout or network).

The SDKs retry rate limits (honouring Retry-After) and, when it is safe, server and network errors: two retries by default (maxRetries). Requests time out after 30 seconds (timeout).

Handle errors
import { LoyvoError, PaymentRequiredError, ValidationError } from "loyvo";

try {
  await loyvo.sms.send({ from: "LOYVO", to: "0771234567", message: "Hi" });
} catch (err) {
  if (err instanceof PaymentRequiredError) {
    // top up SMS credits
  } else if (err instanceof ValidationError) {
    console.log(`Fix ${err.field}: ${err.message}`);
  } else if (err instanceof LoyvoError) {
    console.log(err.code, err.requestId);
  } else {
    throw err;
  }
}