Look up operator information for a phone number using Azure Communication Services
Artikel
Get started with the Phone Numbers client library for JavaScript to look up operator information for phone numbers. Use the operator information to determine whether and how to communicate with that phone number. Follow these steps to install the package and look up operator information about a phone number.
The --save option adds the library as a dependency in your package.json file.
Code examples
Authenticate the client
Import the PhoneNumbersClient from the client library and instantiate it with your connection string, which can be acquired from an Azure Communication Services resource in the Azure portal. Using a COMMUNICATION_SERVICES_CONNECTION_STRING environment variable is recommended to avoid putting your connection string in plain text within your code. Learn how to manage your resource's connection string.
Add the following code to the top of number-lookup-quickstart.js:
JavaScript
const { PhoneNumbersClient } = require('@azure/communication-phone-numbers');
// This code retrieves your connection string from an environment variableconst connectionString = process.env['COMMUNICATION_SERVICES_CONNECTION_STRING'];
// Instantiate the phone numbers clientconst phoneNumbersClient = new PhoneNumbersClient(connectionString);
Look up phone number formatting
To search for a phone number's operator information, call searchOperatorInformation from the PhoneNumbersClient.
JavaScript
let formattingResults = await phoneNumbersClient.searchOperatorInformation([ "<target-phone-number>" ]);
Replace <target-phone-number> with the phone number you're looking up, usually a number you'd like to send a message to.
Amaran
Provide phone numbers in E.164 international standard format, for example, +14255550123.
Look up operator information for a number
To search for a phone number's operator information, call searchOperatorInformation from the PhoneNumbersClient, passing true for the includeAdditionalOperatorDetails option.
Get started with the Phone Numbers client library for C# to look up operator information for phone numbers. Use the operator information to determine whether and how to communicate with that phone number. Follow these steps to install the package and look up operator information about a phone number.
In a terminal or command window, run the dotnet command to check that the .NET SDK is installed.
Setting up
To set up an environment for sending lookup queries, take the steps in the following sections.
Create a new C# application
In a terminal or command window, run the dotnet new command to create a new console app with the name NumberLookupQuickstart. This command creates a simple "Hello World" C# project with a single source file, Program.cs.
Console
dotnet new console -o NumberLookupQuickstart
Change your directory to the newly created app folder and use the dotnet build command to compile your application.
Console
cd NumberLookupQuickstart
dotnet build
Connect to dev package feed
The public preview version of the SDK is published to a dev package feed. You can add the dev feed using the NuGet CLI, which adds it to the NuGet.Config file.
Console
nuget sources add -Name "Azure SDK for .NET Dev Feed" -Source "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json"
More detailed information and other options for connecting to the dev feed can be found in the contributing guide.
Install the package
While still in the application directory, install the Azure Communication Services PhoneNumbers client library for .NET package by using the following command.
Phone Number clients can be authenticated using connection string acquired from an Azure Communication Services resource in the Azure portal. Using a COMMUNICATION_SERVICES_CONNECTION_STRING environment variable is recommended to avoid putting your connection string in plain text within your code. Learn how to manage your resource's connection string.
C#
// This code retrieves your connection string from an environment variable.string? connectionString = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_CONNECTION_STRING");
PhoneNumbersClient client = new PhoneNumbersClient(connectionString, new PhoneNumbersClientOptions(PhoneNumbersClientOptions.ServiceVersion.V2024_03_01_Preview));
Phone Number clients can also authenticate with Microsoft Entra authentication. With this option,
AZURE_CLIENT_SECRET, AZURE_CLIENT_ID, and AZURE_TENANT_ID environment variables need to be set up for authentication.
C#
// Get an endpoint to our Azure Communication Services resource.
Uri endpoint = new Uri("<endpoint_url>");
TokenCredential tokenCredential = new DefaultAzureCredential();
client = new PhoneNumbersClient(endpoint, tokenCredential);
Look up phone number formatting
To look up the national and international formatting for a number, call SearchOperatorInformationAsync from the PhoneNumbersClient.
Replace <target-phone-number> with the phone number you're looking up, usually a number you'd like to send a message to.
Amaran
Provide phone numbers in E.164 international standard format, for example, +14255550123.
Look up operator information for a number
To search for a phone number's operator information, call SearchOperatorInformationAsync from the PhoneNumbersClient, passing true for the IncludeAdditionalOperatorDetails option.
Using this function incurs a charge to your account.
Use operator information
You can now use the operator information. For this quickstart guide, we can print some of the details to the console.
First, we can print details about the number format.
C#
OperatorInformation formattingInfo = formattingResult.Values[0];
Console.WriteLine($"{formattingInfo.PhoneNumber} is formatted {formattingInfo.InternationalFormat} internationally, and {formattingInfo.NationalFormat} nationally");
Next, we can print details about the phone number and operator.
C#
OperatorInformation operatorInformation = searchResult.Values[0];
Console.WriteLine($"{operatorInformation.PhoneNumber} is a {operatorInformation.NumberType ?? "unknown"} number, operated in {operatorInformation.IsoCountryCode} by {operatorInformation.OperatorDetails.Name ?? "an unknown operator"}");
You can also use the operator information to determine whether to send an SMS. For more information about sending an SMS, see Send an SMS message.
Run the code
Run the application from your terminal or command window with the dotnet run command.
Get started with the Phone Numbers client library for Java to look up operator information for phone numbers. Use the operator information to determine whether and how to communicate with that phone number. Follow these steps to install the package and look up operator information about a phone number.
In a terminal or command window, run the mvn -v command to check that Maven is installed.
Setting up
To set up an environment for sending lookup queries, take the steps in the following sections.
Create a new Java application
In a terminal or command window, navigate to the directory where you'd like to create your Java application. Run the following command to generate the Java project from the maven-archetype-quickstart template.
The 'generate' task creates a directory with the same name as the artifactId. Under this directory, the src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project's Project Object Model, or POM.
Connect to dev package feed
The public preview version of the SDK is published to a dev package feed. To connect to the dev feed, open the pom.xml file in your text editor and add the dev repo to both your pom.xml's <repositories> and <distributionManagement> sections that you can add if they don't already exist.
The client can be authenticated using a connection string acquired from an Azure Communication Services resource in the Azure portal. Using a COMMUNICATION_SERVICES_CONNECTION_STRING environment variable is recommended to avoid putting your connection string in plain text within your code. Learn how to manage your resource's connection string.
Java
// This code retrieves your connection string from an environment variable
String connectionString = System.getenv("COMMUNICATION_SERVICES_CONNECTION_STRING");
PhoneNumbersClient phoneNumberClient = new PhoneNumbersClientBuilder()
.connectionString(connectionString)
.buildClient();
Alternatively, you can authenticate using Microsoft Entra authentication. Using the DefaultAzureCredentialBuilder is the easiest way to get started with Microsoft Entra ID. You can acquire your resource name from an Azure Communication Services resource in the Azure portal.
Java
// You can find your resource name from your resource in the Azure portal
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com";
PhoneNumbersClient phoneNumberClient = new PhoneNumbersClientBuilder()
.endpoint(endpoint)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Look up phone number formatting
To look up the national and international formatting for a number, call searchOperatorInformation from the PhoneNumbersClient.
Java
ArrayList<String> phoneNumbers = new ArrayList<String>();
phoneNumbers.add("<target-phone-number>");
// Use the free number lookup functionality to get number formatting information
OperatorInformationResult formattingResult = phoneNumberClient.searchOperatorInformation(phoneNumbers);
OperatorInformation formattingInfo = formattingResult.getValues().get(0);
Replace <target-phone-number> with the phone number you're looking up, usually a number you'd like to send a message to.
Amaran
Provide phone numbers in E.164 international standard format, for example, +14255550123.
Look up operator information for a number
To search for a phone number's operator information, call searchOperatorInformationWithResponse from the PhoneNumbersClient, passing true for the IncludeAdditionalOperatorDetails option.
Java
OperatorInformationOptions options = new OperatorInformationOptions();
options.setIncludeAdditionalOperatorDetails(true);
Response<OperatorInformationResult> result = phoneNumberClient.searchOperatorInformationWithResponse(phoneNumbers, options, Context.NONE);
OperatorInformation operatorInfo = result.getValue().getValues().get(0);
Amaran
Using this function incurs a charge to your account.
Use operator information
You can now use the operator information. For this quickstart guide, we can print some of the details to the console.
First, we can print details about the number format.
Java
System.out.println(formattingInfo.getPhoneNumber() + " is formatted "
+ formattingInfo.getInternationalFormat() + " internationally, and "
+ formattingInfo.getNationalFormat() + " nationally");
Next, we can print details about the phone number and operator.
Java
String numberType = operatorInfo.getNumberType() == null ? "unknown" : operatorInfo.getNumberType().toString();
String operatorName = "an unknown operator";
if (operatorInfo.getOperatorDetails()!= null && operatorInfo.getOperatorDetails().getName() != null)
{
operatorName = operatorInfo.getOperatorDetails().getName();
}
System.out.println(operatorInfo.getPhoneNumber() + " is a " + numberType + " number, operated in "
+ operatorInfo.getIsoCountryCode() + " by " + operatorName);
You can also use the operator information to determine whether to send an SMS. For more information, see Send an SMS message.
Run the code
Run the application from your terminal or command window with the following commands:
Navigate to the directory containing the pom.xml file and compile the project.
Get started with the Phone Numbers client library for Python to look up operator information for phone numbers. Use the operator information to determine whether and how to communicate with that phone number. Follow these steps to install the package and look up operator information about a phone number.
In a terminal or command window, run the python --version command to check that Python is installed.
Setting up
To set up an environment for sending lookup queries, take the steps in the following sections.
Create a new Python application
In a terminal or command window, create a new directory for your app and navigate to it.
Console
mkdir number-lookup-quickstart && cd number-lookup-quickstart
Use a text editor to create a file called number_lookup_sample.py in the project root directory and add the following code. The remaining quickstart code is added in the following sections.
Python
import os
from azure.communication.phonenumbers import PhoneNumbersClient
try:
print('Azure Communication Services - Number Lookup Quickstart')
# Quickstart code goes hereexcept Exception as ex:
print('Exception:')
print(ex)
Install the package
While still in the application directory, install the Azure Communication Services PhoneNumbers client library for Python package by using the pip install command.
The client can be authenticated using a connection string acquired from an Azure Communication Services resource in the Azure portal. Using a COMMUNICATION_SERVICES_CONNECTION_STRING environment variable is recommended to avoid putting your connection string in plain text within your code. Learn how to manage your resource's connection string.
Python
# This code retrieves your connection string from an environment variable
connection_string = os.getenv('COMMUNICATION_SERVICES_CONNECTION_STRING')
try:
phone_numbers_client = PhoneNumbersClient.from_connection_string(connection_string)
except Exception as ex:
print('Exception:')
print(ex)
Alternatively, the client can be authenticated using Microsoft Entra authentication. Using the DefaultAzureCredential object is the easiest way to get started with Microsoft Entra ID and you can install it using the pip install command.
Console
pip install azure-identity
Creating a DefaultAzureCredential object requires you to have AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID already set as environment variables with their corresponding values from your registered Microsoft Entra application.
Once the azure-identity library is installed, you can continue authenticating the client.
Python
from azure.identity import DefaultAzureCredential
# You can find your endpoint from your resource in the Azure portal
endpoint = 'https://<RESOURCE_NAME>.communication.azure.com'try:
credential = DefaultAzureCredential()
phone_numbers_client = PhoneNumbersClient(endpoint, credential)
except Exception as ex:
print('Exception:')
print(ex)
Look up phone number formatting
To look up the national and international formatting for a number, call search_operator_information from the PhoneNumbersClient.
Replace <target-phone-number> with the phone number you're looking up, usually a number you'd like to send a message to.
Amaran
Provide phone numbers in E.164 international standard format, for example, +14255550123.
Look up operator information for a number
To search for a phone number's operator information, call search_operator_information from the PhoneNumbersClient, passing True for the include_additional_operator_details option.
Changes to environment variables might not take effect in programs that are already running. If you notice your environment variables aren't working as expected, try closing and reopening any programs you're using to run and edit code.
The data returned by this endpoint is subject to various international laws and regulations, therefore the accuracy of the results depends on several factors. These factors include whether the number was ported, the country code, and the approval status of the caller. Based on these factors, operator information might not be available for some phone numbers or could reflect the original operator of the phone number, not the current operator.