Voice Verify API - Tutorial: Send a voice message with verification code with Telesign Node.js SDK
NOTE:
To add this product to your account, contact a Telesign expert. This product is available for full-service accounts only.
This tutorial teaches you how to use Telesign Voice Verify API with a Node.js SDK, by walking you step-by-step through the creation of an integration where you send your own verification code to an end-user. Skip to the end of this page to see the full, completed sample code.
Before you begin
Make sure you have the following before you start:
- Authentication credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
- Testing device: A mobile phone on which you can receive a call.
NOTE:
This tutorial uses the following:
- MacOS
- Node v16.9.0
Please modify accordingly if your developer environment differs from these details.
Set up your project
-
Follow the Telesign Full-service Node.js SDK install instructions here on GitHub incorporating these details:
- Use
voice_verify
as the project name.
- Use
-
Create a new file called
send_voice.js
and open it in your editor.
touch send_voice.js
You should end up in the top-level directory for your project in the Terminal.
Send a voice message with a verification code
- Follow the steps below or copy the sample code into the file and update it accordingly.
- Add the imports below. The
telesignenterprisesdk
import refers to the Telesign Full-service SDK.
const TelesignSDK = require('telesignenterprisesdk');
- Define variables to store your authentication credentials. For testing purposes, you can just overwrite the default values below or use environment variables.
const customerId = process.env.CUSTOMER_ID || "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = process.env.API_KEY || "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
- Define a variable to hold the phone number you want to send a voice message to. This should be a string with no spaces or special characters. Use the complete number, including the country code. For example:
16505551212
.
const phoneNumber = process.env.PHONE_NUMBER || "11234567890";
- Generate a verification code.
const optionalParams = {verify_code: "32658"};
- Create a VerifyClient object and pass it your
customerId
andapiKey
.
const client = new TeleSignSDK(customerId, apiKey);
- Create a callback handler for the voice message you will eventually send and a method called inside voiceCallback that will handle user input.
// Callback handler for voice
function voiceCallback(error, responseBody) {
if (error === null) {
console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
` => code: ${responseBody['status']['code']}` +
`, description: ${responseBody['status']['description']}`);
// Ask for user input
prompt('Enter the verification code received:\n', function (input) {
if (input === optionalParams['verify_code']) {
console.log('Your code is correct.');
} else {
console.log('Your code is incorrect. input: ' + input + ", code: " + optionalParams['verify_code']);
}
process.exit();
});
} else {
console.error("Unable to send voice call. " + error);
}
}
// Method to handle user input
function prompt(question, callback) {
const stdin = process.stdin,
stdout = process.stdout;
stdin.resume();
stdout.write(question);
stdin.once('data', function (data) {
callback(data.toString().trim());
});
}
- Send the voice message here, and the callback and user input handler methods you defined in the last step are passed in through the voiceCallback method.
// Send Voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);
Test your integration
- Switch from your editor to the terminal and run:
node send_voice.js
If all goes well, you should send a voice message containing a verification code to the phone number you selected. If you are testing with your phone, you should be able to enter the verification code in response to a prompt from your terminal. The sample will then check your verification code to see if it matches the verification code that was sent and report on the results.
Send a Voice Message with Verification Code in a Different Language
This section explains how to send a custom voice message in a different language using Voice Verify API. A sample is provided here if you want to cut and paste. Note that the only difference between this code sample and the previous code sample in this tutorial is that you use the tts_message
(text-to-speech) parameter to enter a custom message, and the language
parameter to specify the language. For a list of language codes, refer to - Voice Verify API - Supported languages for text-to-speech.
var TeleSignSDK = require('telesignenterprisesdk');
console.log("## verify.voice ##");
const customerId = process.env.CUSTOMER_ID || "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = process.env.API_KEY || "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const phoneNumber = process.env.PHONE_NUMBER || "11234567890";
const optionalParams = {
tts_message: "Votre code de vérification Widgets 'n' More est $$CODE$$.",
language: "fr-FR"
};
const client = new TeleSignSDK(customerId, apiKey);
// Callback handler for voice
function voiceCallback(error, responseBody) {
if (error === null) {
console.log(`Response for phone number: ${phoneNumber}` +
` => code: ${responseBody['status']['code']}` +
`, description: ${responseBody['status']['description']}`);
} else {
console.error("Unable to send voice. " + error);
}
}
// Send voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);
Sample code
var TeleSignSDK = require('telesignenterprisesdk');
console.log("## verify.voice ##");
const customerId = process.env.CUSTOMER_ID || "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = process.env.API_KEY || "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const phoneNumber = process.env.PHONE_NUMBER || "11234567890";
const optionalParams = {verify_code: "32658"};
const client = new TeleSignSDK(customerId, apiKey);
// Callback handler for voice
function voiceCallback(error, responseBody) {
if (error === null) {
console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
` => code: ${responseBody['status']['code']}` +
`, description: ${responseBody['status']['description']}`);
// Ask for user input
prompt('Enter the verification code received:\n', function (input) {
if (input === optionalParams['verify_code']) {
console.log('Your code is correct.');
} else {
console.log('Your code is incorrect. input: ' + input + ", code: " + optionalParams['verify_code']);
}
process.exit();
});
} else {
console.error("Unable to send voice call. " + error);
}
}
// Method to handle user input
function prompt(question, callback) {
const stdin = process.stdin,
stdout = process.stdout;
stdin.resume();
stdout.write(question);
stdin.once('data', function (data) {
callback(data.toString().trim());
});
}
// Send Voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);
Updated about 6 hours ago