Getting started

This guide will help you get started with the Mindee Node.js client library to easily extract data from your documents. The Node.js client library supports invoice and receipt OCR APIs.

You can view the source code on GitHub. The client library is officially supported on Node.js LTS active and maintenance versions.

Prerequisite

Download and install npm and Node.js.

Note: When installing a Node.js instance, nvm will also install a compatible npm version.

Installation

To quickly get started with the Node.js client library.

Install the Mindee client library in your node project.

npm install mindee

Development Installation

If you'll be modifying the source code, you'll need to follow these steps to get started.

  1. First clone the repo.
git clone [email protected]:mindee/mindee-api-nodejs.git
  1. Navigate to the cloned directory and install all required libraries.
cd mindee-api-node.js
npm install 

Updating Version

It is important to always check the version of the Mindee client library you are using, as new and updated features won’t work on old versions.

To get the latest version of your client library.

npm update mindee

To install a specific version of Mindee.

npm install [email protected]<version>

Usage

To get started with Mindee's APIs, you need to create a Client and you're ready to go.

Let's take a deep dive into how this works.

The Client

The mindee.Client requires your API key. You can either pass these directly to the constructor or through environment variables. You only need to specify the API keys for the document endpoints you'll be using.

const { Client } = require("mindee");

const mindeeClient = new Client({
  invoiceToken: "yourInvoiceApiKey",
  receiptToken: "yourReceiptApiKey",
});

There are three ways to add documents to the client using the config-xxx method.

Single Document

You can have a separate client for each document. If you have only a single document type you're working with, this is the easiest way to get started.

const { Client } = require("mindee");
const fs = require("fs");

// Invoice token can be set by Env (MINDEE_INVOICE_TOKEN) or via params (Client({invoiceToken: "key"}))
const mindeeClient = new Client(invoiceToken: "invoiceApikey");

// parsing invoice from pdf
mindeeClient.invoice
  .parse({ input: "./documents/invoices/invoice.pdf" }) // see examples for more input types
  .then((res) => {
    console.log("Success !");
    console.log(res.invoices);
    console.log(res.invoice);
  })
  .catch((err) => {
    console.error(err);
  });

Multiple Documents

You can have all your documents configured in the same client. If you're working with multiple document types this is the easiest way to get started.

const { Client } = require("mindee");

const mindeeClient = new Client({
  invoiceToken: "invoiceApiToken",
  receiptToken: "receiptExpenseApiToken",
});

mindeeClient.invoice.parse({ input: "path/to/file", inputType: "file" });
mindeeClient.receipt.parse({ input: base64String, inputType: "base64" });
mindeeClient.financialDocument.parse({
  input: base64String,
  inputType: "base64",
});

Mix and Match

You can also mix and match. This approach is useful if you have a group of documents that needs to be handled in different ways. The Financial document API optimizes the parsing results when you don't know if your file is a receipt or an invoice.

const { Client } = require("mindee");

// Invoice token and Receipt token must be set
// Receipt token can be set by Env (MINDEE_RECEIPT_TOKEN) or via params (Client({receiptToken: "token"}))
// Invoice token can be set by Env (MINDEE_INVOICE_TOKEN) or via params (Client({invoiceToken: "token"}))

const mindeeClient = new Client();

// parsing receipt from picture
mindeeClient.financialDocument
  .parse({ input: "./documents/receipts/receipt.jpg" })
  .then((res) => {
    console.log("Success !");
    console.log(res.financialDocuments);
    console.log(res.financialDocument);
  })
  .catch((err) => {
    console.error(err);
  });

Environment Variables

API keys should be set as environment variables, especially for any production deployment. The environment variables can also be used for basic logging at various levels. You'll need to create a .env file where you'll add your Mindee API tokens. The file should have the following format:

MINDEE_RECEIPT_TOKEN= "yourReceiptApiKey"
MINDEE_INVOICE_TOKEN = "yourInvoiceApiKey"

Multiple parameters can be sent to the mindee.Client. If both the env variable and the parameter are defined, the parameter used will be the one set in the mindee.Client. The following is a list of the various parameters:

ParametersEnvironment variableDetails
invoiceTokenMINDEE_INVOICE_TOKENInvoice API key
receiptTokenMINDEE_RECEIPT_TOKENReceipt API key
throwOnError(true by default) If true, the client library will throw an error when the API response status is different from 200
debugMINDEE_DEBUG(false by default) If true, logging will be in debug mode

Document Parsing

When parsing your document, the client calls the parse method. The document parse type must be specified when calling the parse method.

const { Client } = require("mindee");

const mindeeClient = new Client({
  invoiceToken: "yourInvoiceApiKey",
  receiptToken: "yourReceiptApiKey"
});

mindeeClient.receipt.parse({
    input : "receipt.jpg",
    inputType : 'path',
})
.then((res) => {
  console.log(res.receipt);
})
.catch((err) => {
  console.error(err);
});

mindeeClient.invoice.parse({
    input : "invoice.pdf",
    inputType : 'path',
    filename : undefined,
    cutPdf : true,
    includeWords : false
})
.then((res) => {
  console.log(res.invoice);
})
.catch((err) => {
  console.error(err);
});

The different ways you can load and parse your data are through:

File Path

This requires an absolute path, as a string.

mindeeClient.invoice.parse({
    input : "invoice.pdf",
    inputType : 'path',
})
.then((res) => {
  console.log(res.invoice);
})
.catch((err) => {
  console.error(err);
});

File Object

This requires a file object with a path.

const fs = require("fs");

const file = fs.createReadStream("./path/to/img/or/pdf");

mindeeClient.invoice.parse({
    input : "file",
    inputType : 'stream',
})
.then((res) => {
  console.log(res.invoice);
})
.catch((err) => {
  console.error(err);
});

Base64

File should be in a base64 encoded string.

const base64 = fs.readFileSync("./inv.pdf", {
  encoding: "base64",
});

mindeeClient.invoice.parse({
    input : "base64",
    inputType : 'base64',
})
.then((res) => {
  console.log(res.invoice);
})
.catch((err) => {
  console.error(err);
});

Did this page help you?