Send verification code

When you have collected a user's phone number, start the verification process by sending a verify request to the Verify API.

The Verify API returns a request_id. Use this to identify a specific verification request in subsequent calls to the API, such as when making a check request to see if the user provided the correct code.

Replace the following variables in the sample code with your own values:

Name Description
VONAGE_API_KEY Your Vonage API key
VONAGE_API_SECRET Your Vonage API secret
RECIPIENT_NUMBER The phone number to verify
BRAND_NAME Included in the message to explain who is confirming the phone number

Write the code

Add the following to send-verification-code.sh:

Copy to Clipboard
curl -X GET "https://api.nexmo.com/verify/json?&api_key=$NEXMO_API_KEY&api_secret=$NEXMO_API_SECRET&number=$RECIPIENT_NUMBER&brand=AcmeInc"

View full source

Run your code

Save this file to your machine and run it:

sh send-verification-code.sh

Prerequisites

Install dependencies

npm install @vonage/server-sdk
Initialize your dependencies

Create a file named request.js and add the following code:

Copy to Clipboard
const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET
});

View full source

Write the code

Add the following to request.js:

Copy to Clipboard
vonage.verify.request({
  number: RECIPIENT_NUMBER,
  brand: BRAND_NAME
}, (err, result) => {
  if (err) {
    console.error(err);
  } else {
    const verifyRequestId = result.request_id;
    console.log('request_id', verifyRequestId);
  }
});

View full source

Run your code

Save this file to your machine and run it:

node request.js

Prerequisites

Install dependencies

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'
Initialize your dependencies

Create a class named StartVerification and add the following code to the main method:

Copy to Clipboard
VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();

View full source

Write the code

Add the following to the main method of the StartVerification class:

Copy to Clipboard
VerifyResponse response = client.getVerifyClient().verify(RECIPIENT_NUMBER, BRAND_NAME);

if (response.getStatus() == VerifyStatus.OK) {
    System.out.printf("RequestID: %s", response.getRequestId());
} else {
    System.out.printf("ERROR! %s: %s", response.getStatus(), response.getErrorText());
}

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.verify with the package containing StartVerification:

gradle run -Pmain=com.vonage.quickstart.verify.StartVerification

Prerequisites

Install dependencies

Install-Package Nexmo.Csharp.Client
Import dependencies

Create a file named SendVerificationRequest.cs and add the following code:

Copy to Clipboard
using Vonage.Verify;
using Vonage;
using Vonage.Request;

View full source

Initialize your dependencies

Add the following to SendVerificationRequest.cs:

Copy to Clipboard
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to SendVerificationRequest.cs:

Copy to Clipboard
var request = new VerifyRequest() { Brand = BRAND_NAME, Number = RECIPIENT_NUMBER };
var response = client.VerifyClient.VerifyRequest(request);

View full source

Prerequisites

Install dependencies

composer require vonage/client
Initialize your dependencies

Create a file named request.php and add the following code:

Copy to Clipboard
$basic  = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client(new \Vonage\Client\Credentials\Container($basic));

View full source

Write the code

Add the following to request.php:

Copy to Clipboard
$request = new \Vonage\Verify\Request(NUMBER, BRAND_NAME);
$response = $client->verify()->start($request);

echo "Started verification, `request_id` is " . $response->getRequestId();

View full source

Run your code

Save this file to your machine and run it:

php request.php

Prerequisites

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named request.py and add the following code:

Copy to Clipboard
import vonage

client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
verify = vonage.Verify(client)

View full source

Write the code

Add the following to request.py:

Copy to Clipboard
response = verify.start_verification(number=RECIPIENT_NUMBER, brand="AcmeInc")

if response["status"] == "0":
    print("Started verification request_id is %s" % (response["request_id"]))
else:
    print("Error: %s" % response["error_text"])

View full source

Run your code

Save this file to your machine and run it:

python request.py

Prerequisites

Install dependencies

gem install vonage
Initialize your dependencies

Create a file named request.rb and add the following code:

Copy to Clipboard
client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

View full source

Write the code

Add the following to request.rb:

Copy to Clipboard
response = client.verify.request(
  number: TO_NUMBER,
  brand: 'AcmeInc'
)

if response
  # display the Verify `request_id`
  puts response.request_id
end

View full source

Run your code

Save this file to your machine and run it:

ruby request.rb

Note: If you receive the error code 15: The destination number is not in a supported network, the target network might have been blocked by the platform's anti-fraud system. See Velocity Rules.