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
| Language | Package | Needs |
|---|---|---|
| Node.js | loyvo | Node 18+. No dependencies; import or require; TypeScript types included. |
| PHP | loyvo/loyvo | PHP 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.
npm install loyvoSet 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.
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().
// 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 thepublicInstallUrl.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.
// 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.
| Error | Status | When |
|---|---|---|
| ValidationError | 400 / 422 | An input is invalid (see field). |
| AuthenticationError | 401 | Missing, invalid or revoked key. |
| PaymentRequiredError | 402 | Not enough SMS credits, or the plan has ended. |
| PermissionDeniedError | 403 | The key may not do this, e.g. test_key_not_supported. |
| NotFoundError | 404 | No such resource. |
| RateLimitError | 429 | Too many requests (see retryAfter). |
| ServerError | 5xx | Something went wrong at Loyvo. |
| ConnectionError | none | Loyvo 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).
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;
}
}