# Collect Google Reviews

### Set Up Wix Automation

{% embed url="<https://youtu.be/f6opV5OLwXo>" %}

1. Go to **Automations** in the Wix Dashboard and then click on **Create Automation.**
2. Select the automation you want to trigger (in this case, **Order Placed)**
3. Next, add a 3-day delay by clicking the **plus icon** and selecting the ***Delay*** option.
4. Add a Velo Code Action and add a .js file containing the following code(depending upon the type of communication: WhatsApp or SMS):

{% tabs %}
{% tab title="WhatApp" %}
{% code overflow="wrap" lineNumbers="true" %}

```javascript
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');

    // 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
};

```

{% endcode %}
{% endtab %}

{% tab title="SMS " %}
{% code overflow="wrap" lineNumbers="true" %}

```javascript
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');
    // 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: "sms",
      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
};



```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:🔧 Changes to Make After Copy-Pasting the Code**

1. **Update the Secret Name**

In this line:

```js
const bearerToken = await elevatedGetSecretValue('MERCURI_MESSAGING_API_KEY');
```

replace `'MERCURI_MESSAGING_API_KEY'` with the **Secret Name** you created in **Wix Secret Manager**.<br>

2. **Add Your Phone Number ID**

* Replace `"xxxxxxxxxxxxxxxxxxx"`  corresponding to **phoneNumberId** in the code with the **Phone Number ID** you copied from the Mercuri Dashboard.

3. **Add Your Template ID**

* Replace `"xxxxxxxxxxxxxxxxxxx"`   corresponding to **templateId** with the **Template ID** you copied from the Mercuri Dashboard.

[***Please click on this link to view the instructions for locating the Phone Number ID and Template ID.***](/features/introduction-mercuri-api/faqs.md)

The parameters array will automatically map values from the Wix automation payload to your template fields, such as firstName, lastName, and recipient.
{% endhint %}

### &#x20;Final Steps

1. After uploading the Velo code, save your changes.
2. Activate the automation in Wix.
3. 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mercuri.cx/features/introduction-mercuri-api/use-cases-how-to-use-the-mercuri-api-in-wix-velo-+-automations/collect-google-reviews.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
