这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。
Number Insight Advanced (Synchronous)
This code snippet shows you how to use Number Insight Advanced API synchronously.
Note: Vonage does not recommend this approach, because it can result in timeouts. In most cases, you should use an asynchronous call to the Number Insight API.
Before attempting to run the code examples, replace the variable placeholders:
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). |
INSIGHT_NUMBER |
The number you want to retrieve insight information for. |
Write the code
Add the following to ni-advanced.sh
:
source "../config.sh"
curl "https://api.nexmo.com/ni/advanced/json?api_key=$VONAGE_API_KEY&api_secret=$VONAGE_API_SECRET&number=$INSIGHT_NUMBER"
Run your code
Save this file to your machine and run it:
sh ni-advanced.sh
Prerequisites
npm install @vonage/server-sdk
Create a file named ni-advanced.js
and add the following code:
const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET
});
Write the code
Add the following to ni-advanced.js
:
vonage.numberInsight.get({level: 'advancedSync', number: INSIGHT_NUMBER}, (error, result) => {
if(error) {
console.error(error);
}
else {
console.log(result);
}
});
Run your code
Save this file to your machine and run it:
node ni-advanced.js
Prerequisites
Add the following to `build.gradle`:
compile 'com.vonage:client:5.5.0'
Create a class named AdvancedInsight
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 AdvancedInsight
class:
AdvancedInsightResponse response = client.getInsightClient().getAdvancedNumberInsight(INSIGHT_NUMBER);
System.out.println("BASIC INFO:");
System.out.println("International format: " + response.getInternationalFormatNumber());
System.out.println("National format: " + response.getNationalFormatNumber());
System.out.println("Country: " + response.getCountryName() + " (" + response.getCountryCodeIso3() + ", +"
+ response.getCountryPrefix() + ")");
System.out.println();
System.out.println("STANDARD INFO:");
System.out.println("Current carrier: " + response.getCurrentCarrier().getName());
System.out.println("Original carrier: " + response.getOriginalCarrier().getName());
System.out.println();
System.out.println("ADVANCED INFO:");
System.out.println("Validity: " + response.getValidNumber());
System.out.println("Reachability: " + response.getReachability());
System.out.println("Ported status: " + response.getPorted());
RoamingDetails roaming = response.getRoaming();
if (roaming == null) {
System.out.println("- No Roaming Info -");
} else {
System.out.println("Roaming status: " + roaming.getStatus());
if (response.getRoaming().getStatus() == RoamingDetails.RoamingStatus.ROAMING) {
System.out.print(" Currently roaming in: " + roaming.getRoamingCountryCode());
System.out.println(" on the network " + roaming.getRoamingNetworkName());
}
}
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.insight
with the package containing AdvancedInsight
:
gradle run -Pmain=com.vonage.quickstart.insight.AdvancedInsight
Prerequisites
Install-Package Vonage
Create a file named AdvancedSync.cs
and add the following code:
using Vonage;
using Vonage.NumberInsights;
using Vonage.Request;
Add the following to AdvancedSync.cs
:
var creds = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(creds);
Write the code
Add the following to AdvancedSync.cs
:
var request = new AdvancedNumberInsightRequest() { Number = INSIGHT_NUMBER};
var response = client.NumberInsightClient.GetNumberInsightAdvanced(request);
Prerequisites
composer require vonage/client
Write the code
Add the following to advanced.php
:
$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);
$insights = $client->insights()->advanced(INSIGHT_NUMBER);
Run your code
Save this file to your machine and run it:
php advanced.php
Prerequisites
pip install vonage pprint
Write the code
Add the following to ni-advanced.py
:
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
insight_json = client.get_advanced_number_insight(number=INSIGHT_NUMBER)
pprint(insight_json)
Run your code
Save this file to your machine and run it:
python ni-advanced.py
Prerequisites
gem install vonage
Create a file named ni-advanced.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 ni-advanced.rb
:
insight = client.number_insight.advanced(
number: INSIGHT_NUMBER
)
puts insight.inspect
Run your code
Save this file to your machine and run it:
ruby ni-advanced.rb
The response from the API contains the following data:
{
"status": 0,
"status_message": "Success",
"lookup_outcome": 0,
"lookup_outcome_message": "Success",
"request_id": "75fa272e-4743-43f1-995e-a684901222d6",
"international_format_number": "447700900000",
"national_format_number": "07700 900000",
"country_code": "GB",
"country_code_iso3": "GBR",
"country_name": "United Kingdom",
"country_prefix": "44",
"request_price": "0.03000000",
"remaining_balance": "10.000000",
"current_carrier": {
"network_code": "23420",
"name": "Hutchison 3G Ltd",
"country": "GB",
"network_type": "mobile"
},
"original_carrier": {
"network_code": "23410",
"name": "Telefonica UK Limited",
"country": "GB",
"network_type": "mobile"
},
"valid_number": "valid",
"reachable": "reachable",
"ported": "ported",
"roaming": { "status": "not_roaming" }
}
For a description of each returned field and to see all possible values, see the Number Insights API documentation