Skip to main content

Microsoft Azure Communication Phone Numbers Client Library for Python

Project description

Build Status

Azure Communication Phone Numbers Package client library for Python

Azure Communication Phone Numbers client package is used to administer Phone Numbers.

Getting started

Prerequisites

Install the package

Install the Azure Communication Phone Numbers client library for Python with pip:

pip install azure-communication-phonenumbers

Key concepts

Initializing Phone Numbers Client

# You can find your connection string from your resource in the Azure Portal
import os
from azure.communication.phonenumbers import PhoneNumbersClient
from azure.identity import DefaultAzureCredential

endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have your
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
phone_number_administration_client = PhoneNumbersAdministrationClient(endpoint, DefaultAzureCredential())

Initializing the Client Using Your Connection String

Connection string authentication is also available for Phone Numbers Client.

# You can find your connection string from your resource in the Azure Portal
import os
from azure.communication.phonenumbers import PhoneNumbersClient

connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
phone_number_administration_client = PhoneNumbersAdministrationClient.from_connection_string(connection_str)

Phone Number Types overview

Phone numbers come in two types; Geographic and Toll-Free. Geographic phone numbers are phone numbers associated with a location, whose area codes are associated with the area code of a geographic location. Toll-Free phone numbers are phone numbers with no associated location. For example, in the US, toll-free numbers can come with area codes such as 800 or 888.

Searching and Purchasing and Releasing numbers

Phone numbers can be searched through the search creation API by providing an area code, quantity of phone numbers, application type, phone number type, and capabilities. The provided quantity of phone numbers will be reserved for ten minutes and can be purchased within this time. If the search is not purchased, the phone numbers will become available to others after ten minutes. If the search is purchased, then the phone numbers are acquired for the Azure resources.

Phone numbers can also be released using the release API.

Examples

Get All Phone Numbers

Lists all of your acquired phone numbers

acquired_phone_numbers = phone_numbers_client.list_acquired_phone_numbers()
acquired_phone_number = acquired_phone_numbers.next()
print(acquired_phone_number.phone_number)

Get Phone Number

Gets the information from the specified phone number

result = phone_numbers_client.get_phone_number("<phone number>")
print(result.country_code)
print(result.phone_number)

Long Running Operations

The Phone Number Client supports a variety of long running operations that allow indefinite polling time to the functions listed down below.

Search for Available Phone Number

You can search for available phone numbers by providing the capabilities of the phone you want to acquire, the phone number type, the assignment type, and the country code. It's worth mentioning that for the toll-free phone number type, proving the area code is optional. The result of the search can then be used to purchase the number in the corresponding API.

capabilities = PhoneNumberCapabilities(
        calling = PhoneNumberCapabilityType.INBOUND,
        sms = PhoneNumberCapabilityType.INBOUND_OUTBOUND
    )
poller = phone_numbers_client.begin_search_available_phone_numbers(
    "US",
    PhoneNumberType.TOLL_FREE,
    PhoneNumberAssignmentType.APPLICATION,
    capabilities,
    area_code ="833", # Area code is optional for toll-free numbers
    quantity = 2, # Quantity is optional. If not set, default is 1
    polling = True
)
search_result = poller.result()

Purchase Phone Numbers

The result of your search can be used to purchase the specificied phone numbers. This can be done by passing the search_id from the search response to the purchase phone number API.

purchase_poller = phone_numbers_client.begin_purchase_phone_numbers(
    search_result.search_id, 
    polling=True
)

Release Phone Number

Releases an acquired phone number.

poller = self.phone_number_client.begin_release_phone_number(
    "<phone number>", 
    polling = True
)

Updating Phone Number Capabilities

Updates the specified phone number capabilities for Calling and SMS to one of:

  • PhoneNumberCapabilityType.NONE
  • PhoneNumberCapabilityType.INBOUND
  • PhoneNumberCapabilityType.OUTBOUND
  • PhoneNumberCapabilityType.INBOUND_OUTBOUND
poller = self.phone_number_client.begin_update_phone_number_capabilities(
    "<phone number>",
    PhoneNumberCapabilityType.OUTBOUND,
    PhoneNumberCapabilityType.INBOUND_OUTBOUND,
    polling = True
)

Troubleshooting

The Phone Numbers Administration client will raise exceptions defined in Azure Core.

Next steps

More sample code

Please take a look at the samples directory for detailed examples of how to use this library.

Provide Feedback

If you encounter any bugs or have suggestions, please file an issue in the Issues section of the project

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-communication-phonenumbers-1.0.0b4.zip (66.0 kB view details)

Uploaded Source

Built Distribution

azure_communication_phonenumbers-1.0.0b4-py2.py3-none-any.whl (44.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file azure-communication-phonenumbers-1.0.0b4.zip.

File metadata

  • Download URL: azure-communication-phonenumbers-1.0.0b4.zip
  • Upload date:
  • Size: 66.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for azure-communication-phonenumbers-1.0.0b4.zip
Algorithm Hash digest
SHA256 9bf96acef97988f63bffc36d4f553a10c56d70eee70d5b69ba67b74d3baa72f0
MD5 f6a8990282e836d69a86a4c68660821d
BLAKE2b-256 206e8915ae5d99c4e00fa82bb4096b0a078a269bf2ca6aff16d90a0ac0d3b208

See more details on using hashes here.

File details

Details for the file azure_communication_phonenumbers-1.0.0b4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for azure_communication_phonenumbers-1.0.0b4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 32def4ad7b9452ec43eec6e7dadb7ff97fe7acb00b4f643f8eb5d2f745ed407a
MD5 ee6c91c5c097e00267a92c640221c1a3
BLAKE2b-256 717f467ee738ce1651a0b83ca08e33c865467ec21235f909b4f34e1a19876852

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page