Local Setup¶
In this step, we'll configure your local development environment to use the Azure Communication Services (ACS) JavaScript SDK.
This tutorial walks these steps in order:
flowchart TD
START["Local Setup"]
N1["1. Install Node.js and npm"]
N2["2. Initialize Node.js Project"]
N3["3. Install ACS SDK Packages"]
N4["4. Create ACS Resource in Azure"]
N5["5. Get Connection String"]
N6["6. Set Up Environment Variables"]
N7["7. Verify with Simple Identity Token Creation"]
START --> N1
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
N5 --> N6
N6 --> N7 1. Install Node.js and npm¶
Ensure you have Node.js 18 or later and npm (installed with Node.js) or yarn installed.
2. Initialize Node.js Project¶
Create a new directory for your project and initialize it with npm.
3. Install ACS SDK Packages¶
Install the necessary ACS SDK packages via npm.
npm install @azure/communication-identity @azure/communication-sms @azure/communication-email @azure/communication-chat @azure/communication-calling @azure/communication-phone-numbers
4. Create ACS Resource in Azure¶
- Log in to the Azure Portal.
- Click Create a resource and search for Communication Services.
- Fill in the required details:
- Resource Group: Create a new or use an existing one.
- Resource Name: Choose a unique name.
- Data Location: Select a location near you.
- Click Review + create and then Create.
5. Get Connection String¶
- Once the resource is deployed, navigate to it in the Azure Portal.
- Under Settings, select Keys.
- Copy the Connection string for the primary key.
6. Set Up Environment Variables¶
It's best practice to store sensitive information in environment variables.
# Linux/MacOS
export COMMUNICATION_SERVICES_CONNECTION_STRING="<your-connection-string>"
# Windows
# setx COMMUNICATION_SERVICES_CONNECTION_STRING "<your-connection-string>"
7. Verify with Simple Identity Token Creation¶
Create a file named identity_token.js and add the following code to verify your setup.
const { CommunicationIdentityClient } = require("@azure/communication-identity");
async function main() {
try {
const connectionString = process.env.COMMUNICATION_SERVICES_CONNECTION_STRING;
if (!connectionString) {
console.log("Please set the COMMUNICATION_SERVICES_CONNECTION_STRING environment variable.");
return;
}
// Initialize the client
const client = new CommunicationIdentityClient(connectionString);
// Create a new user identity
const user = await client.createUser();
console.log(`Created a new user identity: ${user.communicationUserId}`);
// Issue an access token for the user with 'chat' and 'voip' scopes
const tokenResult = await client.getToken(user, ["chat", "voip"]);
console.log(`Issued an access token: ${tokenResult.token}`);
console.log(`Token expires at: ${tokenResult.expiresOn}`);
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}
main();
Run the script to verify your connection: