How to Use Dynamsoft Document Capture REST API in Node.js

Dynamsoft Document Capture – an online document capture and OCR service – has been launched for a while. The corresponding RESTful service is also available now. In this article, I will unveil how to use the REST APIs to operate image files and perform OCR.

Dynamsoft Cloud Service Registration

Visit the registration page to create a new account.

OCR API Key

After login, please go to the OCR API Key page to generate a key, which will be used for authorization of your requests.

OCR API key

Limitations for Free Account

  • Your input source file will be automatically deleted after successful recognition.
  • The output file can live on the server for 30 days.
  • The maximum size for one file supported by the service is 100 MB.
  • The maximum size for all files under one account is 500 MB.
  • Trial accounts get 25 free credits on registration.

API Reference

In API reference page, you can get information about APIs, error code and OCR language support.

Quick Start

Install the DDC Node.js module:

npm install rest-ocr

Create test.js:

var DDC = require('rest-ocr');
var key = ‘Your OCR API key';
var ddc = new DDC(key);

ddc.queryQuota(function (error, response, body) {

    if (ddc.isJSON(body)) {
        console.log(body);
    }

});

Run the app to query your account quota:

node test.js

Here is my returned JSON data:

{"quota":29,"used":23,"error_code":0,"error_msg":"Success.","request_id":"8b9ac1f2b4ba4f7f9a1c42bf3e8d65df"}

How to Use RESTful APIs

The basic steps of OCR recognition:

  1. Upload or capture an image file.
  2. Choose an output format: Microsoft Word, Microsoft Excel, Microsoft PowerPoint, ePub, HTML, CSV, Text, Formatted Text, PDF, and XML. The default file format is Docx.
  3. Recognize text and save content to the target file.

To quickly send an HTTP request in Node.js, we can use request:

var request = require('request');

Check page credits

Quota means how many images you can use for OCR service. For example, if the value is 25, you can only upload or capture 25 images:

var options = {
      url: 'https://cloud.dynamsoft.com/rest/ocr/v1/quota?method=page',
      method: 'GET',
      headers: {
        'x-api-key': this.key
      }
    };
    request(options, callback);

Check disk space

The storage space is limited. Before uploading images, you’d better check the available disk space:

let options = {
      url: 'https://cloud.dynamsoft.com/rest/ocr/v1/quota?method=disk',
      method: 'GET',
      headers: {
        'x-api-key': this.key
      }
    };
    request(options, callback);

Upload a picture file

let formData = {
      file_name: fileName,
      file: fs.createReadStream(filePath),
    };

    let options = {
      url: 'https://cloud.dynamsoft.com/rest/ocr/v1/file?method=upload',
      method: 'POST',
      headers: {
        'x-api-key': this.key,
        'content-type': 'multipart/form-data'
      },
      formData: formData
    };
    request(options, callback);

ddc file upload

OCR

The Data format should be: {“list”:[{“file_name”:”1.jpg”},{“file_name”:”2.jpg”}]}

let formData = {
      language: 'eng',
      output_format: 'DOCX',
      param: JSON.stringify(data)
    };

    let options = {
      url: 'https://cloud.dynamsoft.com/rest/ocr/v1/file?method=recognize',
      method: 'POST',
      headers: {
        'x-api-key': this.key,
        'content-type': 'multipart/form-data'
      },
      formData: formData
    };
    request(options, callback);

 

OCR

Download and delete files

Note: currently, only saved files (Document Repository > files), not the original uploaded files, can be operated.

Download a file:

let url =
        'https://cloud.dynamsoft.com/rest/ocr/v1/file?method=download&file_name=' +
        urlencode(fileName);

    let options = {
      url: url,
      method: 'GET',
      headers: {
        'x-api-key': this.key,
      }
    };

    request(options, callback).pipe(fs.createWriteStream(fileName));

Delete a file:

let url =
        'https://cloud.dynamsoft.com/rest/ocr/v1/file?method=delete&file_name=' +
        urlencode(fileName);

    let options = {
      url: url,
      method: 'POST',
      headers: {
        'x-api-key': this.key,
      }
    };
    request(options, callback);

Delete multiple files:

let formData = {param: JSON.stringify(data)};

    let options = {
      url: 'https://cloud.dynamsoft.com/rest/ocr/v1/file?method=delete',
      method: 'POST',
      headers: {
        'x-api-key': this.key,
        'content-type': 'multipart/form-data'
      },
      formData: formData
    };
    request(options, callback);

The Data format should be: {“list”:[{“file_name”:”1.jpg”},{“file_name”:”2.jpg”}]}.

Source Code

https://github.com/dynamsoft-document-capture/REST-API