@azure/communication-identity

中文文档
2.0.0发布于 2026/10/02MITTypeScriptESM + CJS近 30 天下载 16.9万

SDK for Azure Communication service which facilitates user token administration.

原始使用文档
文档目录25 节
  1. Getting started
  2. Prerequisites
  3. Installing
  4. Browser support
  5. Key concepts
  6. Clients
  7. Examples
  8. Authentication
  9. Create KeyCredential with AzureKeyCredential before initial…
  10. Using a connection string
  11. Using a TokenCredential
  12. Usage
  13. Creating a new user
  14. Creating and refreshing a user token
  15. Creating a user token with custom expiration
  16. Creating a user and a token in a single request
  17. Creating a user and a token with custom expiration in a sin…
  18. Revoking tokens for a user
  19. Deleting a user
  20. Exchanging Azure AD access token of a Teams User for a Comm…
  21. Troubleshooting
  22. Logging
  23. Next steps
  24. Contributing
  25. Related projects

Azure Communication Identity client library for JavaScript

The identity library is used for managing users and tokens for Azure Communication Services.

Getting started

Prerequisites

Installing

npm install @azure/communication-identity

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

Clients

The CommunicationIdentityClient provides methods to manage users and their tokens.

Examples

Authentication

You can get a key and/or connection string from your Communication Services resource in Azure Portal. Once you have a key, you can authenticate the CommunicationIdentityClient with any of the following methods:

Create KeyCredential with AzureKeyCredential before initializing the client

import { AzureKeyCredential } from "@azure/core-auth";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const key = "<some-key>";
const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new AzureKeyCredential(key);
const client = new CommunicationIdentityClient(endpoint, credential);

Using a connection string

import { CommunicationIdentityClient } from "@azure/communication-identity";

// Example connection string
const connectionString =
  "endpoint=https://contoso.eastus.communications.azure.net/;accesskey=secret";

const client = new CommunicationIdentityClient(connectionString);

Using a TokenCredential

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

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.

Usage

Creating a new user

Use the createUser method to create a new user.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

Creating and refreshing a user token

Use the getToken method to issue or refresh a token for an existing user. The method also takes in a list of communication token scopes. Scope options include:

  • chat (Use this for full access to Chat APIs)
  • voip (Use this for full access to Calling APIs)
  • chat.join (Access to Chat APIs but without the authorization to create, delete or update chat threads)
  • chat.join.limited (A more limited version of chat.join that doesn't allow to add or remove participants)
  • voip.join (Access to Calling APIs but without the authorization to start new calls)
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

const { token } = await client.getToken(user, ["chat"]);

To refresh the user token, issue another token with the same user.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

let { token } = await client.getToken(user, ["chat"]);

// Refresh the token again
({ token } = await client.getToken(user, ["chat"]));

Creating a user token with custom expiration

It's also possible to create a Communication Identity access token by customizing the expiration time. Validity period of the token must be within [60,1440] minutes range. If not provided, the default value of 1440 minutes (24 hours) will be used.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

const tokenOptions = { tokenExpiresInMinutes: 60 };
const { token } = await client.getToken(user, ["chat"], tokenOptions);

Creating a user and a token in a single request

For convenience, use createUserAndToken to create a new user and issue a token with one function call. This translates into a single web request as opposed to creating a user first and then issuing a token.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const { user, token } = await client.createUserAndToken(["chat"]);

Creating a user and a token with custom expiration in a single request

It's also possible to create a Communication Identity access token by customizing the expiration time. Validity period of the token must be within [60,1440] minutes range. If not provided, the default value of 1440 minutes (24 hours) will be used.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const userAndTokenOptions = { tokenExpiresInMinutes: 60 };
const { user, token } = await client.createUserAndToken(["chat"], userAndTokenOptions);

Revoking tokens for a user

Use the revokeTokens method to revoke all issued tokens for a user.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

// Create user
const user = await client.createUser();

// Later when you want to revoke the user's tokens
await client.revokeTokens(user);

Deleting a user

Use the deleteUser method to delete a user.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

// Create user
const user = await client.createUser();

// Later when you want to delete the user
await client.deleteUser(user);

Exchanging Azure AD access token of a Teams User for a Communication access token

Use getTokenForTeamsUser method to exchange an Azure AD access token of a Teams user for a new CommunicationAccessToken with a matching expiration time.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const { token, expiresOn } = await client.getTokenForTeamsUser({
  teamsUserAadToken: "<aad-access-token-of-a-teams-user>",
  clientId: "<cliend-id-of-an-aad-application>",
  userObjectId: "<aad-object-id-of-a-teams-user>",
});

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

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.

版本与发布记录

@azure/communication-identity 共发布 442 个版本,首次发布于 2021/01/27;过去 12 个月发布了 57 个版本。

版本发布日期距今
2.0.0latest2026/10/021 个月前
1.4.0-alpha.20260211.1dev2026/02/118 个月前
1.4.0-alpha.20260210.12026/02/108 个月前
1.4.0-alpha.20260209.12026/02/098 个月前
1.4.0-alpha.20260206.12026/02/069 个月前
1.4.0-alpha.20260205.12026/02/059 个月前

其他发布通道:next → 1.4.0-beta.1,dev → 1.4.0-alpha.20260211.1。

关于 @azure/communication-identity 的常见问题

@azure/communication-identity 是什么?

SDK for Azure Communication service which facilitates user token administration.

如何安装 @azure/communication-identity?

在项目目录运行 npm install @azure/communication-identity,或使用 pnpm add / yarn add / bun add。

@azure/communication-identity 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/communication-identity 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/communication-identity 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/communication-identity 可以商用吗?

@azure/communication-identity 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/communication-identity 的下载量有多少?

根据 npm 官方下载统计,@azure/communication-identity 在 2026/10/08 – 2026/11/06 这 30 天内被下载 168,678 次,日均约 5,623 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/communication-identity 还在维护吗?

最新版本 2.0.0 发布于 2026/10/02(1 个月前);至今共发布 442 个版本,其中过去 12 个月发布了 57 个;首次发布于 2021/01/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。