Buy a Number

You need to purchase a Nexmo virtual number if you want to:

This page shows you how to buy a number programmatically.

You can also cancel a number 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 The Nexmo virtual number you want to cancel. Omit the leading zero but include the international dialing code. For example: 447700900000.

Write the code

Add the following to buy-number.sh:

Copy to Clipboard
curl -X POST \
  "https://rest.nexmo.com/number/buy?api_key=$NEXMO_API_KEY&api_secret=$NEXMO_API_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "country=${COUNTRY_CODE}&msisdn=${NEXMO_NUMBER}"

View full source

Run your code

Save this file to your machine and run it:

sh buy-number.sh

Prerequisites

Install dependencies

npm install nexmo
Initialize your dependencies

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

Copy to Clipboard
const Nexmo = require('nexmo')

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

View full source

Write the code

Add the following to buy.js:

Copy to Clipboard
nexmo.number.buy(COUNTRY_CODE, NEXMO_NUMBER, (err, res) => {
  if (err) {
    console.error(err)
  }
  else {
    console.log(JSON.stringify(res, null, 2))
  }
})

View full source

Run your code

Save this file to your machine and run it:

node buy.js

Prerequisites

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named purchase.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 purchase.php:

Copy to Clipboard
try {
    $client->numbers()->purchase(NEXMO_NUMBER, COUNTRY_CODE);
    echo "Number purchased";
} catch (Exception $e) {
    echo "Error purchasing number";
}

View full source

Run your code

Save this file to your machine and run it:

php purchase.php

Prerequisites

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named buy.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 buy.py:

Copy to Clipboard
try:
    response = client.buy_number({"country": COUNTRY_CODE, "msisdn": NEXMO_NUMBER})
    print("Number purchased")
except Exception as exc:
    print("Error purchasing number", exc)

View full source

Run your code

Save this file to your machine and run it:

python buy.py

Prerequisites

Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named buy.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 buy.rb:

Copy to Clipboard
begin
  response = client.numbers.buy(
    country: COUNTRY_CODE,
    msisdn: NEXMO_NUMBER
  )
  puts "Number purchased"
rescue
  puts "Error purchasing number"
end

View full source

Run your code

Save this file to your machine and run it:

ruby buy.rb

See also