How to Send a Viber Message with Node.js

Published April 22, 2020 by Garann Means
Categories:

If you or your customers are in a country where Viber is the default messaging platform, you’ll want your organization to be able to communicate using the service too. You might perform core work via text message or just want to send out notifications. Whatever the complexity, you can add Viber communication to your Node.js app. To see how it works without applying for a Viber business profile, you can use Vonage’s Viber sandbox.

Prerequisites

For this example, you’ll use the lightweight axios client to make a POST request. You get access to everything else you need from the Messages API, via the sandbox. So to start, you only need:

Set up Your Sandbox

If you haven’t already, navigate to Messages and Dispatch > Sandbox in your dashboard to set up your sandbox. The quickest way is to take your device with Viber installed and center the camera on the QR code supplied. Sending the custom message generated will whitelist your number. If the QR code doesn’t work with your setup, you can also join the whitelist via email.

Set up Axios

You can use axios pretty much out of the box, but you will need to install it. Create a file called app.js in a new directory or a directory you use for experiments. In the same directory, install axios:

Begin your code in app.js by creating an axios client:

Provide Your Data

The data you need to provide is all visible in the cURL command in your dashboard. You’ll need a username and password, the Viber ID to send the message from, and the whitelisted number to send the message to.

You can copy the username and password from the masked value following the -u flag. You may recognize them as your API key and secret, and you can also copy them from the Getting Started page in the dashboard. The from ID is also shown in the cURL command, and the to number is your own whitelisted number:

So you’re not just sending static text, you can append the day of the week to your message. Create a few variables to store it for use in your data. Or, even better, substitute some dynamic data from your own use case or application:

Make the Request

Axios will provide the default settings for your POST request if you supply the URL and data. The data object is the same data from the cURL command in the dashboard. If you want to send a test message that’s more relevant to your use case, you can switch it up inline. You can also provide an options object, which will hold your authorization credentials.

Once the request completes, the next callback will receive the response. From that you can check the status code and any data that came back. In this case, you should get a message UUID. You can also catch any errors that might occur and log them to the console:

Try It Out

From the directory where you created your file, run:

You should receive your test message in Viber on the device you whitelisted. To see the whole example together, you can check out the code on Github.

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at [email protected].