Skip to content

Phone Numbers

Enfonica's virtual phone numbers gives you instant access to local, mobile and toll-free numbers for your voice call and sms applications. You can even use your own number as well by porting them to us.

With the Phone Numbers APIs, you can search for and purchase available Enfonica numbers and manage them.

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 all available fields is available in the public interface definitions.

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_);
}
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 all available fields is available in the public interface definitions.

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}");
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. You can also enter tags against a phone number if needed. A description of all available fields is available in the public interface definitions.

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
});
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);
  }
})();