Azure Communication Phone Numbers

raw JSON →
1.4.0 verified Fri May 01 auth: no python

Microsoft Azure Communication Phone Numbers client library for Python. Manage phone number resources, purchase, release, and search for phone numbers. Current version: 1.4.0. Released monthly with Azure SDK for Python.

pip install azure-communication-phonenumbers
error ImportError: cannot import name 'PhoneNumbersClient' from 'azure.communication'
cause Incorrect import path; missing 'phonenumbers' subpackage.
fix
Use: from azure.communication.phonenumbers import PhoneNumbersClient
error AttributeError: 'PhoneNumberCapabilities' object has no attribute 'sms'
cause Trying to access a capability that was not set; capabilities are optional and may be None.
fix
Check capability attribute for None before use: if cap.sms: ...
breaking Version 1.4.0 drops support for Python <3.9.
fix Upgrade Python to 3.9+.
gotcha PhoneNumberCapabilities is imported directly from azure.communication.phonenumbers, not from a 'models' submodule.
fix Use: from azure.communication.phonenumbers import PhoneNumberCapabilities
gotcha The 'phone_number' property returns a string with the '+' prefix (e.g., '+15551234567').
fix Strip '+' if needed for E.164 comparison.

Initialize the client and list purchased phone numbers.

import os
from azure.communication.phonenumbers import PhoneNumbersClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ.get('COMMUNICATION_SERVICES_ENDPOINT', '')
credential = DefaultAzureCredential()
client = PhoneNumbersClient(endpoint, credential)

# List purchased phone numbers
purchased = client.list_purchased_phone_numbers()
for pn in purchased:
    print(pn.phone_number)