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 |
---|---|
NEXMO_API_KEY |
Your Nexmo API key |
NEXMO_API_SECRET |
Your Nexmo 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:
|
Write the code
Add the following to list-numbers.sh
:
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"
Run your code
Save this file to your machine and run it:
sh list-numbers.sh
Prerequisites
Install dependencies
npm install nexmo
Initialize your dependencies
Create a file named list.js
and add the following code:
const Nexmo = require('nexmo')
const nexmo = new Nexmo(
{
apiKey: NEXMO_API_KEY,
apiSecret: NEXMO_API_SECRET
},
{
debug: true
}
)
Write the code
Add the following to list.js
:
nexmo.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}`)
})
}
}
)
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.nexmo:client:5.1.0'
Initialize your dependencies
Create a class named ListNumbers
and add the following code to the main
method:
NexmoClient client = NexmoClient.builder()
.apiKey(NEXMO_API_KEY)
.apiSecret(NEXMO_API_SECRET)
.build();
Write the code
Add the following to the main
method of the ListNumbers
class:
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());
}
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.nexmo.quickstart.numbers
with the package containing ListNumbers
:
gradle run -Pmain=com.nexmo.quickstart.numbers.ListNumbers
Prerequisites
Install dependencies
Install-Package Nexmo.Csharp.Client
Initialize your dependencies
Create a file named NumberSender.cs
and add the following code:
const string API_KEY = "NEXMO_API_KEY";
const string API_SECRET = "NEXMO_API_SECRET";
var client = new Client(creds: new Nexmo.Api.Request.Credentials(
nexmoApiKey: API_KEY, nexmoApiSecret: API_SECRET));
Write the code
Add the following to NumberSender.cs
:
var response = client.Number.ListOwnNumbers(new Number.SearchRequest()
{
pattern = NUMBER_SEARCH_CRITERIA,
search_pattern = NUMBER_SEARCH_PATTERN
});
Prerequisites
Install dependencies
composer require nexmo/client
Initialize your dependencies
Create a file named list-owned.php
and add the following code:
$basic = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client($basic);
Write the code
Add the following to list-owned.php
:
$response = $client->numbers()->searchOwned(
NUMBER_SEARCH_CRITERIA,
[
"search_pattern" => NUMBER_SEARCH_PATTERN,
]
);
echo count($response). " of your numbers match:\n";
foreach($response as $number) {
echo "Tel: " . $number->getMsisdn() . " Type: " . $number->getType() . "\n";
}
Run your code
Save this file to your machine and run it:
php list-owned.php
Prerequisites
Install dependencies
pip install nexmo
Initialize your dependencies
Create a file named list.py
and add the following code:
import nexmo
client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)
Write the code
Add the following to list.py
:
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"]}')
Run your code
Save this file to your machine and run it:
python list.py
Prerequisites
Install dependencies
gem install nexmo
Initialize your dependencies
Create a file named list.rb
and add the following code:
client = Nexmo::Client.new(
api_key: NEXMO_API_KEY,
api_secret: NEXMO_API_SECRET
)
Write the code
Add the following to list.rb
:
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
Run your code
Save this file to your machine and run it:
ruby list.rb