How do I use the Mercuri API in Wix (Velo + Automations)?
Set Up Wix Automation
Go to Automations in the Wix Dashboard and then click on Create Automation.
Select the automation you want to trigger (e.g., Cart Abandonment)
Add a Velo Code Action and add a .js file containing the following code:
import { secrets } from 'wix-secrets-backend.v2';
import { elevate } from 'wix-auth';
import { fetch } from 'wix-fetch';
// Create elevated versions of secrets functions
const elevatedGetSecretValue = elevate(secrets.getSecretValue);
/**
* Autocomplete function declaration, do not delete
* @param {import('./__schema__.js').Payload} options
*/
export const invoke = async ({ payload }) => {
try {
// Get bearer token securely with elevation
const bearerToken = await elevatedGetSecretValue('MERCURI_MESSAGING_API_KEY');
console.log("Bearer Token",bearerToken)
// Extract contact data
const firstName = payload.contact?.name?.first || '';
const lastName = payload.contact?.name?.last || '';
const recipient = payload.contact?.phone || '';
if (!recipient) {
console.error('Recipient phone number missing.');
return {};
}
const apiPayload = {
phoneNumberId: "xxxxxxxxxxxxxxxxxxx",
channel: "whatsapp",
recipient: recipient,
message: {
type: "template",
template: {
templateId: "xxxxxxxxxxxxxxxxxxx",
parameters: [
{
firstName: firstName,
lastName: lastName
}
]
}
},
saveToInbox: true
};
// Call Mercuri API
const response = await fetch('https://api.mercuri.cx/v1/send_message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${bearerToken}`
},
body: JSON.stringify(apiPayload)
});
if (!response.ok) {
const errorMsg = await response.text();
console.error(`Mercuri API error: ${response.status} - ${errorMsg}`);
} else {
const respData = await response.json();
console.log('Message sent successfully:', respData);
}
} catch (error) {
console.error('Error in invoke:', error);
}
return {}; // Required empty return object
};
Final Steps
After uploading the Velo code, save your changes.
Activate the automation in Wix.
Optionally, test with a sample contact to ensure the messages are sent correctly.
Once these steps are completed, you have successfully set up Wix Automation to send WhatsApp messages using the Mercuri API.
Last updated