Number Insight Advanced Webhook
This code snippet shows you how to code the webhook handler that receives the data returned by an asynchronous call to the Number Insight Advanced API. See the Number Insight Advanced code snippet to learn how to code the initial request for the insight data.
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. |
Prerequisites
npm install express body-parser
Create a file named ni-advanced-async.js
and add the following code:
const app = require('express')()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
Write the code
Add the following to ni-advanced-async.js
:
app.post('/webhooks/insight', handleInsight)
function handleInsight(request, response) {
console.log("params", Object.assign(request.query, request.body))
response.status(204).send()
}
app.listen(3000)
Run your code
Save this file to your machine and run it:
node ni-advanced-async.js
Prerequisites
Add the following to `build.gradle`:
compile 'com.vonage:client:6.2.0'
compile 'com.sparkjava:spark-core:2.7.2'
Write the code
Add the following to the main
method of the AsyncInsightTrigger
class:
port(3000);
Spark.post("/webhooks/insight", (req, res) -> {
AdvancedInsightResponse response = AdvancedInsightResponse.fromJson(req.body());
System.out.println("Country: " + response.getCountryName());
res.status(204);
return "";
});
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 AsyncInsightTrigger
:
gradle run -Pmain=com.vonage.quickstart.insight.AsyncInsightTrigger
Prerequisites
Install-Package Vonage
Create a file named NumberInsightsController.cs
and add the following code:
using Vonage.Utility;
using System;
Write the code
Add the following to NumberInsightsController.cs
:
var insights = await WebhookParser.ParseWebhookAsync<AdvancedInsightsResponse>
(Request.Body, Request.ContentType);
Console.WriteLine($"Advanced insights received: {insights.RequestId} " +
$"that number's carrier is {insights.CurrentCarrier.Name} " +
$"and it's ported status is: {insights.Ported}");
return NoContent();
}
}
}
Prerequisites
composer require vonage/client
Write the code
Add the following to index.php
:
$handler = function (Request $request, Response $response) {
$params = $request->getParsedBody();
error_log($params['status_message']);
error_log($params['country_code']);
error_log($params['current_carrier']['name']);
return $response->withStatus(204);
};
Run your code
Save this file to your machine and run it:
php index.php
Prerequisites
pip install vonage python-dotenv flask
Create a file named app.py
and add the following code:
from flask import Flask, request
Write the code
Add the following to app.py
:
@app.route("/", methods=['GET', 'POST'])
def callback():
print(request.get_json())
return "Hello World"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=4000, debug=True)
Run your code
Save this file to your machine and run it:
python app.py
Prerequisites
gem install sinatra sinatra/multi-route json
Write the code
Add the following to ni-advanced-async.rb
:
require 'sinatra'
require 'sinatra/multi_route'
require 'json'
helpers do
def parsed_body
json? ? JSON.parse(request.body.read) : {}
end
def json?
request.content_type == 'application/json'
end
end
route :post, '/webhooks/insight' do
puts params.merge(parsed_body)
status 200
end
set :port, 3000
Run your code
Save this file to your machine and run it:
ruby ni-advanced-async.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