I have an automation that awards new members 50 loyalty points, but can’t figure out how to add additional points to their account with code. When I run the code nothing happens, literally. Seems to get stuck on loading the backend files (which are identical to the WIx examples, and show no errors). Nothing goes to the console log.
frontend:
import { awardLoyaltyPoints } from "backend/AwardLoyaltyPoints.web.js";
import { getLoyaltyAccountID } from "backend/GetLoyaltyPointsAccount.web.js";
$w.onReady(async function () {
let opt = {
contactId: "0af9f886-b446-44fd-9787-6ef48a368736"// Sample member's contact ID.
}
console.log("Processing order :" + opt);
let loyaltyID = await getLoyaltyAccountID(opt);
console.log("Will attempt to award 50 points to :" + loyaltyID);
let success = await AwardPoints(loyaltyID);
console.log ("Backend Award points: " + success)
});
function AwardPoints(loyalID){
let opts = {
amount: 50,
description: "Earn 50 points for booking an order",
appId: "553c79f3-5625-4f38-b14b-ef7c0d1e87df", //Wix sample
idempotencyKey: "bfdc785c-bbc6-447d-b987-220ca649a3b2" //Wix sample
}
return awardLoyaltyPoints(loyalID, opts)
}
backend:
GetLoyaltyPointsAccount.web.js
import { Permissions, webMethod } from "wix-web-module";
import { accounts } from "wix-loyalty.v2";
export const getLoyaltyAccountID = webMethod(
Permissions.Anyone,
async (options) => {
try {
const loyaltyAccount = await accounts.getAccountBySecondaryId(options);
const accountId = loyaltyAccount.account._id;
const currentRevision = loyaltyAccount.account.revision;
console.log(
"Success! This site contact's loyalty account ID is: ",
accountId,
);
return loyaltyAccount;
} catch (error) {
console.error(error);
}
},
);
AwardLoyaltyPoints.web.js
import { Permissions, webMethod } from "wix-web-module";
import { accounts } from "wix-loyalty.v2";
export const awardLoyaltyPoints = webMethod(Permissions.Anyone, async (accountId, options) => {
try {
const updatedAccount = await accounts.earnPoints(accountId, options);
const newBalance = updatedAccount.account.points.balance;
//const transactionId = updatedAccount.transaction_id;
console.log("Success! New balance is ", newBalance);
return updatedAccount;
} catch (error) {
console.error(error);
}
});
I haven’t jumped into the code, or what it’s doing yet, but notice you’re calling your backend functions to the page code as .js
files.
To expose these functions and allow them to be called from the frontend, they need to be within .web.js
files. (You’ve already got them written as web methods, which is great!)
1 Like
Yes, you were correct about the backend file names. I have updated the code in my post above and it runs, but doesn’t get very far.
GetLoyaltyPointsAccount.web.js returns a permission denied error: ‘LOYALTY.READ_ACCOUNTS required to call this endpoint.: Forbidden’
I also rechecked the contact ID to ensure that it was the contact ID of a member that already has loyalty points, and tried a few other contact IDs as well but this did not make any difference.
Taking a quick read over the docs, some methods require elevated permissions (like accounts.getAccountBySecondaryId()
)

The docs for setting up elevate are here - Elevate | Velo 
1 Like
Really wish the API docs would show a working sample of an elevated call for any function that requires it. But I did manage to figure it out and get it working, and corrected a few minor errors. I’m posting my working code here for the community. 
import { awardLoyaltyPoints } from "backend/AwardLoyaltyPoints.web.js";
import { getLoyaltyAccountID } from "backend/GetLoyaltyPointsAccount.web.js";
$w.onReady(async function () {
let opt = {
contactId: "0af9f886-b446-44fd-9787-6ef48a368736"// CairoCorrespondent ID
}
let loyaltyAccount = await getLoyaltyAccountID(opt); // CairoCorrespondent ID
const accountId = loyaltyAccount.account._id;
const currentPoints = loyaltyAccount.account.points.balance;
console.log("Loyalty account ID: " + accountId);
console.log("Current points: " + loyaltyAccount.account.points.balance);
console.log("Will attempt to award 50 points to :" + accountId);
let options = {
"amount": 50,
"description": "Earn 50 points for booking an order",
"appId": "553c79f3-5625-4f38-b14b-ef7c0d1e87df", // Wix appId for points added manually
"idempotencyKey": "bfdc785c-bbc6-447d-b987-220ca649a3b2" // Wix sample
}
const updatedAccount = await awardLoyaltyPoints(accountId, options)
const newBalance = updatedAccount.account.points.balance;
console.log ("Loyalty points, updated balance: " + newBalance)
});
GetLoyaltyPointsAccount.web.js
import { Permissions, webMethod } from "wix-web-module";
import { accounts } from "wix-loyalty.v2";
import { elevate } from "wix-auth";
export const getLoyaltyAccountID = webMethod( Permissions.Anyone,
async (options) => {
try {
const elevatedAccounts = elevate(accounts.getAccountBySecondaryId);
const loyaltyAccount = await elevatedAccounts(options);
return loyaltyAccount;
} catch (error) {
console.error(error);
}
});
AwardLoyaltyPoints.web.js
import { Permissions, webMethod } from "wix-web-module";
import { accounts } from "wix-loyalty.v2";
import { elevate } from "wix-auth";
export const awardLoyaltyPoints = webMethod( Permissions.Anyone,
async (accountId, options) => {
try {
const elevatedEarnPoints = elevate(accounts.earnPoints);
const updatedAccount = await elevatedEarnPoints(accountId, options);
return updatedAccount;
} catch (error) {
console.error(error);
}
});
1 Like