Azure Communication Identity Client

1.5.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `CommunicationIdentityClient`, create a new user identity, and issue an access token for that identity with specific scopes (e.g., chat and VoIP). It requires the `COMMUNICATION_SERVICES_CONNECTION_STRING` environment variable to be set, containing your Azure Communication Services resource's endpoint and access key. Identities incur costs, so consider deleting them after use if they are temporary.

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}")

view raw JSON →