List Your Numbers

This page shows you how to list the numbers that you own programmatically.

You can also view your numbers online, using the developer dashboard or from the command line, using the Nexmo CLI.

Replace the following variables in the sample code with your own values:

Name Description
VONAGE_API_KEY Your Vonage API key
VONAGE_API_SECRET Your Vonage API secret
NUMBER_SEARCH_CRITERIA The filter criteria. For example, numbers containing 234.
NUMBER_SEARCH_PATTERN Where the NUMBER_SEARCH_CRITERIA should appear in the number:
  • 0 - At the beginning of the number
  • 1- Anywhere in the number
  • 2 - At the end of the number

Write the code

Add the following to list-numbers.sh:

Copy to Clipboard
curl "https://rest.nexmo.com/account/numbers?api_key=$NEXMO_API_KEY&api_secret=$NEXMO_API_SECRET&pattern=$NUMBER_SEARCH_CRITERIA&search_pattern=$NUMBER_SEARCH_PATTERN"

View full source

Run your code

Save this file to your machine and run it:

sh list-numbers.sh

Prerequisites

Install dependencies

npm install @vonage/server-sdk
Initialize your dependencies

Create a file named list.js and add the following code:

Copy to Clipboard
const Vonage = require('@vonage/server-sdk')

const vonage = new Vonage(
  {
    apiKey: VONAGE_API_KEY,
    apiSecret: VONAGE_API_SECRET
  },
  {
    debug: true
  }
)

View full source

Write the code

Add the following to list.js:

Copy to Clipboard
vonage.number.get(
  {
    pattern: NUMBER_SEARCH_CRITERIA,
    search_pattern: NUMBER_SEARCH_PATTERN
  },
  (err, res) => {
    if (err) {
      console.error(err)
    } else {
      console.log(`Here are ${res.numbers.length} of your ${res.count} matching numbers:`)
      res.numbers.forEach((number) => {
        console.log(`Tel: ${number.msisdn} Cost: ${number.type}`)
      })
    }
  }
)

View full source

Run your code

Save this file to your machine and run it:

node list.js

Prerequisites

Install dependencies

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'
Initialize your dependencies

Create a class named ListNumbers and add the following code to the main method:

Copy to Clipboard
VonageClient client = VonageClient.builder()
        .apiKey(VONAGE_API_KEY)
        .apiSecret(VONAGE_API_SECRET)
        .build();

View full source

Write the code

Add the following to the main method of the ListNumbers class:

Copy to Clipboard
NumbersClient numbersClient = client.getNumbersClient();

ListNumbersResponse response = numbersClient.listNumbers(
        new ListNumbersFilter(1, 10, NUMBER_SEARCH_CRITERIA, NUMBER_SEARCH_PATTERN)
);

for (OwnedNumber number : response.getNumbers()) {
    System.out.println("Tel: " + number.getMsisdn());
    System.out.println("Type: " + number.getType());
    System.out.println("Country: " + number.getCountry());
}

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.numbers with the package containing ListNumbers:

gradle run -Pmain=com.vonage.quickstart.numbers.ListNumbers

Prerequisites

Install dependencies

Install-Package Nexmo.Csharp.Client
Import dependencies

Create a file named ListOwnedNumbers.cs and add the following code:

Copy to Clipboard
using Vonage;
using Vonage.Numbers;
using Vonage.Request;

View full source

Initialize your dependencies

Add the following to ListOwnedNumbers.cs:

Copy to Clipboard
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to ListOwnedNumbers.cs:

Copy to Clipboard
var request = new NumberSearchRequest() 
{ 
    SearchPattern = NUMBER_SEARCH_PATTERN, 
    Pattern = NUMBER_SEARCH_CRITERIA 
};

var response = client.NumbersClient.GetOwnedNumbers(request);

View full source

Prerequisites

Install dependencies

composer require vonage/client
Initialize your dependencies

Create a file named list-owned.php and add the following code:

Copy to Clipboard
$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);

View full source

Write the code

Add the following to list-owned.php:

Copy to Clipboard
try {
    $filter = new \Vonage\Numbers\Filter\OwnedNumbers();
    $filter
        ->setPattern((int) NUMBER_SEARCH_CRITERIA)
        ->setSearchPattern((int) NUMBER_SEARCH_PATTERN)
    ;
    $response = $client->numbers()->searchOwned($filter);

    echo count($response). " of your numbers match:\n";

    foreach($response as $number) {
        echo "Tel: " . $number->getMsisdn() . " Type: " . $number->getType() . "\n";
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

View full source

Run your code

Save this file to your machine and run it:

php list-owned.php

Prerequisites

Install dependencies

pip install vonage
Initialize your dependencies

Create a file named list.py and add the following code:

Copy to Clipboard
import vonage

client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)

View full source

Write the code

Add the following to list.py:

Copy to Clipboard
responseData = client.get_account_numbers(
    {"pattern": NUMBER_SEARCH_CRITERIA, "search_pattern": NUMBER_SEARCH_PATTERN}
)

print(
    f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers in your account:'
)

for number in responseData["numbers"]:
    print(f'Tel: {number["msisdn"]} Type: {number["type"]}')

View full source

Run your code

Save this file to your machine and run it:

python list.py

Prerequisites

Install dependencies

gem install vonage
Initialize your dependencies

Create a file named list.rb and add the following code:

Copy to Clipboard
client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

View full source

Write the code

Add the following to list.rb:

Copy to Clipboard
begin
  response = client.numbers.list()

  puts "Here are #{response.numbers.length} of your #{response.count} matching numbers:"
  
  response.numbers.each do |number|
    puts "Tel: #{number.msisdn} Type: #{number.type}"
  end
rescue
  puts "Error listing your owned numbers"
end 

View full source

Run your code

Save this file to your machine and run it:

ruby list.rb

See also