Creates a loyalty account for one of a site's contacts.
The createAccount()
function returns a Promise that resolves to the new loyalty account when it is created.
To create a new loyalty account, the customer must first be a site contact with a contact ID. The site must also have an active loyalty program before loyalty accounts can be created.
Note: Only visitors with Manage Loyalty permissions can create a loyalty account.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function createAccount(contactId: string): Promise<CreateAccountResponse>;
Contact ID for a Wix site contact. See the Contacts API to learn more.
import { accounts } from "wix-loyalty.v2";
// Sample contactId value: 'ff61204b-b19a-5cc8-823b-7eed8ae5fc28'
export async function myCreateAccountFunction() {
try {
const newLoyaltyAccount = await accounts.createAccount(contactId);
const accountId = newLoyaltyAccount.account._id;
console.log(
"Success! The account ID for this new loyalty account is: ",
accountId,
);
return newLoyaltyAccount;
} catch (error) {
console.error(error);
}
}
/* Promise resolves to:
* {
* "account": {
* "_id": "1e3f99cf-1e7f-4cba-8777-f081549f49cd",
* "contactId": "ff61204b-b19a-5cc8-823b-7eed8ae5fc28",
* "points": {
* "balance": 0,
* "earned": 0,
* "adjusted": 0,
* "redeemed": 0
* },
* "rewardAvailable": false,
* "_createdDate": "2022-11-09T12:49:25.408Z",
* "_updatedDate": "2022-11-09T12:49:25.408Z",
* "revision": "1"
* }
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.