Node.js API Wrapper

The Flowroute Node.js API Wrapper provides methods for interacting with Number Management v1 and Messaging v2 of the Flowroute API.

Requirements


Numbers SDK (v1)

View Repo

Installation

After cloning the flowroute-numbers-nodejs repository, go to the flowroute-numbers-nodejs directory. Once Node.js is installed, install the request package. From a terminal window, run:

npm install request

            

Usage

In Flowroute's approach to building the Numbers (v1) API wrapper, HTTP requests are handled by controllers, which contain the methods used to perform tasks with the Node.js SDK.

Controllers

  • PurchasablePhoneNumbersController

    Contains all of the methods necessary to search through Flowroute's phone number inventory.

  • TelephoneNumbersController

    Contains all of the methods necessary to purchase a new phone number and manage your owned phone number inventory.

  • InboundRoutesController

    Contains the methods required to create new routes and view your current routes.

    • list() - List existing inbound routes from your account
    • createNewRoute() - Create a new inbound route that can then be assigned as either a primary or failover route for a phone number

In your Node.js file, import the Numbers SDK and set up your callback function. The callback parameter prints the response out to the screen. It points back to the callback function added to the Node.js file.

/*
1. Load the Numbers SDK and Node.js util module
2. Declare flowroutenumberslib path relative to your JS file
*/
var flowroute = require('./flowroutenumberslib');
var util = require('util');

//Set up your callback function
var cb =  function(err, response){
  if(err){
   console.log(err);
   }
     console.log(util.inspect(response, false, null));
};

            

Credentials

Replace AccessKey and SecretKey with your API credentials from the Flowroute Manager.

flowroute.configuration.username = "AccessKey";
flowroute.configuration.password = "SecretKey";

        

Methods

listAvailableNPAs()

The method accepts the limit and page parameters which you can learn more about in the API reference. The following example limits the number of NPAs returned to 3:

#List Available NPAs
flowroute.PurchasablePhoneNumbersController.listAvailableNPAs(3,cb)

            
Example Response
{ npas:
 { '201':
   { nxxs: '/v1/available-tns/npanxxs/?npa=201',
     tns: '/v1/available-tns/tns/?npa=201' },
  '202':
   { nxxs: '/v1/available-tns/npanxxs/?npa=202',
     tns: '/v1/available-tns/tns/?npa=202' },
  '203':
   { nxxs: '/v1/available-tns/npanxxs/?npa=203',
     tns: '/v1/available-tns/tns/?npa=203' } },
links: { next: '/v1/available-tns/npas/?limit=3&page=2' } 
}
            
            
listAreaAndExchange()

The method accepts the limit, npa, page, and callback parameters which you can learn more about in the API reference. The following example sets the limit to 2, the npa to 203, and the display page to 3:

//List NPA and NXX
flowroute.PurchasablePhoneNumbersController.listAreaAndExchange(2,203,3,cb);

            
Example Response

Results are returned in numerical order, starting with the lowest NPA number. The npaxxs parameter variable is formatted as a combination of the NPA and NXX. Based on the parameters passed above, 2 NPAs are returned for NXX 203, displayed on page 3:

{ npanxxs:
{ '203648': { tns: '/v1/available-tns/tns/?npa=203&nxx=648' },
   '203654': { tns: '/v1/available-tns/tns/?npa=203&nxx=654' }
},
links:
{ prev: '/v1/available-tns/npanxxs/?npa=203&limit=2&page=2',
 next: '/v1/available-tns/npanxxs/?npa=203&limit=2&page=4' }
}
            
            
search()

The search() method is the most robust option for searching through Flowroute's purchasable phone number inventory. It allows you to search by NPA, NXX, Ratecenter, State, and/or TN (telephone number). The method accepts the limit, npa, nxx, page, ratecenter, state, tn, and callback parameters which you can learn more about in the API reference.

In the following example, a search request sets the limit to 3, the npa to 206, the nxx to 641, the display page to 2, the ratecenter to seattle, the state to wa, the tn to null, and the callback to cb.

//Search
flowroute.PurchasablePhoneNumbersController.search(3,206,641,2,"SEATTLE","WA",null,cb);

            
Example Response
{ tns:
{ '12066417727':
     { initial_cost: '1.00',
       monthly_cost: '1.25',
      billing_methods: [Object],
      ratecenter: 'SEATTLE',
      state: 'WA' },
 '12066417667':
      { initial_cost: '1.00',
       monthly_cost: '1.25',
      billing_methods: [Object],
      ratecenter: 'SEATTLE',
      state: 'WA' },
 '12066417669':
      { initial_cost: '1.00',
       monthly_cost: '1.25',
      billing_methods: [Object],
      ratecenter: 'SEATTLE',
      state: 'WA' } },
links:
{ prev: '/v1/available-tns/tns/?npa=206&nxx=641&state=WA&ratecenter=SEATTLE&limit=3&page=1',
 next: '/v1/available-tns/tns/?npa=206&nxx=641&state=WA&ratecenter=SEATTLE&limit=3&page=3' } }
            
            
purchase()

The purchase() method is used to purchase a telephone number from Flowroute's inventory. The method accepts the billing_method (within the BillingMethod object), telephone_number, and callback parameters which you can learn more about in the API reference.

//Purchase a Telephone Number
var billingMethod = {"billing_method":"METERED"};
flowroute.TelephoneNumbersController.purchase(billingMethod, 12066417667,cb);

            
Example Response

If the purchase is successful, a 201 CreatedHTTP response and empty body are returned indicating the date on which the purchase occurred. One possible error is a 422 Unprocessable Entity as shown below:

'Business Logic Error: The TN is not available for purchase.'

          
list_account_telephone_numbers()

The method accepts the limit, page, pattern, and callback parameters which you can learn more about in the API reference. The following example sets the limit to 3, the page to null, the search pattern to 206, and the callback parameter to cb:

//List Account Telephone Numbers
flowroute.TelephoneNumbersController.listAccountTelephoneNumbers(3,null,206,cb);

            
Example Response
{ tns:
 { '12062092845':
   { billing_method: 'METERED',
     routes: [
       { type: 'SIP-REG', name: 'sip-reg' },
       { type: 'SIP-REG', name: 'sip-reg' } ],
     detail: '/v1/tns/12062092844' },
  '12066417667':
   { billing_method: 'METERED',
       { type: 'SIP-REG', name: 'sip-reg' },
       { type: 'PSTN', name: 'PSTNroute1', value: '178' } ],
     detail: '/v1/tns/12066417667' } } 
  }  
            
            
telephoneNumberDetails()

The method accepts the telephone_number and callback parameters which you can learn more about in the API reference. In the following example, details are requested for the telephone number just purchased using the purchase method.

//Telephone Number Details
flowroute.TelephoneNumbersController.telephoneNumberDetails("12061231234", callback);

            
Example Response
{
  "billing_method": "METERED",
"routes": [
 {
   "type": "SIP-REG",
   "name": "sip-reg"
 },
 {
   "type": "SIP-REG",
   "name": "sip-reg"
 }
      ]
}
            
            

update()

The method accepts the routes, telephone_number, and callback parameters which you can learn more about in the API reference.

In the following example, the primary route is updated to use MyPSTN, while the failover route is updated to use MyHost2:

//Update Telephone Number Routes
var rtes = '[{"name":"MyPSTN"}, {"name":"MyHost2"}]';
flowroute.TelephoneNumbersController.update("12066417667", rtes, cb);

            
Example Response

An empty line is returned for a successful update. No other message is returned. To view the route changes on the phone number, run the listAccountTelephoneNumbers() or telephoneNumberDetails() methods.

204 No Content
''
            
            
list()

The method accepts the limit, page, and callback parameters which you can learn more about in the API reference.

//List Routes
flowroute.InboundRoutesController.list(10, 1, cb);

            
Example Response
{ routes:
{ MyNewURIroute: { type: 'URI', value: 'sip:18002364456@215.122.69.152:5060' },
  MyHost2: { type: 'HOST', value: '24.239.24.45:5060' },
  MyURI2: { type: 'URI', value: 'sip:112066417744@215.122.69.152:5060' },
  'sip-reg': { type: 'SIP-REG', value: null },
  MyPSTN2: { type: 'PSTN', value: '18002364455' },
  MyPSTN: { type: 'PSTN', value: '12065551212' },
  MyNewPSTNroute: { type: 'PSTN', value: '18002364456' },
  PSTNroute1: { type: 'PSTN', value: '178' },
  URIroute1: { type: 'URI', value: 'sip:16476998778@215.122.69.152:5060' },
  MyHost: { type: 'HOST', value: '24.239.23.45:5060' } },
 links: { next: '/v1/routes/?limit=10&page=2' } 
 }
            
            
createNewRoute()

The method accepts the route_name, type, value, and callback parameters which you can learn more about in the API reference.

You can pass as many createNewRoute methods in a single operation. The following example creates new PSTN, HOST, and URI routes:

//Create New Routes
flowroute.InboundRoutesController.createNewRoute("MyNewPSTNroute", "PSTN", "18002364456", cb);
flowroute.InboundRoutesController.createNewRoute("MyNewHOSTroute","HOST","24.239.23.40:5060", cb);
flowroute.InboundRoutesController.createNewRoute("MyNewURIroute","URI","sip: 18002364456@215.122.69.152:5060", cb);

            
Example Response

An empty string ('') is returned for each successfully created route; no other code or message is returned. An error encountered for a specific irc->createNewRoute() line does not prevent the other routes from being created.

' '
            
            

Messaging SDK (v2)

View Repo

Usage

In Flowroute's approach to building the Messaging (v2) API wrapper, HTTP requests are handled by the APIController, which contains the functions used to perform tasks with the Node.js SDK.

APIController

In your Node.js file, import the Messaging SDK and set up your callback function. The callback parameter prints the response out to the screen. It points back to the callback function added to the Node.js file.


var flowroute = require('./flowroutemessaginglib');
//Set up your callback function
var cb =  function(err, response){
  if(err){
   console.log(err);
   }
    console.log(response);
};

            

Credentials

Replace AccessKey and SecretKey with your API credentials from the Flowroute Manager.

flowroute.configuration.username = "AccessKey";
flowroute.configuration.password = "SecretKey";

            

Methods

createMessage()

The method accepts the msg, to, from, body, and callback parameters which you can learn more about in the API reference.

#Create and send the message
var msg = {"to": "12066418000", "from": "12064205780", "content": "That rug really tied the room together."};
flowroute.MessagesController.createMessage(msg, cb);

            
Example Response

{"data": {"id": "mdr1-d858379c1bee4dd3bfe5c556f7cef70f"}}

            
getMessageLookup()

The method accepts the record_id and callback parameters which you can learn more about in the API reference.

#Get the MDR
flowroute.MessagesController.getMessageLookup("d858379c1bee4dd3bfe5c556f7cef70f", cb);

            
Example Response
{
"data": {
 "attributes": {
   "body": "That rug really tied the room together.",
   "direction": "outbound",
   "timestamp": "2016-06-13T20:34:29.095045+00:00",
   "amount_nanodollars": 4000000,
   "from": "12064205780",
   "message_encoding": 0,
   "has_mms": false,
   "to": "12066418000",
   "amount_display": "$0.0040",
   "callback_url": null,
   "message_type": "long-code"
         },
 "type": "message",
 "id": "mdr1-d858379c1bee4dd3bfe5c556f7cef70f"
    }
}