Search Available Numbers

This page shows you how to programmatically search for numbers that are available for purchase.

You can also search for available 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
NEXMO_API_KEY Your Nexmo API key
NEXMO_API_SECRET Your Nexmo API secret
COUNTRY_CODE The two digit country code for the number you want to cancel. For example: GB for the United Kingdom.
NEXMO_NUMBER_TYPE The type of number: landline, mobile-lvn or landline-toll-free
NEXMO_NUMBER_FEATURES The capabilities of the number: SMS, VOICE or SMS,VOICE for both
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 search-numbers.sh:

Copy to Clipboard
curl "https://rest.nexmo.com/number/search?api_key=$NEXMO_API_KEY&api_secret=$NEXMO_API_SECRET&country=$COUNTRY_CODE&type=$NEXMO_NUMBER_TYPE&features=$NEXMO_NUMBER_FEATURES&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 search-numbers.sh

Prerequisites

Install dependencies

npm install nexmo
Initialize your dependencies

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

Copy to Clipboard
const nexmo = new Nexmo(
  {
    apiKey: NEXMO_API_KEY,
    apiSecret: NEXMO_API_SECRET
  },
  {
    debug: true
  }
)

View full source

Write the code

Add the following to search.js:

Copy to Clipboard
nexmo.number.search(
  COUNTRY_CODE,
  {
    type: NEXMO_NUMBER_TYPE,
    pattern: NUMBER_SEARCH_CRITERIA,
    search_pattern: NUMBER_SEARCH_PATTERN,
    features: NEXMO_NUMBER_FEATURES
  },
  (err, res) => {
    if (err) {
      console.error(err)
    }
    else {
      console.log(`Here are ${res.numbers.length} of the ${res.count} matching numbers available for purchase:`)
      res.numbers.forEach((number) => {
        console.log(`Tel: ${number.msisdn} Cost: ${number.cost}`)
      })
    }
  }
)

View full source

Run your code

Save this file to your machine and run it:

node search.js

Prerequisites

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named search-available.php and add the following code:

Copy to Clipboard
$basic = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client($basic);

View full source

Write the code

Add the following to search-available.php:

Copy to Clipboard
$response = $client->numbers()->searchAvailable(
    COUNTRY_CODE,
    [
        "pattern" => NUMBER_SEARCH_CRITERIA,
        "search_pattern" => NUMBER_SEARCH_PATTERN,
        "type" => NEXMO_NUMBER_TYPE,
        "features" => NEXMO_NUMBER_FEATURES,
    ]
);

echo "There are " . count($response) . " matching numbers available for purchase:\n";

foreach ($response as $number) {
    echo "Tel: " . $number->getMsisdn() . " Cost: " . $number->getCost() . "\n";
}

View full source

Run your code

Save this file to your machine and run it:

php search-available.php

Prerequisites

Install dependencies

pip install nexmo
Initialize your dependencies

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

Copy to Clipboard
import nexmo

client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)

View full source

Write the code

Add the following to search.py:

Copy to Clipboard
responseData = client.get_available_numbers(
    COUNTRY_CODE,
    {
        "pattern": NUMBER_SEARCH_CRITERIA,
        "search_pattern": NUMBER_SEARCH_PATTERN,
        "type": NEXMO_NUMBER_TYPE,
        "features": NEXMO_NUMBER_FEATURES,
    },
)

print(
    f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers available for purchase:'
)

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

View full source

Run your code

Save this file to your machine and run it:

python search.py

Prerequisites

Install dependencies

gem install nexmo
Initialize your dependencies

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

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET
)

View full source

Write the code

Add the following to search.rb:

Copy to Clipboard
begin
  response = client.numbers.search(
    country: COUNTRY_CODE,
    msisdn: NEXMO_NUMBER,
    type: NEXMO_NUMBER_TYPE,
    features: NEXMO_NUMBER_FEATURES,
    pattern: NUMBER_SEARCH_CRITERIA,
    search_pattern: NUMBER_SEARCH_PATTERN
  )

  puts "Your search returned #{response.numbers.length} of the #{response.count} matching numbers available for purchase:"

  response.numbers.each do |number|
    puts "Tel: #{number.msisdn} Cost: #{number.cost}"
  end
rescue
  puts "Error searching numbers"
end

View full source

Run your code

Save this file to your machine and run it:

ruby search.rb

See also