Getting started

 

 

Get started with receipt 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({
  receiptToken: "yourReceiptToken",
});

 

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:

 

  • receiptToken (env variable : MINDEE_RECEIPT_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 a receipt

 

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

 

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

const mindeeClient = new Client({
  receiptToken: "yourReceiptToken"
});

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

 

 

Input types

 

You can pass your input file in three ways:

 

From file path

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

 

 From a file object

const fs = require("fs");

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

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

 

From a base64

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

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

 

 

Result data structure

 

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

 

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

 

result.receipt

 

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

 

result.receipts

 

For multi-pages pdf, the receipts attribute is a list of receipt 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 Receipt fields section