Skip to main content
Skip table of contents

Example Application - Credit Score

Below, you will find a guide to creating an example credit score application using Boss Insights' api.

  1. Quick Start Project In Node.js

Now that we can connect to different endpoints in the boss insights API, here is a simple console application to show how the API can be used to create a mock credit score.

Lets say you are a Lender. You give out loans based on criteria listed below. Making the credit score isn’t all that hard but it can take some time for the borrower to get all their information in order This is a huge inconvenience to both you and the borrower.

This is where Boss Insights comes in. Instead of having to go through everything themselves and submitting information. Boss insights makes it so that all the borrower has to do is connect their accounts like QuickBooks and the Boss API can get you the necessary data.

Mock Credit Score Criteria:

  • [invoices] More than $20k invoiced in last 60 days, +5 points

  • [customers] More than 5 customer +5 points

  • [balance sheet] Assets greater than liabilities + 10 points

  • [accounts receivable] Percentage of accounts paid > 50% in last 60 days +10 pts

  • [accounts payable] Accounts payable over 60 days not greater than 10k + 10pts

Step 1: First we will require the necessary dependencies, declare repeated variables for the API connection, and then wrap our request in a function so that we only have to write it once and then call it through out our code.

JS
var fs = require("fs");
var https = require("follow-redirects").https;
//const path = require("path");

//Declare repeated variables for API connection
var auth = "username:password"; //where username is your username and password is your password
var protocol = "https:";
var hName = "myCompanyName.myintranetapps.ca"; //where myCompanyName is you company name

var creditScore = 0;

console.log("Calculating Credit score. This may take a few minutes");

// Send request function to establish connection to API
function sendRequest(options) {
  return new Promise(function (resolve, reject) {
    //Create https get request
    var req = https.request(options, function (res) {
      var chunks = [];
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });
      //If connection Successful.
      res.on("end", function (chunk) {
        var body = Buffer.concat(chunks);
        //parse into json object
        body = JSON.parse(body);

        resolve(body);
      });
      res.on("error", function (error) {
        console.error(error);
        reject(error);
      });
    });
    req.end();
  });
}

Step 2: Now lets start the first part of out credit score. We will start with:

  • [balance sheet] Assets greater than liabilities + 10 points

JS
//Async function to connect to balance sheet endpoint to calculate credit score.
async function getbalanceSheet() {
  //API connection parameters
  var balanceSheetOptions = {
    rejectUnauthorized: false,
    auth: auth,
    protocol: protocol,
    method: "GET",
    hostname: hName,
    //Path has start and end date. Since only one balance sheet page will be 1.
    path: "/api/balance_sheets?start=2021-01-01end=2021-03-31&accounting_method=accrual&page=1",
    headers: {},
    maxRedirects: 20,
    port: "4443", //for testing purposes may be changes or taken out. 
  };
  var balanceSheetBody = await sendRequest(balanceSheetOptions);

  //Get Assets and Liabilities from API and store it into variables Assets and Liabilities
  for (var i = 0; i < balanceSheetBody.length; i++) {
    var assets = balanceSheetBody[i].totalAssets;
    var liabilities = balanceSheetBody[i].totalLiabilities;
  }
  console.log(`Assets: $${assets}, Liabilities: $${liabilities}`);
  //if assets are greater than liabilities +10
  if (assets > liabilities) {
    console.log(`Assets Are greater than Liablilities`);
    creditScore = creditScore + 10;
  } else if (assets < liabilities) {
    console.log(`Liabilities greater than Assets`);
  }

  console.log("Credit Score:", creditScore);
}
getbalanceSheet();

Step 2: Next we will connect to the Invoices endpoint. From here we can calculate both:

  • [invoices] More than $20k invoiced in last 60 days, +5 points and

  • [accounts receivable] Percentage of accounts paid > 50% in last 60 days +10 pts\

In this end point and the others we must deal with pagination.

So we will loop through all of the pages and store all the data in an array.

JS
//Async function connecting to Invoices end point to calculate amount invoiced and percentage of accounts paid in 60 days
async function getInvoices() {
  //loop through all pages to get all data and store it in allInvoices
  var page = 1;
  var allInvoices = [];
  while (true) {
    var invoiceOptions = {
      rejectUnauthorized: false,
      auth: auth,
      protocol: protocol,
      method: "GET",
      hostname: hName,
      path: "/api/invoices?page=" + page,
      headers: {},
      maxRedirects: 20,
      port: "4443",
    };

    var invoiceBody = await sendRequest(invoiceOptions);
    //if invoiceBody is empty break the loop.
    if (invoiceBody.length === 0) {
      break;
    }
    //Get data from all pages and store them in array allInvoices
    allInvoices = allInvoices.concat(invoiceBody);
    page++;
  }
  //Calculate credit score based on condition: More than $20k invoiced in last 60 days, +5 points
  // and Percentage of accounts paid > 50% in last 60 days +10 pts
  //Get date from 60 days ago.
  var sixtyDaysAgo = new Date();
  var ourDate = sixtyDaysAgo.getDate() - 60;
  sixtyDaysAgo.setDate(ourDate);

  var sumOfBalanceInSixtyDays = 0;
  var totalInSixtyDays = 0;
  var invoiceTotal = 0;

  //Loop through allInvoices and store them in "obj"
  for (var i = 0; i < allInvoices.length; i++) {
    var obj = allInvoices[i];

    invoiceTotal += obj.totalAmount;
    var invoiceDates = new Date(obj.created);
    //Only if invoiceDates are greater than dates sixtydaysAgo
    //Get the total amount in 60 days and balance in 60 days.
    if (invoiceDates > sixtyDaysAgo) {
      totalInSixtyDays += obj.totalAmount;
      sumOfBalanceInSixtyDays += obj.balance;
    }
  }
  //Calculate percentage paid based on totalInSixtyDays and sumOfBalanceInSixtyDays
  var totalPaid = totalInSixtyDays - sumOfBalanceInSixtyDays;
  var percentagePaid = (totalPaid / totalInSixtyDays) * 100;
  console.log(`percentage of accounts paid: ${percentagePaid.toFixed(2)}%`);
  //Percentage of accounts paid > 50% in last 60 days +10 pts
  if (percentagePaid > 50) {
    console.log(`Percentage of accounts paid greater than 50%`);
    creditScore = creditScore + 10;
  } else if (percentagePaid < 50) {
    console.log(`Percentage of accounts paid less than 50%`);
  }

  console.log("Credit Score:", creditScore);
  // If More than $20k invoiced in last 60 days, +5 points
  if (totalInSixtyDays > 20000) {
    console.log(`Invoice total in last 60 days Greater than $20,000`);
    creditScore = creditScore + 5;
  } else if (totalInSixtyDays < 20000) {
    console.log(`Invoice total in last 60 days less than $20,000`);
  }

  console.log("Credit Score:", creditScore);
}
getInvoices();

Step 3: Now we want to connect to the customers end point and see how many customers the borrowers has so that we can do the next part of our credit score:

  • [customers] More than 5 customer +5 points

JS
async function getCustomers() {
  var page = 1;
  var allcustomers = [];
  //Loop through all pages and store data in array all customers from each page
  while (true) {
    var customerOptions = {
      rejectUnauthorized: false,
      auth: auth,
      protocol: protocol,
      method: "GET",
      hostname: hName,
      path: "/api/customers?page=" + page,
      headers: {},
      maxRedirects: 20,
      port: "4443",
    };

    var customerBody = await sendRequest(customerOptions);

    //If customerBody is empty(nothing in page) then break the loop.
    if (customerBody.length === 0) {
      break;
    }
    allcustomers = allcustomers.concat(customerBody);
    page++;
  }
  console.log(`All Customers: ${allcustomers.length}`);
  // If More than 5 customer +5 points
  if (allcustomers.length >= 5) {
    console.log(`Number of customers Greater than 5`);
    creditScore = creditScore + 5;
  } else if (allcustomers.length < 5) {
    console.log(`Number of Customers Less than 5`);
  }

  console.log("Credit Score:", creditScore);
}
getCustomers();

Step 4: Lastly, we will connect to the bills endpoint to calculate the credit score requirement for account's payable (A/P):

  • [accounts payable] Accounts payable over 60 days not greater than 10k + 10pts

JS
//Function to get AP by connecting to bills endpoint to calculate credit score
async function getbills() {
  var page = 1;
  var allBills = [];
  while (true) {
    var billsOptions = {
      rejectUnauthorized: false,
      auth: auth,
      protocol: protocol,
      method: "GET",
      hostname: hName,
      path: "/api/bills?page=" + page,
      headers: {},
      maxRedirects: 20,
      port: "4443",
    };
    //console.log("sending request for Bills page ", page);
    var billsBody = await sendRequest(billsOptions);
    if (billsBody.length === 0) {
      break;
    }
    allBills = allBills.concat(billsBody);
    page++;
  }
  //get date from 60 days ago
  var sixtyDaysAgo = new Date();
  var ourDate = sixtyDaysAgo.getDate() - 60;
  sixtyDaysAgo.setDate(ourDate);

  var totalInSixtyDays = 0;
  //Loop through all bills
  for (var i = 0; i < allBills.length; i++) {
    var obj = allBills[i]; //store all bills into and object
    var billsDates = new Date(obj.created); // Get dates from api into date object
    if (billsDates > sixtyDaysAgo) {
      totalInSixtyDays += obj.totalAmount; //Get the dates from the last 60 dasy add them up and store into variable
    }
  }
  console.log(`total bills in last 60 days:  ${totalInSixtyDays}`);

  if (totalInSixtyDays < 10000) {
    console.log("AP in last 60 days less than $10,000");
    creditScore = creditScore + 10; // if Ap/bills in last 60 days less than $10000 +10 to credit score
  } else if (totalInSixtyDays > 10000) {
    console.log("AP in last 60 days greater than $10,000");
  }

  console.log("Credit Score:", creditScore);
}
getbills();

These are the steps and endpoints that we connected to to make a fake mini fake credit score. A lot more can be taken into consideration when calculating a credit score and Boss insights has many other endpoints to help you calculate. Boss insights API can help you accomplish even more than just fake credit scores. For example:  integrating commerce data to offer lending to a company based on past sales or accounts receivable. Ultimately what you can accomplish with Boss insights is all up to you.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.