Getting started

 

 

Get started with Invoice parsing in node.js

 

1. Install the mindee node.js helper library

 

The easiest way to install the mindee node.js helper is from NPM. You can run the command below from your project directory to install the library:

 

npm install mindee

 

 

2. Instantiate your Client

 

The mindee.Client needs your API credentials. You can either pass these directly to the constructor (see the code below) or via environment variables.

 

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

const mindeeClient = new Client({
  invoiceToken: "yourInvoiceToken",
});

 

The mindee.Client can take multiple parameters. Some of those parameters can also be set using environment variables. For any parameter, if the env variable and the parameter are both defined, the parameter used will be the one set in the mindee.Client. This is the list of the different parameters:

 

  • invoiceToken (env variable : MINDEE_INVOICE_TOKEN) The invoice API token
  • throwOnError (true by default) If true, the SDK will throw an error when the API response status is different from 200
  • debug (env variable : MINDEE_DEBUGfalse by default) If true, logging will be in debug mode

 

 

We suggest storing your credentials as environment variables. Why? You'll never have to worry about committing your credentials and accidentally posting them somewhere public.

 

 

 

3. Parse an invoice

 

Using the parse method from Client.invoice, you can pass any image or pdf file to get the invoice data.

 

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

const mindeeClient = new Client({
  invoiceToken: "yourInvoiceToken"
});

mindeeClient.invoice.parse(
  "path/to/file.jpg",
  'path',
  version = "2",
  cutPdf = true,
  includeWords = false
)
  .then((res) => {
    console.log(res.invoice);
  })
  .catch((err) => {
    console.error(err);
  });

 

 

Input types

 

You can pass your input file in three ways:

 

From file path

mindeeClient.invoice.parse(
  "path/to/file.jpg",
  'path',
)
  .then((res) => {
    console.log(res.invoice);
  })
  .catch((err) => {
    console.error(err);
  });

 

 From a file object

const fs = require("fs");

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

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

 

From a base64

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

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

 

 

Result data structure

 

The result object returned by the parse method contains the following elements:

 

mindeeClient.invoice.parse(
  '/path/to/file,
  'path',
)
  .then((result) => {
    console.log(res.invoice);
    console.log(res.invoices);
    console.log(res.httpResponse);
  })
  .catch((err) => {
    console.error(err);
  });

 

result.invoice

 

The invoice attribute is the Invoice object constructed by gathering all the pages into a single document setting the fields by checking the maximum confidence score of all pages.

 

result.invoices

 

For multi-pages pdf, the invoices attribute is a list of Invoice objects, each object is constructed using a unique page of the pdf

 

result.http_response

 

Contains the full Mindee API HTTP response object

 

 

More details about the extracted fields in the Invoice fields section