Phone Numbers¶
Enfonica Phone Numbers gives you instant access to local, mobile and toll-free numbers for your voice and SMS applications.
Phone numbers in our inventory go through a rigorous screening process, to ensure that the phone numbers are clean and do not receive any unwanted traffic.
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:
- Your access token (a bearer token).
- Your project name (e.g.
projects/my-project
).
To use the Enfonica Client Library, you will need:
- 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.
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 the REST API method can be found here.
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 the REST API method can be found here.
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);
}
})();