Azure Communication Identity Client
The `azure-communication-identity` library provides client-side functionality for managing user identities and issuing access tokens for Azure Communication Services. It allows applications to create, delete, and manage users, and to generate tokens required for client-side authentication with various communication functionalities like chat and calling. The current version is 1.5.0, and new versions are released regularly as part of the broader Azure SDK for Python, typically every 1-2 months or as features/fixes dictate.
Common errors
-
ModuleNotFoundError: No module named 'azure.communication.identity'
cause The package `azure-communication-identity` is not installed in your Python environment.fixRun: `pip install azure-communication-identity` -
AttributeError: 'CommunicationIdentityClient' object has no attribute 'issue_token'
cause The method `issue_token` was renamed to `get_token` in version 1.3.0.fixUpdate your code to use `client.get_token(user, scopes=[...])` instead of `client.issue_token(...)`. -
ValueError: Connection string is not in valid format.
cause The provided connection string is malformed or missing required components like 'endpoint' or 'accesskey'.fixDouble-check your `COMMUNICATION_SERVICES_CONNECTION_STRING` environment variable or hardcoded string against the format `endpoint=https://<your-resource>.communication.azure.com/;accesskey=<your-access-key>`. -
ImportError: cannot import name 'CommunicationTokenScope' from 'azure.communication.identity'
cause Starting from version 1.4.0, `CommunicationTokenScope` was moved to the `models` submodule.fixChange your import statement to `from azure.communication.identity.models import CommunicationTokenScope`.
Warnings
- breaking The client class name changed from `CommunicationUserTokenClient` (in preview versions) to `CommunicationIdentityClient`.
- breaking The method for issuing tokens was renamed from `issue_token` to `get_token`.
- breaking The `CommunicationTokenScope` enum moved from the main package namespace to the `models` submodule.
- gotcha Azure Communication Service identities incur costs. Ensure you delete identities when they are no longer needed to avoid unexpected charges.
- gotcha The connection string format must be exact: `endpoint=https://<your-resource>.communication.azure.com/;accesskey=<your-access-key>`. Missing parts or incorrect delimiters will lead to authentication errors.
Install
-
pip install azure-communication-identity
Imports
- CommunicationIdentityClient
from azure.communication.identity import CommunicationIdentityClient
- CommunicationTokenScope
from azure.communication.identity import CommunicationTokenScope
from azure.communication.identity.models import CommunicationTokenScope
Quickstart
import os
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.identity.models import CommunicationTokenScope
# Set the COMMUNICATION_SERVICES_CONNECTION_STRING environment variable
# Example: 'endpoint=https://<your-resource>.communication.azure.com/;accesskey=<your-access-key>'
connection_string = os.environ.get(
"COMMUNICATION_SERVICES_CONNECTION_STRING",
"endpoint=https://<your-resource>.communication.azure.com/;accesskey=<your-access-key>"
)
if "endpoint=" not in connection_string or "accesskey=" not in connection_string:
print("Error: COMMUNICATION_SERVICES_CONNECTION_STRING not set or invalid.")
print("Please ensure it contains 'endpoint=' and 'accesskey='.")
exit(1)
try:
client = CommunicationIdentityClient.from_connection_string(connection_string)
print("CommunicationIdentityClient initialized.")
# Create a new identity
user = client.create_user()
print(f"Created identity with ID: {user.communication_user_id}")
# Issue an access token for the identity
token_response = client.get_token(
user,
scopes=[CommunicationTokenScope.CHAT, CommunicationTokenScope.VOIP]
)
print(f"Issued token (partial): {token_response.token[:10]}...{token_response.token[-10:]}")
print(f"Token expires on: {token_response.expires_on}")
# Clean up: Delete the created identity (optional, but good for cost management)
# client.delete_user(user)
# print(f"Deleted identity: {user.communication_user_id}")
except Exception as e:
print(f"An error occurred: {e}")