|
| 1 | +## Setting Up |
| 2 | + |
| 3 | +### Create a new Node.js Application |
| 4 | + |
| 5 | +Open your terminal or command window create a new directory for your app, and navigate to it. |
| 6 | + |
| 7 | +```console |
| 8 | +mkdir managed-identity-quickstart && cd managed-identity--quickstart |
| 9 | +``` |
| 10 | + |
| 11 | +Run `npm init -y` to create a **package.json** file with default settings. |
| 12 | + |
| 13 | +```console |
| 14 | +npm init -y |
| 15 | +``` |
| 16 | + |
| 17 | +### Install the SDK packages |
| 18 | + |
| 19 | +```console |
| 20 | +npm install @azure/communication-identity |
| 21 | +npm install @azure/communication-common |
| 22 | +npm install @azure/communication-sms |
| 23 | +npm install @azure/identity |
| 24 | +``` |
| 25 | + |
| 26 | +### Create a new file |
| 27 | + |
| 28 | +Open a new file with a text editor and save it as `index.js`, we'll be placing our code inside this file. |
| 29 | + |
| 30 | +### Use the SDK packages |
| 31 | + |
| 32 | +Add the following `require` directives to the top of `index.js` to use the Azure Identity and Azure Storage SDKs. |
| 33 | + |
| 34 | +```JavaScript |
| 35 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 36 | +const { CommunicationIdentityClient, CommunicationUserToken } = require("@azure/communication-identity"); |
| 37 | +const { SmsClient, SmsSendRequest } = require("@azure/communication-sms"); |
| 38 | +``` |
| 39 | +## Create a DefaultAzureCredential |
| 40 | + |
| 41 | +We'll be using the [DefaultAzureCredential](/javascript/api/@azure/identity/defaultazurecredential) for this quickstart. This credential is suitable for production and development environments. As it is needed for each operation let's create it within the top of our `index.js` file. |
| 42 | + |
| 43 | +```JavaScript |
| 44 | + const credential = new DefaultAzureCredential(); |
| 45 | +``` |
| 46 | + |
| 47 | +## Create an identity and issue a token with Managed Identity |
| 48 | + |
| 49 | +Next, we'll write a function which creates a new identity and issues a token for this identity, we'll use this later to test our managed identity setup. |
| 50 | + |
| 51 | +```JavaScript |
| 52 | +async function createIdentityAndIssueToken(resourceEndpoint) { |
| 53 | + const client = new CommunicationIdentityClient(resourceEndpoint, credential); |
| 54 | + return await client.createUserAndToken(["chat"]); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Send an SMS with Managed Identity |
| 59 | + |
| 60 | +Now, lets write a function which uses managed identities to send an SMS: |
| 61 | + |
| 62 | +```JavaScript |
| 63 | +async function sendSms(resourceEndpoint, fromNumber, toNumber, message) { |
| 64 | + const smsClient = new SmsClient(resourceEndpoint, credential); |
| 65 | + const sendRequest = { |
| 66 | + from: fromNumber, |
| 67 | + to: [toNumber], |
| 68 | + message: message |
| 69 | + }; |
| 70 | + return await smsClient.send( |
| 71 | + sendRequest, |
| 72 | + {} //Optional SendOptions |
| 73 | + ); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Write the main method |
| 78 | + |
| 79 | +With our functions created we can now write a main function to call them and demonstrate the use of Managed Identities: |
| 80 | +```JavaScript |
| 81 | +async function main() { |
| 82 | + // You can find your endpoint and access key from your resource in the Azure portal |
| 83 | + // e.g. "https://<RESOURCE_NAME>.communication.azure.com"; |
| 84 | + endpoint = "https://<RESOURCE_NAME>.communication.azure.com/" |
| 85 | + |
| 86 | + |
| 87 | + console.log("Retrieving new Access Token, using Managed Identities"); |
| 88 | + const result = await createIdentityAndIssueToken(endpoint); |
| 89 | + console.log(`Retrieved Access Token: ${result.token}`); |
| 90 | + |
| 91 | + console.log("Sending SMS using Managed Identities"); |
| 92 | + |
| 93 | + // You will need a phone number from your resource to send an SMS. |
| 94 | + const smsResult = await sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities"); |
| 95 | + console.log(`SMS ID: ${smsResult[0].messageId}`); |
| 96 | + console.log(`Send Result Successful: ${smsResult[0].successful}`); |
| 97 | +} |
| 98 | + |
| 99 | +main(); |
| 100 | +``` |
| 101 | + |
| 102 | +The final `index.js` file should look like this: |
| 103 | +```JavaScript |
| 104 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 105 | +const { CommunicationIdentityClient, CommunicationUserToken } = require("@azure/communication-identity"); |
| 106 | +const { SmsClient, SmsSendRequest } = require("@azure/communication-sms"); |
| 107 | + |
| 108 | +const credential = new DefaultAzureCredential(); |
| 109 | + |
| 110 | +async function createIdentityAndIssueToken(resourceEndpoint) { |
| 111 | + const client = new CommunicationIdentityClient(resourceEndpoint, credential); |
| 112 | + return await client.createUserAndToken(["chat"]); |
| 113 | +} |
| 114 | + |
| 115 | +async function sendSms(resourceEndpoint, fromNumber, toNumber, message) { |
| 116 | + const smsClient = new SmsClient(resourceEndpoint, credential); |
| 117 | + const sendRequest = { |
| 118 | + from: fromNumber, |
| 119 | + to: [toNumber], |
| 120 | + message: message |
| 121 | + }; |
| 122 | + return await smsClient.send( |
| 123 | + sendRequest, |
| 124 | + {} //Optional SendOptions |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +async function main() { |
| 129 | + // You can find your endpoint and access key from your resource in the Azure portal |
| 130 | + // e.g. "https://<RESOURCE_NAME>.communication.azure.com"; |
| 131 | + endpoint = "https://<RESOURCE_NAME>.communication.azure.com/" |
| 132 | + |
| 133 | + |
| 134 | + console.log("Retrieving new Access Token, using Managed Identities"); |
| 135 | + const result = await createIdentityAndIssueToken(endpoint); |
| 136 | + console.log(`Retrieved Access Token: ${result.token}`); |
| 137 | + |
| 138 | + console.log("Sending SMS using Managed Identities"); |
| 139 | + |
| 140 | + // You will need a phone number from your resource to send an SMS. |
| 141 | + const smsResult = await sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities"); |
| 142 | + console.log(`SMS ID: ${smsResult[0].messageId}`); |
| 143 | + console.log(`Send Result Successful: ${smsResult[0].successful}`); |
| 144 | +} |
| 145 | + |
| 146 | +main(); |
| 147 | +``` |
| 148 | + |
| 149 | +## Run the Program |
| 150 | + |
| 151 | +With everything complete, you can run the file by entering `node index.js` from your project's directory. If everything went well you should see something similar to the following. |
| 152 | + |
| 153 | +```Bash |
| 154 | + $ node index.js |
| 155 | + Retrieving new Access Token, using Managed Identities |
| 156 | + Retrieved Access Token: ey...Q |
| 157 | + Sending SMS using Managed Identities |
| 158 | + SMS ID: Outgoing_2021040602194...._noam |
| 159 | + Send Result Successful: true |
| 160 | +``` |
0 commit comments