Number Insight Advanced Async Trigger

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 trigger an asynchronous call to the Number Insight API.

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

Write the code

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

Copy to Clipboard
curl -X "POST" "https://api.nexmo.com/ni/advanced/async/json" \
     -d "api_key=$NEXMO_API_KEY" \
     -d "api_secret=$NEXMO_API_SECRET" \
     -d "number=$INSIGHT_NUMBER" \
     -d "callback=$WEBHOOK_URL"

View full source 

Run your code

Save this file to your machine and run it:

$ sh ni-advanced-async.sh

Prerequisites

Install dependencies

$ npm install nexmo express body-parser

Write the code

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

Copy to Clipboard
const Nexmo = require('nexmo');

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET
});

nexmo.numberInsight.get({
  level: 'advancedAsync',
  number: INSIGHT_NUMBER,
  callback: 'https://demo.ngrok.io/webhooks/insight'
}, (error, result) => {
  if (error) {
    console.error(error);
  }
  else {
    console.log(result);
  }
});

View full source 

Run your code

Save this file to your machine and run it:

$ node ni-advanced-async-client.js

Prerequisites

Install dependencies

$ gem install nexmo

Write the code

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

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET
)

insight = client.number_insight.advanced(
  number: INSIGHT_NUMBER,
  callback: 'https//demo.ngrok.io/webhooks/insight'
)

puts insight.inspect

View full source 

Run your code

Save this file to your machine and run it:

$ ruby ni-advanced-async-client.rb

The API acknowledges the request by returning the following information to the client:

{
    "request_id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
    "number": "447700900000",
    "remaining_balance": "10.000000",
    "request_price": "0.03000000",
    "status": 0
}

When it becomes available, the Advanced Number Insight API data is sent to the specified webhook via a POST request. See the Number Insight Advanced Async Callback building block to learn how to code the webhook handler.