azure-sdk-for-js/sdk/communication/communication-recipient-ver...
Matthew Podwysocki 6de6ddeeb2
[communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727)
### Packages impacted by this PR

- @azure-tools/communication-recipient-verification

### Issues associated with this PR

- https://github.com/Azure/azure-sdk-for-js/issues/31338

### Describe the problem that is addressed by this PR

Moves @azure-tools/communication-recipient-verification to ESM/vitest

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
2024-11-12 16:05:35 -05:00
..
review [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
samples/v1-beta [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
samples-dev
src [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
swagger
test/public [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
CHANGELOG.md [communication] Add header to changelog (#30207) 2024-06-27 10:13:28 -07:00
README.md [eslint] fix linting issues in communication README.md files (#30817) 2024-08-21 10:34:57 -07:00
api-extractor.json [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
assets.json Fix Communication Rooms Readme Issue And Push Recordings In Communication Library (#28607) 2024-06-03 13:54:16 +08:00
package.json [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
sample.env
tests.yml Switch to TME test subscription (#31329) 2024-10-14 18:19:12 -07:00
tsconfig.browser.config.json [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
tsconfig.json [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
vitest.browser.config.ts [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00
vitest.config.ts [communication] Move @azure-tools/communication-recipient-verification to ESM/vitest (#31727) 2024-11-12 16:05:35 -05:00

README.md

Azure Communication Recipient Verification client library for JavaScript

The Recipient Verification library allows users to verify the phone number of recipients before sending messages or making calls to the phone number.

Getting started

Prerequisites

Installing

npm install @azure-tools/communication-recipient-verification

Browser support

JavaScript Bundle

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.

Key concepts

Examples

Authentication

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:

Using a connection string

const {
  RecipientVerificationClient,
} = require("@azure-tools/communication-recipient-verification");
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);

Using an access key with AzureKeyCredential

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);

Using an Azure Active Directory 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");

const credential = new DefaultAzureCredential();
const client = new RecipientVerificationClient("<endpoint-from-resource>", credential);

Usage

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

import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);

async function main() {
  // body of the request
  const VerificationRequest = {
    identity: "+11234567890",
    channel: "sms",
  };

  // get the verification status
  const status = await client.requestVerification(VerificationRequest);
  console.log(status);
}

main();

Verify phone number

import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);

async function main() {
  // 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();

Remove verified number

import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);

async function main() {
  // 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();

Get verified numbers

import { RecipientVerificationClient } from "@azure-tools/communication-recipient-verification";
const connectionString = "endpoint=<endpoint>;accessKey=<accessKey>";
const client = new RecipientVerificationClient(connectionString);

async function main() {
  // get all verifications for a resource
  const verifications = await client.getVerifications();

  // print all verifications
  for await (const verification of verifications) {
    console.log(verification);
  }
}

main();

Troubleshooting

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

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.

Impressions