# Node.js OCR SDK

## Mindee API Helper Library for Node.js

Quickly and easily connect to Mindee's API services using Node.js.

### Quick Start

Here's the TL;DR of getting started.

First, get an [API Key](https://docs.mindee.com/v1/get-started/create-api-key)

Then, install this library:

```shell
npm install mindee@^5.0.0
```

Finally, Node.js away!

#### Loading a File and Parsing It

**Global Documents**

```js
const mindee = require("mindee");
// for TS or modules:
// import * as mindee from "mindee";

// Init a new client
const mindeeClient = new mindee.v1.Client({ apiKey: "my-api-key" });

// Load a file from disk
const filePath = "/path/to/the/file.ext";
const inputSource = new mindee.PathInput({ inputPath: filePath });

// Parse it on the API of your choice
const apiResponse = mindeeClient.parse(
  mindee.v1.product.InvoiceV4, inputSource
);
```

**Note:** Files can also be loaded from:

A base64 encoded string:

```js
const b64String = "iVBORw0KGgoAAAANSUhEUgAAABgAAA ...";
const inputSource = new mindee.Base64Input({
  inputString: b64String,
  filename: "base64_file.txt",
});
```

A byte sequence:

```js
const inputBytes = await fs.promises.readFile("/path/to/the/file.ext");
const inputSource = new mindee.BytesInput({
  inputBytes: inputBytes,
  filename: "file.ext",
});
```

A stream:

```js
const stream = fs.createReadStream("/path/to/the/file.ext");
const inputSource = new mindee.StreamInput({
  inputStream: stream,
  filename: "file.ext",
});
```

A buffer:

```js
const buffer = Buffer.from(
  await fs.promises.readFile("/path/to/the/file.ext")
);
const inputSource = new mindee.BufferInput({
  buffer: buffer,
  filename: "file.ext",
});
```

A URL (`https` only):

```js
const inputSource = new mindee.UrlInput({
  url: "https://example.com/file.ext"
});
```

You can also load the document locally before sending it:

```js
const inputSource = new mindee.UrlInput({
  url: "https://example.com/file.ext"
});
await inputSource.init();
const localInputSource = inputSource.asLocalInputSource();
```

**Note:** Files hidden behind redirections are rejected by the server; this solution helps to circumvent that issue.

**Region-Specific Documents**

Region-Specific Documents use the following syntax:

```js
const mindee = require("mindee");
// for TS or modules:
// import * as mindee from "mindee";

const mindeeClient = new mindee.v1.Client({ apiKey: "my-api-key" });

const filePath = "/path/to/the/file.ext";
const inputSource = new mindee.PathInput({ inputPath: filePath });

// The IdCardV1 product belongs to mindee.v1.product.fr, not mindee.product itself
const apiResponse = mindeeClient.parse(mindee.v1.product.fr.IdCardV1, inputSource);
```

**Custom Documents (docTI & Custom APIs)**

Custom documents will require you to provide their endpoint manually.

```js
const mindee = require("mindee");
// for TS or modules:
// import * as mindee from "mindee";

// Init a new client
const mindeeClient = new mindee.v1.Client({
  apiKey: "my-api-key"
});

// Load a file from disk
const filePath = "/path/to/the/file.ext";
const inputSource = new mindee.PathInput({ inputPath: filePath });

// Create a custom endpoint for your product
const customEndpoint = mindeeClient.createEndpoint(
  "my-endpoint",
  "my-account",
  "my-version" // will default to 1 if not provided
);

// Parse it
const apiResponse = await mindeeClient
  .enqueueAndParse(
    mindee.v1.product.GeneratedV1,
    inputSource,
    {
      endpoint: customEndpoint
    }
  );
```

#### Handling the Return

```js
// Handle the response Promise
apiResponse.then((resp) => {
    // print a string summary
    console.log(resp.document.toString());

    // individual pages (array)
    console.log(res.document.inference.pages);
});
```

#### Additional Options

Options to pass when sending a file to be parsed.

**Page Options**

Allows only sending certain pages in a PDF.

In this example we only send the first, penultimate, and last pages:

```js
const apiResponse = mindeeClient.parse(
  mindee.v1.product.InvoiceV4,
  inputSource,
  {
    pageOptions: {
      pageIndexes: [0, -2, -1],
      operation: mindee.PageOptionsOperation.KeepOnly,
      onMinPages: 2
    }
  });
```

### Further Reading

Take a look at the [**Reference Documentation**](https://mindee.github.io/mindee-api-nodejs/).

### License

Copyright © Mindee

Available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
