Buy a Number
You need to purchase a Vonage 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 Vonage CLI.
Replace the following variables in the sample code with your own values:
Key | Description |
---|---|
VONAGE_API_KEY |
Your Vonage API key (see it on your dashboard). |
VONAGE_API_SECRET |
Your Vonage API secret (also available on your dashboard). |
COUNTRY_CODE |
The two digit country code for the number. For example: GB for the United Kingdom |
VONAGE_NUMBER |
Your Vonage Number. E.g. 447700900000
|
Write the code
Add the following to buy-number.sh
:
curl -X POST \
"https://rest.nexmo.com/number/buy?api_key=$VONAGE_API_KEY&api_secret=$VONAGE_API_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "country=${COUNTRY_CODE}&msisdn=${VONAGE_NUMBER}"
Run your code
Save this file to your machine and run it:
sh buy-number.sh
Prerequisites
npm install @vonage/server-sdk
Create a file named buy.js
and add the following code:
const Vonage = require('@vonage/server-sdk')
const vonage = new Vonage(
{
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET
},
{
debug: true
}
)
Write the code
Add the following to buy.js
:
vonage.number.buy(COUNTRY_CODE, VONAGE_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
Add the following to `build.gradle`:
compile 'com.vonage:client:6.2.0'
Create a class named BuyNumber
and add the following code to the main
method:
VonageClient client = VonageClient.builder()
.apiKey(VONAGE_API_KEY)
.apiSecret(VONAGE_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, VONAGE_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.vonage.quickstart.numbers
with the package containing BuyNumber
:
gradle run -Pmain=com.vonage.quickstart.numbers.BuyNumber
Prerequisites
Install-Package Vonage
Create a file named BuyNumber.cs
and add the following code:
using Vonage;
using Vonage.Numbers;
using Vonage.Request;
Add the following to BuyNumber.cs
:
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);
Write the code
Add the following to BuyNumber.cs
:
var request = new NumberTransactionRequest() { Country = COUNTRY_CODE, Msisdn = VONAGE_NUMBER };
var response = client.NumbersClient.BuyANumber(request);
Prerequisites
composer require vonage/client
Create a file named purchase.php
and add the following code:
$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);
Write the code
Add the following to purchase.php
:
try {
$client->numbers()->purchase(VONAGE_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
pip install vonage
Create a file named buy.py
and add the following code:
import vonage
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
Write the code
Add the following to buy.py
:
try:
response = client.buy_number({"country": COUNTRY_CODE, "msisdn": VONAGE_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
gem install vonage
Create a file named buy.rb
and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET
)
Write the code
Add the following to buy.rb
:
begin
response = client.numbers.buy(
country: COUNTRY_CODE,
msisdn: VONAGE_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