Buy a Number
You need to purchase a Nexmo virtual number if you want to:
- Make or receive telephone calls with the Voice API
- Receive inbound SMS with the SMS API
- Use multichannel messaging with the Messages API
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
:
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}"
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:
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 buy.js
:
nexmo.number.buy(COUNTRY_CODE, NEXMO_NUMBER, (err, res) => {
if (err) {
console.error(err)
}
else {
console.log(JSON.stringify(res, null, 2))
}
})
Run your code
Save this file to your machine and run it:
node buy.js
Prerequisites
Install dependencies
Add the following to `build.gradle`:
compile 'com.nexmo:client:5.1.0'
Initialize your dependencies
Create a class named BuyNumber
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 BuyNumber
class:
NumbersClient numbersClient = client.getNumbersClient();
numbersClient.buyNumber(COUNTRY_CODE, NEXMO_NUMBER);
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 BuyNumber
:
gradle run -Pmain=com.nexmo.quickstart.numbers.BuyNumber
Prerequisites
Install dependencies
Install-Package Nexmo.Csharp.Client
Initialize your dependencies
Create a file named NumbersController.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 NumbersController.cs
:
var result = Client.Number.Buy(COUNTRY_CODE, NEXMO_NUMBER);
Prerequisites
Install dependencies
composer require nexmo/client
Initialize your dependencies
Create a file named purchase.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 purchase.php
:
try {
$client->numbers()->purchase(NEXMO_NUMBER, COUNTRY_CODE);
echo "Number purchased";
} catch (Exception $e) {
echo "Error purchasing number";
}
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:
import nexmo
client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)
Write the code
Add the following to buy.py
:
try:
response = client.buy_number({"country": COUNTRY_CODE, "msisdn": NEXMO_NUMBER})
print("Number purchased")
except Exception as exc:
print("Error purchasing number", exc)
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:
client = Nexmo::Client.new(
api_key: NEXMO_API_KEY,
api_secret: NEXMO_API_SECRET
)
Write the code
Add the following to buy.rb
:
begin
response = client.numbers.buy(
country: COUNTRY_CODE,
msisdn: NEXMO_NUMBER
)
puts "Number purchased"
rescue
puts "Error purchasing number"
end
Run your code
Save this file to your machine and run it:
ruby buy.rb