Number Insight Basic API

You use Number Insight Basic API to retrieve local and international representations of a phone number in order to pretty-print numbers in a user interface. Number Insight Basic API performs semantic checks on phone numbers. This helps you decide if you accept the phone number entered by your user, or ask them to check and correct it.

Number Insight Basic API is a free synchronous, easy-to-use web service. For any phone number you can:

  • Retrieve the international and local format.
  • Know the country it is registered in.

The workflow for Number Insight Basic API is:

workflow_number_insight_basic_api
  1. Request information about a phone number.
  2. Receive a response and see the information you requested.

Implementing the Number Insight Basic API workflow

To use Number Insight Basic API:

  1. Request information about a phone number.
    <?php
    $url = 'https://api.nexmo.com/ni/basic/json?' . http_build_query(
        [
            'api_key' => 'API_KEY',
            'api_secret' => 'API_SECRET',
            'number' => '441632960960'
        ]
    );
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    import urllib
    import urllib2
    
    params = {
        'api_key': 'API_KEY',
        'api_secret': 'API_SECRET',
        'number': '441632960960'
    }
    
    url = 'https://api.nexmo.com/ni/basic/json?' + urllib.urlencode(params)
    
    request = urllib2.Request(url)
    request.add_header('Accept', 'application/json')
    response = urllib2.urlopen(request)
    
    require "net/http"
    require "uri"
    require 'json'
    
    uri = URI.parse("https://api.nexmo.com/ni/basic/json")
    params = {
        "api_key" => 'API_KEY',
        "api_secret" => 'API_SECRET',
        "number" => '441632960960'
    }
    
    response = Net::HTTP.post_form(uri, params)
    
    var https = require('https');
    
    var data = JSON.stringify({
     api_key: 'API_KEY',
     api_secret: 'API_SECRET',
     number: '441632960960',
    
    });
    
    var options = {
     host: 'api.nexmo.com',
     path: '/ni/basic/json',
     port: 443,
     method: 'POST',
     headers: {
       'Content-Type': 'application/json',
       'Content-Length': Buffer.byteLength(data)
     }
    };
    
    var req = https.request(options);
    
    req.write(data);
    req.end();
    
    var responseData = '';
    req.on('response', function(res){
     res.on('data', function(chunk){
       responseData += chunk;
     });
    
     res.on('end', function(){
       console.log(JSON.parse(responseData));
     });
    });
    
  2. Receive a response and see the information you requested.
    <?php
    //Decode the json object you retrieved when you ran the request.
    $decoded_response = json_decode($response, true);
    
    if ($decoded_response['status'] == 0) {
        error_log("Success " . $decoded_response['request_id']);
        error_log($decoded_response['international_format_number']
        . ' is in ' . $decoded_response['country_name'] );
    } else {
        error_log("Error " . $decoded_response['status']
      . ": " . $decoded_response['status_message']);
    }
    
    import json
    
    #Using the response object from the request
    if response.code == 200 :
        data = response.read()
        #Decode JSON response from UTF-8
        decoded_response = json.loads(data.decode('utf-8'))
        # Check if your messages are succesful
        if decoded_response['status'] == 0:
            print "Success " + decoded_response['request_id']
            print decoded_response['international_format_number'] \
                + " is in " + decoded_response['country_name']
        else:
            print "Error " + str( decoded_response["status"]) + ': ' \
                + decoded_response['status_message']
    else :
        #Check the errors
        print "unexpected http {code} response from nexmo api". response.code
    
    
    
    #Decode the json object from the response object you retrieved from the request.
    if response.kind_of? Net::HTTPSuccess
        decoded_response = JSON.parse(response.body )
    
        if decoded_response["status"] == 0
          p "Success" + decoded_response["request_id"] + "\n"
            p decoded_response['international_format_number']  \
                    + ' is in ' + decoded_response['country_name']
        else
            p "message has error " + decoded_response["status"].to_s  \
                    + " " + decoded_response["status_message"]
        end
    else
      puts response.code + " error sending message"
    end
    
    //Decode the json object you retrieved when you ran the request.
    
    var decoded_response = JSON.parse(responseData);
    
    if (decoded_response.status === 0) {
      console.log('Success ' + decoded_response.request_id );
      console.log( decoded_response.international_format_number
        + ' is in ' + decoded_response.country_name );
     } else {
        console.log('Error ' + decoded_response.status
         + ': ' +  decoded_response.status_message );
    }
    
Previous   Next