这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Verify API

The Verify API enables you to confirm that you can contact a user at a specific number, so that you can:

  • Reach your users at any time, by ensuring that you have their correct phone number
  • Protect against fraud and spam, by preventing one user from creating multiple accounts
  • Add an extra layer of security to help confirm a user's identity when they want to perform certain activities

How it works

Verification is a two-stage process that requires two API calls:

Verification request

#{alt_text}

  1. A user registers for your service via your app or web site and provides a phone number.

  2. To confirm that the user has access to the number that they have registered with, your application makes an API call to the Verification request endpoint.

  3. The Verify API generates a PIN code, with an associated request_id.

    It is possible to supply your own PIN code in some circumstances, please contact your account manager.

  4. The Verify API then attempts to deliver this PIN to the user. The format (SMS or Text-to-speech (TTS)) and timing of these attempts are defined by your chosen workflow. If the user does not revisit your app or website to enter the PIN they have received, the verification request will ultimately time out. Otherwise, you need to verify the number that they entered using by performing a Verification check.

Verification check

#{alt_text}

5. The user receives the PIN and enters it into your application.

6. Your application makes an API call to the Verification check endpoint, passing in the request_id and the PIN that the user entered.

7. The Verify API checks that the PIN entered matches the one that was sent and returns the result to your application.

Getting Started

The following sample shows you how to start the verification process by sending a verification code to a user. To learn how to validate the code the user supplies and perform other operations, see the Code Snippets.

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 nexmo
Initialize your dependencies

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

Copy to Clipboard
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET
});

View full source

Write the code

Add the following to request.js:

Copy to Clipboard
nexmo.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.nexmo:client:5.2.1'
Initialize your dependencies

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

Copy to Clipboard
NexmoClient client = NexmoClient.builder().apiKey(NEXMO_API_KEY).apiSecret(NEXMO_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, "NEXMO");

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.nexmo.quickstart.verify with the package containing StartVerification:

gradle run -Pmain=com.nexmo.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 Nexmo.Api.Verify;
using Nexmo.Api;
using Nexmo.Api.Request;

View full source

Initialize your dependencies

Add the following to SendVerificationRequest.cs:

Copy to Clipboard
var credentials = Credentials.FromApiKeyAndSecret(NEXMO_API_KEY, NEXMO_API_SECRET);
var client = new NexmoClient(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 nexmo/client
Initialize your dependencies

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

Copy to Clipboard
$basic  = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client(new \Nexmo\Client\Credentials\Container($basic));

View full source

Write the code

Add the following to request.php:

Copy to Clipboard
$verification = new \Nexmo\Verify\Verification(NUMBER, 'Acme Inc');
$client->verify()->start($verification);

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 nexmo

client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)

View full source

Write the code

Add the following to request.py:

Copy to Clipboard
response = client.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 nexmo
Initialize your dependencies

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

Copy to Clipboard
client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_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

Guides

Code Snippets

Use Cases

Further Reading