Overview
This guide will help you extract data from Mortgage Statements using Butler's OCR APIs in Node.js. In 15 minutes you'll be ready to add Node.js Mortgage Statement OCR into your product or workflow!
Before getting started, you'll want to make sure to do the following:
- Signup for a free Butler account at https://app.butlerlabs.ai
- Write down your Butler API key from the Settings menu. Follow the Getting Started guide for more details about how to do that.
Get your API ID
Sign into the Butler product, go to the Library and search for the Mortgage Statement model:
Click on the Mortgage Statements card, then press the Try Now button to create a new Mortgage Statement model:
Once on the model details page, go to the APIs tab:
Copy the API ID (also known as the Queue ID) and write it down. We'll use it in our code below.
Sample Node.js Mortgage Statement OCR Code
You can copy and paste the following Node.js sample code to process documents with OCR using the API.
// Import necessary libraries
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
// Specify variables for use in script below
const apiBaseUrl = 'https://app.butlerlabs.ai/api';
// Make sure to add the API Key you wrote down above to the auth headers
const apiKey = 'MY_API_KEY';
const authHeaders = {
'Authorization': 'Bearer ' + apiKey
};
// Use the Queue API Id you grabbed earlier
const queueId = 'MY_QUEUE_ID';
// Specify the path to the file you would like to process
const localFilePaths = ['/path/to/file'];
// Specify the API URL
const uploadUrl = apiBaseUrl + '/queues/' + queueId + '/uploads';
// This async function uploads the files passed to it and returns the id
// needed for fetching results.
// It is used in our main execution function below
const uploadFiles = async (filePaths) => {
// Prepare file for upload
const formData = new FormData();
filePaths.forEach((filePath) => {
formData.append('files', fs.createReadStream(filePath));
});
// Upload files to the upload API
console.log('Uploding files to Butler for processing');
const uploadResponse = await axios.post(
uploadUrl,
formData,
{
headers: {
...authHeaders,
...formData.getHeaders(),
}
})
.catch((err) => console.log(err));
// Return the Upload ID
return uploadResponse.data.uploadId;
}
// This async function polls every 5 seconds for the extraction results using the
// upload id provided and returns the results once ready
const getExtractionResults = async (uploadId) => {
// URL to fetch the result
const extractionResultsUrl = apiBaseUrl + '/queues/' + queueId + '/extraction_results';
const params = { uploadId };
// Simple helper function for use while polling on results
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
// Make sure to poll every few seconds for results.
// For smaller documents this will typically take only a few seconds
let extractionResults = null;
while (!extractionResults) {
console.log('Fetching extraction results');
const resultApiResponse = await axios.get(
extractionResultsUrl,
{ headers: { ...authHeaders }, params, }
);
const firstDocument = resultApiResponse.data.items[0];
const extractionStatus = firstDocument.documentStatus;
// If extraction has not yet completed, sleep for 5 seconds
if (extractionStatus !== 'Completed') {
console.log('Extraction still in progress. Sleeping for 5 seconds...');
await sleep(5 * 1000);
} else {
console.log('Extraction results ready');
return resultApiResponse.data;
}
}
}
// Use the main function to run our entire script
const main = async () => {
// Upload Files
const uploadId = await uploadFiles(localFilePaths);
// Get the extraction results
const extractionResults = await getExtractionResults(uploadId);
// Print out the extraction results for each document
extractionResults.items.forEach(documentResult => {
const fileName = documentResult.fileName;
console.log('Extraction results from ' + fileName);
// Print out each field name and extracted value
console.log('Fields')
documentResult.formFields.forEach(field => {
const fieldName = field.fieldName;
const extractedValue = field.value;
console.log(fieldName + ' : ' + extractedValue);
});
// Print out the results of each table
console.log('\n\nTables');
documentResult.tables.forEach(table => {
console.log('Table name: ' + table.tableName);
table.rows.forEach((row, idx) => {
let rowResults = 'Row ' + idx + ': \n';
row.cells.forEach(cell => {
// Add each cells name and extracted value to the row results
rowResults += cell.columnName + ': ' + cell.value + ' \n';
});
console.log(rowResults);
});
});
});
}
main();
Make sure to do the following before running the code:
- Replace the queueId variable with your API ID
- Replace the apiKey variable with your API Key
- Replace the localFilePaths variable with your local file location
In-Product Sample Code
You can also copy the sample code directly from the product. This code will have your API ID and API Key already pre-populated for you!
Extracted Mortgage Statement Fields
Here is an example of what a Mortgage Statement JSON response looks like:
{
"documentId": "8491e900-5bbd-44e1-8fbb-e3ecb397f0e0",
"documentStatus": "Completed",
"fileName": "Sample Mortgage Statement.pdf",
"mimeType": "application/pdf",
"documentType": "Mortgage Statements",
"confidenceScore": "High",
"formFields": [
{
"fieldName": "Borrower Address",
"value": "4700 Oak Ridge Ln\nBethesda, MD 20814",
"confidenceScore": "High"
},
{
"fieldName": "Borrower Name 1",
"value": "Adam and Mary Jones",
"confidenceScore": "High"
},
{
"fieldName": "Due Date",
"value": "4/1/2012",
"confidenceScore": "High"
},
{
"fieldName": "Fees Due",
"value": "$160.00",
"confidenceScore": "High"
},
{
"fieldName": "Frequency",
"value": "Monthly",
"confidenceScore": "Low"
},
{
"fieldName": "Interest Due",
"value": "$1,048.07",
"confidenceScore": "High"
},
{
"fieldName": "Interest Rate",
"value": "4.75%",
"confidenceScore": "High"
},
{
"fieldName": "Loan Number",
"value": "1234567",
"confidenceScore": "High"
},
{
"fieldName": "Maturity Date",
"value": "September 2039",
"confidenceScore": "High"
},
{
"fieldName": "Principal Balance",
"value": "$264,776.43",
"confidenceScore": "High"
},
{
"fieldName": "Principal Due",
"value": "$386.46",
"confidenceScore": "High"
},
{
"fieldName": "Property Address",
"value": "4700 Oak Ridge Ln\nBethesda, MD 20814",
"confidenceScore": "High"
},
{
"fieldName": "Regular Payment Amount",
"value": "$1,669.71",
"confidenceScore": "High"
},
{
"fieldName": "Servicer Address",
"value": "8100 Market Ave\nBethesda, MD 20814",
"confidenceScore": "Low"
},
{
"fieldName": "Servicer Name",
"value": "Springside Mortgage",
"confidenceScore": "Low"
},
{
"fieldName": "Statement Date",
"value": "3/20/2012",
"confidenceScore": "High"
},
{
"fieldName": "Total Due",
"value": "$1,829.71",
"confidenceScore": "High"
},
{
"fieldName": "Total Escrow Due",
"value": "$235.18",
"confidenceScore": "High"
}
],
"tables": [
{
"tableName": "Transactions",
"confidenceScore": "Low",
"rows": [
{
"cells": [
{
"columnName": "Description",
"value": "Late Fee (charged because payment was received after 3/15/2012)",
"confidenceScore": "High"
},
{
"columnName": "Effective Date",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Escrow Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Fees Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Interest Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Others Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Principal Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Total Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Total Charged",
"value": "$160.00",
"confidenceScore": "High"
},
{
"columnName": "Total Paid",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Transaction Date",
"value": "3/16/12",
"confidenceScore": "High"
},
{
"columnName": "Unapplied Amount",
"value": "",
"confidenceScore": "Low"
}
]
},
{
"cells": [
{
"columnName": "Description",
"value": "Payment Received - Thank you",
"confidenceScore": "High"
},
{
"columnName": "Effective Date",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Escrow Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Fees Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Interest Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Others Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Principal Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Total Amount",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Total Charged",
"value": "",
"confidenceScore": "Low"
},
{
"columnName": "Total Paid",
"value": "$1,669.71",
"confidenceScore": "High"
},
{
"columnName": "Transaction Date",
"value": "3/17/12",
"confidenceScore": "High"
},
{
"columnName": "Unapplied Amount",
"value": "",
"confidenceScore": "Low"
}
]
}
]
}
]
}
Mortgage Statement API Response Details
For full details about the Mortgage Statement Model and its API response, see the Mortgage Statement page.