Nexmo Number Insight API GraphQ

by nexmo-community

GitHub Readme.md

Number Insight API GraphQL Example

Deploy Deploy to now

Nexmo's Number Insight API is a great way to get more information about numbers that you have stored in your system.

This repository contains an example GraphQL server for retreiving information from the Number Insight API, it will work with the basic, standard and advanced levels of information retrieval.

The server is written in Node.js and uses the Apollo GraphQL Server.

Installation

Clone the repository and run the following command to install the project dependencies:

npm install

Note: Dependencies are apollo-server, graphql, apollo-datasource-rest and config

API Key & Secret

You will need a Nexmo API Key and API Secret. If you don't have these credentials then sign up for a Nexmo account to get them.

config expects credentials to be found at this path ./config/default.json so create this file first.

Set your default.json file up like this:

{
  "nexmo_api_key": "<your api key>",
  "nexmo_api_secret": "<your api secret>",
  "graphql_playground": true,
  "graphql_introspection": true
}

Starting The Server

To start the server locally run this command:

npm start

The server will launch on port 4000. If you need to change this port then you can by modifying line 24 of server.js.

Running Queries

You are free to run queries against this server however you like, but the best place to start is by using the interactive query editor provided by the Apollo GraphQL Server.

You can launch this with your server running by going to http://localhost:4000 in your browser.

Example Query - Basic

Here is a basic query to start with:

query {
  insight(number: "<enter your number here>") {
    country_name
  }
}

This will return a JSON response similar to this:

{
  "data": {
    "insight": {
      "country_name": "United Kingdom"
    }
  }
}

Example Query - Standard

To retrieve standard level insight (which cost slightly more), run your queries with an additional type parameter in the request.

For example:

query {
  insight(number: "<enter your number here>", type: "standard") {
    country_name
    current_carrier {
      name
    }
  }
}

This will return a JSON response similar to this:

{
  "data": {
    "insight": {
      "country_name": "United Kingdom",
      "current_carrier": {
        "name": "Telefonica UK Limited"
      }
    }
  }
}

The full list of available parameters from the Number Insight API, and at what level you can access them, can be found in the API Reference.

Deploying The Server

The quickest way to deploy this to a production server would be to use the one-click deploy buttons at the top of this readme. They will both as you to set your NEXMO_API_KEY and NEXMO_API_SECRET as part of the deployment.

If deploying to your own server, ensure that these environment variables are also set.

GraphQL Playground and Introspection in Production

By default, introspection and the GraphQL playground are not available when the NODE_ENV on a server is set to production. If you do want to use either of these options once you have deployed the server make sure to set the GRAPHQL_INTROSPECTION and GRAPHQL_PLAYGROUND to true in your environment variables.