How to send transactional Viber Business Message

Steps to Send a Transactional Message to Viber Users via Phone Number

  1. Prerequisites:

    • Viber Business API Access: You need to register with Viber Business and obtain an API key/token.

    • Recipient's Phone Number: You need the recipient's Viber-registered phone number in international format (e.g., +65xxxxxxxx).

    • Node.js: Ensure that you have Node.js installed and set up for making API calls.

Full Example Code to Send a Transactional Message

const axios = require('axios');

// Replace with your Viber Business API token
const VIBER_BUSINESS_API_TOKEN = 'YOUR_VIBER_BUSINESS_API_TOKEN';

// Function to send a transactional message via Viber using the recipient's phone number
const sendTransactionalMessage = async (phoneNumber, messageText) => {
  try {
    const messagePayload = {
      "receiver": phoneNumber,  // The recipient's phone number in international format (e.g., +65xxxxxxx)
      "min_api_version": 1,  // Minimum API version required for this message
      "sender": {
        "name": "YourBusinessName",  // The sender's name as it will appear on the user's phone
        "avatar": "https://example.com/avatar.jpg"  // Optional: URL to the avatar image for the sender
      },
      "type": "text",  // The type of message (in this case, it's a text message)
      "text": messageText,  // The transactional message content (e.g., order confirmation)
      "tracking_data": "tracking_data_example",  // Optional tracking data for your system
    };

    // Send POST request to Viber Business API endpoint to send the message
    const response = await axios.post('https://chatapi.viber.com/pa/send_message', messagePayload, {
      headers: {
        'X-Viber-Auth-Token': VIBER_BUSINESS_API_TOKEN,  // Viber Business API authentication token
      }
    });

    // Log success response
    console.log('Message sent successfully:', response.data);
  } catch (error) {
    // Log any errors encountered during the API request
    console.error('Error sending message:', error.response ? error.response.data : error.message);
  }
};

// Example usage:
// Replace with the recipient's actual Viber-registered phone number in international format
const recipientPhoneNumber = '+65xxxxxxxx';  // Example for Singapore (+65)
// Replace with your actual message content
const transactionalMessageText = 'Your order #12345 has been confirmed! Track it at https://tracking-link.com';

// Call the function to send the transactional message
sendTransactionalMessage(recipientPhoneNumber, transactionalMessageText);

Explanation of the Code:

  1. API Token:

    • Replace YOUR_VIBER_BUSINESS_API_TOKEN with your Viber Business API token. This token is provided after registering for Viber's Business Messaging service.

  2. Phone Number:

    • Replace +65xxxxxxxx with the actual recipient’s Viber-registered phone number in international format (e.g., +65xxxxxxx for Singapore). The phone number must be registered with Viber.

  3. Message Text:

    • Modify the content of the transactionalMessageText variable to whatever transactional message you want to send (e.g., order confirmation, delivery update, etc.).

  4. API Request:

    • The code uses axios to send a POST request to the https://chatapi.viber.com/pa/send_message endpoint, with the message payload that includes the recipient’s phone number, message type, and content.

  5. Sender Information:

    • The sender object contains the business name and an optional avatar URL. These will be shown to the recipient when they receive the message.

  6. Optional Tracking Data:

    • The tracking_data field is optional and can be used to pass any data that you want to track for the message, such as an internal identifier.

Testing the Application:

  1. Ensure you have Node.js installed.

  2. Install the axios package if you haven't already:

npm install axios
  1. Run the script using Node.js:

node index.js

Result:

If everything is set up correctly, the transactional message (e.g., order confirmation) will be delivered to the specified Viber user, and you will see a success message logged in the console.

Important Considerations:

  1. Business Messaging API Access:

    • You need to work with a Viber Business partner or apply through Viber Business Messaging Partner to get API access.

    • Depending on your business case, you may need to comply with specific Viber guidelines and have approval for sending transactional messages.

  2. Rate Limits:

    • Ensure you are aware of the rate limits imposed by Viber to avoid hitting limits or being blocked from sending messages.

  3. Pricing:

    • Viber charges for sending messages, especially promotional ones. The cost may vary depending on the country and the message type (e.g., transactional vs. promotional).

By following this code, you'll be able to send transactional Viber messages directly to customers using their phone numbers.

Last updated