Number Insight Advanced Async Callback

You can optionally use the Number Insight Advanced API asynchronously to return the insight data when it becomes available, via a webhook.

Note that the Basic and Standard API levels do no offer this feature.

This building block shows you how to code the webhook handler that deals with the data returned by an asynchronous call to the Number Insight Advanced API. See the Number Insight Advanced Async Trigger building block to learn how to code the initial request for the insight data.

Before attempting to run the code examples, replace the variable placeholders as instructed in replaceable variables.

Prerequisites

Install dependencies

$ npm install nexmo express body-parser

Write the code

Add the following to ni-advanced-async.js:

Copy to Clipboard
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)

View full source

Run your code

Save this file to your machine and run it:

$ node ni-advanced-async.js

Prerequisites

Install dependencies

$ gem install sinatra sinatra/multi-route json

Write the code

Add the following to ni-advanced-async.rb:

Copy to Clipboard
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

View full source

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