Skip to content

Phone Number Management

This recipe shows how to search, purchase, and release Azure Communication Services (ACS) phone numbers using the Python SDK.

Prerequisites

SDK Installation

pip install azure-communication-phonenumbers

Search Available Phone Numbers

You can search for available phone numbers by country, area code, and type.

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

# Initialize client
endpoint = os.getenv("COMMUNICATION_SERVICES_ENDPOINT")
client = PhoneNumbersClient(endpoint, DefaultAzureCredential())

# Search for available phone numbers in the US with SMS capabilities
search_poller = client.begin_search_available_phone_numbers(
    area_code="425",
    country_code="US",
    phone_number_type="geographic",
    assignment_type="person",
    capabilities={"sms": "inbound+outbound", "calling": "none"},
    quantity=1
)

search_result = search_poller.result()
print(f"Found phone number: {search_result.phone_numbers[0]}")
print(f"Search ID: {search_result.search_id}")

Purchase Phone Numbers

To purchase a phone number, use the search_id from the search result.

# Purchase the phone number (uncomment carefully)
# purchase_poller = client.begin_purchase_phone_numbers(search_result.search_id)
# purchase_poller.result()
# print("Phone number purchased successfully!")

Configure Capabilities

You can update the capabilities of an existing phone number.

# Update capabilities for a specific phone number
# update_poller = client.begin_update_phone_number_capabilities(
#     "<your-phone-number>",
#     sms="inbound+outbound",
#     calling="none"
# )
# update_poller.result()
# print("Phone number capabilities updated!")

Release Phone Numbers

If you no longer need a phone number, you can release it.

# Release a phone number (uncomment carefully)
# release_poller = client.begin_release_phone_number("<your-phone-number>")
# release_poller.result()
# print("Phone number released successfully!")

List Purchased Phone Numbers

You can list all the phone numbers currently purchased in your ACS resource.

# List all purchased phone numbers
purchased_numbers = client.list_purchased_phone_numbers()
for number in purchased_numbers:
    print(f"Phone number: {number.phone_number}, Type: {number.phone_number_type}")

See Also

Sources