The Recipient Verification library allows users to verify the phone number of recipients before sending messages or making calls to the phone number.
- An Azure subscription.
- An existing Communication Services resource. If you need to create the resource, you can use the Azure Portal, the Azure PowerShell, or the Azure CLI.
npm install @azure-tools/communication-recipient-verification
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.
To create a client object to access the Communication Services API, you will need a connection string
or the endpoint
of your Communication Services resource and a credential
. The Recipient Verification client can use either Azure Active Directory credentials or an API key credential to authenticate.
You can get a key and/or connection string from your Communication Services resource in the Azure Portal. You can also find the endpoint for your Communication Services resource in the Azure Portal.
Once you have a key, you can authenticate the RecipientVerificationClient
with any of the following methods:
const {
RecipientVerificationClient,
} = require("@azure-tools/communication-recipient-verification");
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);
If you use a key to initialize the client you will also need to provide the appropriate endpoint. You can get this endpoint from your Communication Services resource in Azure Portal. Once you have a key and endpoint, you can authenticate with the following code:
const { AzureKeyCredential } = require("@azure/core-auth");
const {
RecipientVerificationClient,
} = require("@azure-tools/communication-recipient-verification");
const credential = new AzureKeyCredential("<key-from-resource>");
const client = new RecipientVerificationClient("<endpoint-from-resource>", credential);
Connection string authentication is used in most of the examples, but you can also authenticate with Azure Active Directory using the Azure Identity library. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the @azure/identity
package:
npm install @azure/identity
The @azure/identity
package provides a variety of credential types that your application can use to do this. The README for @azure/identity
provides more details and samples to get you started.
const { DefaultAzureCredential } = require("@azure/identity");
const {
RecipientVerificationClient,
} = require("@azure-tools/communication-recipient-verification");
let credential = new DefaultAzureCredential();
const client = new RecipientVerificationClient("<endpoint-from-resource>", credential);
The following sections provide code snippets that cover some of the common tasks using the Azure Communication Services Recipient Verification Client. The scenarios that are covered here consist of:
- Request phone number verification code
- Verify phone number
- Remove a verified number
- Get verified numbers
import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);
async main function() {
// body of the request
const VerificationRequest = {
identity: "+11234567890",
channel: "sms",
};
// get the verification status
var status = await client.requestVerification(VerificationRequest);
console.log(status);
}
main();
import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);
async main function() {
// id that is used to reference users phone number
const verificationId = "7e5dd7e1-5203-41ab-960e-65c1eb804fc6";
// body of the request
const VerificationRequest = {
verificationCode: "1234567",
};
// verifying your phone number
const status = await client.verifyIdentity(verificationId, VerificationRequest);
console.log(status);
}
main();
import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);
async main function() {
// id that is used to reference users phone number
const verificationId = "4d313ff0-3aeb-477e-8c15-7c9a893e8999";
// delete verification for a resource
await client.deleteVerification(verificationId);
}
main();
import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);
async main function() {
// get all verifications for a resource
var verifications = await client.getVerifications();
// print all verifications
for await (const verification of verifications) {
console.log(verification);
}
}
main();
Please take a look at the samples directory for detailed examples on how to use this library.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.