Phone Numbers

Phone Numbers

On this page
  1. Getting started with Phone Numbers
  2. Get prepared
  3. Search phone numbers
  4. Purchase phone number
  5. Edit phone number

Enfonica Phone Numbers gives you instant access to local, mobile and toll-free numbers for your voice and SMS applications.

With the Phone Numbers API, you can search for, purchase and configure phone numbers. Enfonica can also port a number that you own to Enfonica.

Getting started with Phone Numbers

This quickstart shows you how to search for Enfonica phone numbers and then purchase them for your project and how to updated them.

Get prepared

To use the API, you will need:

  1. Your access token (a bearer token).
  2. Your project name (e.g. projects/my-project).

To use the Enfonica Client Library, you will need:

  1. Your service account key, Click here for more information on how to get your service account key.

Search phone numbers

Search through our catalogue of phone numbers to purchase the ones you would like. The search phone numbers API allows you to filter search results based the type of phone number as well its capability (SMS, VOICE or Both). A description of the REST API method can be found here.

.NET

var client = PhoneNumbersClient.Create();

// search for australian mobile numbers
var results = client.SearchPhoneNumbersAsync(new()
{
    CountryCode = "AU",
    Prefix = "+614"
});

// display all results (using auto-magic pagination)
await foreach (var number in results)
{
    Console.WriteLine(number.PhoneNumber_);
}

Node.js

const PhoneNumbersClient = new phoneNumbers.PhoneNumbersClient();

(async () => {
  try {
    let response = await PhoneNumbersClient.searchPhoneNumbers({
      countryCode: 'AU',
      numberType: 'TOLL_FREE'
    });

    console.log(response);
  } catch (error) {
    console.log(error);
  }
})();

Purchase phone number

Purchase a phone number by creating an instance of it against your given project. One completed the purchased phone number will no longer be returned by the search phone number API. A description of the REST API method can be found here.

An Australian local or mobile number, or a New Zealand local number, needs a regulatory listing: the name and address of the person or business that uses the number. To buy one of these numbers:

  1. Create a regulatory listing in the project, in the Enfonica Console or with the regulatory listings API. For what a listing is and how to create one in the Console, see What is a regulatory listing, and why does my number need one?.
  2. In the request that buys the number, set regulatoryListing on the phone number instance to the listing's name, such as projects/my-project/regulatoryListings/LISTING_ID. The listing must be in the same project as the number.

If the number needs a listing and the request doesn't have a suitable one, the request returns INVALID_ARGUMENT. Regulatory guidelines lists what each type of number needs. The API reference marks regulatoryListing as output only, but the API accepts it in this request.

The examples below don't set a listing. For a number that needs one, add it to the phone number instance: RegulatoryListing in .NET, and regulatoryListing in Node.js.

.NET

var phoneNumbersClient = PhoneNumbersClient.Create();
var phoneNumberInstancesClient = PhoneNumberInstancesClient.Create();

// get the first AU mobile phone number result
var results = phoneNumbersClient.SearchPhoneNumbersAsync(new()
{
    CountryCode = "AU",
    Prefix = "+614"
});
var firstMobile = (await results.ReadPageAsync(1)).First();

// provision
var provisioned = await phoneNumberInstancesClient.CreatePhoneNumberInstanceAsync(new()
{
    Parent = "projects/my-project",
    PhoneNumberInstance = new()
    {
        PhoneNumber = new()
        {
            Name = firstMobile.Name
        }
    }
});
Console.WriteLine($"Provisioned phone number instance {provisioned.Name}");

Node.js

const PhoneNumbersClient = new phoneNumbers.PhoneNumbersClient();
const PhoneNumberInstancesClient = new phoneNumbers.PhoneNumberInstancesClient();

(async () => {
  try {
    let response = await PhoneNumbersClient.searchPhoneNumbers({
      countryCode: 'AU',
      numberType: 'TOLL_FREE'
    });

    if (response[0].length == 0) {
      throw new Error('No numbers are available');
    }
    
    let purchaseNumberResponse = await PhoneNumberInstancesClient.createPhoneNumberInstance({
      parent: 'projects/-',
      phoneNumberInstance: {
        phoneNumber: response[0][0]
      }
    });

    console.log(purchaseNumberResponse);

  } catch (error) {
    console.log(error);
  }
})();

Edit phone number

Once you have a purchased a number you can also update its instance using the api. This will allow you to configure how a call is handled or an incoming message: set incomingCallHandlerUris for calls and incomingMessageHandlerUris for SMS, with up to five URIs in each. You can also enter tags against a phone number if needed. A description of the REST API method can be found here.

A number's labels are copied to each call and message that the number receives, when it arrives. Changing a number's labels doesn't change the calls and messages it has already received. See <Label> in VoiceML and MessageML.

.NET

var phoneNumberInstancesClient = PhoneNumberInstancesClient.Create();

// define the changes we want to make
var changes = new PhoneNumberInstance();
changes.Labels.Add("purpose", "test");

// apply the changes to the specified phone number instance
await phoneNumberInstancesClient.UpdatePhoneNumberInstanceAsync(new()
{
    Name = "projects/my-project/phoneNumberInstances/7sdynf8sydfym7sdnf87ny7dfy8sydf87ysd",
    PhoneNumberInstance = changes
});

Node.js

const PhoneNumberInstancesClient = new phoneNumbers.PhoneNumberInstancesClient();

(async () => {
  try {
    let response = await PhoneNumberInstancesClient.updatePhoneNumberInstance({
      name: 'projects/my-project/phoneNumberInstances/7sdynf8sydfym7sdnf87ny7dfy8sydf87ysd',
      phoneNumberInstance: {
        labels: {
          purpose: 'test'
        }
      },
    });
    console.log(response);
  } catch (error) {
    console.log(error);
  }
})();