Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@google-cloud/documentai

googleapis208.8kApache-2.09.1.0TypeScript support: included

Document AI client for Node.js

readme

Google Cloud Platform logo

Document AI: Node.js Client

release level npm version

Document AI client for Node.js

A comprehensive list of changes in each version may be found in the CHANGELOG.

Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Table of contents:

Quickstart

Before you begin

  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Document AI API.
  4. Set up authentication with a service account so you can access the API from your local workstation.

Installing the client library

npm install @google-cloud/documentai

Using the client library

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
// const processorId = 'YOUR_PROCESSOR_ID'; // Create processor in Cloud Console
// const filePath = '/path/to/local/pdf';

const {DocumentProcessorServiceClient} =
  require('@google-cloud/documentai').v1;

// Instantiates a client
// apiEndpoint regions available: eu-documentai.googleapis.com, us-documentai.googleapis.com (Required if using eu based processor)
// const client = new DocumentProcessorServiceClient({apiEndpoint: 'eu-documentai.googleapis.com'});
const client = new DocumentProcessorServiceClient();

async function quickstart() {
  // The full resource name of the processor, e.g.:
  // projects/project-id/locations/location/processor/processor-id
  // You must create new processors in the Cloud Console first
  const name = `projects/${projectId}/locations/${location}/processors/${processorId}`;

  // Read the file into memory.
  const fs = require('fs').promises;
  const imageFile = await fs.readFile(filePath);

  // Convert the image data to a Buffer and base64 encode it.
  const encodedImage = Buffer.from(imageFile).toString('base64');

  const request = {
    name,
    rawDocument: {
      content: encodedImage,
      mimeType: 'application/pdf',
    },
  };

  // Recognizes text entities in the PDF document
  const [result] = await client.processDocument(request);
  const {document} = result;

  // Get all of the document text as one big string
  const {text} = document;

  // Extract shards from the text field
  const getText = textAnchor => {
    if (!textAnchor.textSegments || textAnchor.textSegments.length === 0) {
      return '';
    }

    // First shard in document doesn't have startIndex property
    const startIndex = textAnchor.textSegments[0].startIndex || 0;
    const endIndex = textAnchor.textSegments[0].endIndex;

    return text.substring(startIndex, endIndex);
  };

  // Read the text recognition output from the processor
  console.log('The document contains the following paragraphs:');
  const [page1] = document.pages;
  const {paragraphs} = page1;

  for (const paragraph of paragraphs) {
    const paragraphText = getText(paragraph.layout.textAnchor);
    console.log(`Paragraph text:\n${paragraphText}`);
  }
}

Samples

Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

Sample Source Code Try it
Document_processor_service.batch_process_documents source code Open in Cloud Shell
Document_processor_service.create_processor source code Open in Cloud Shell
Document_processor_service.delete_processor source code Open in Cloud Shell
Document_processor_service.delete_processor_version source code Open in Cloud Shell
Document_processor_service.deploy_processor_version source code Open in Cloud Shell
Document_processor_service.disable_processor source code Open in Cloud Shell
Document_processor_service.enable_processor source code Open in Cloud Shell
Document_processor_service.evaluate_processor_version source code Open in Cloud Shell
Document_processor_service.fetch_processor_types source code Open in Cloud Shell
Document_processor_service.get_evaluation source code Open in Cloud Shell
Document_processor_service.get_processor source code Open in Cloud Shell
Document_processor_service.get_processor_type source code Open in Cloud Shell
Document_processor_service.get_processor_version source code Open in Cloud Shell
Document_processor_service.list_evaluations source code Open in Cloud Shell
Document_processor_service.list_processor_types source code Open in Cloud Shell
Document_processor_service.list_processor_versions source code Open in Cloud Shell
Document_processor_service.list_processors source code Open in Cloud Shell
Document_processor_service.process_document source code Open in Cloud Shell
Document_processor_service.review_document source code Open in Cloud Shell
Document_processor_service.set_default_processor_version source code Open in Cloud Shell
Document_processor_service.train_processor_version source code Open in Cloud Shell
Document_processor_service.undeploy_processor_version source code Open in Cloud Shell
Document_understanding_service.batch_process_documents source code Open in Cloud Shell
Document_understanding_service.batch_process_documents source code Open in Cloud Shell
Document_understanding_service.process_document source code Open in Cloud Shell
Document_processor_service.batch_process_documents source code Open in Cloud Shell
Document_processor_service.create_processor source code Open in Cloud Shell
Document_processor_service.delete_processor source code Open in Cloud Shell
Document_processor_service.delete_processor_version source code Open in Cloud Shell
Document_processor_service.deploy_processor_version source code Open in Cloud Shell
Document_processor_service.disable_processor source code Open in Cloud Shell
Document_processor_service.enable_processor source code Open in Cloud Shell
Document_processor_service.evaluate_processor_version source code Open in Cloud Shell
Document_processor_service.fetch_processor_types source code Open in Cloud Shell
Document_processor_service.get_evaluation source code Open in Cloud Shell
Document_processor_service.get_processor source code Open in Cloud Shell
Document_processor_service.get_processor_type source code Open in Cloud Shell
Document_processor_service.get_processor_version source code Open in Cloud Shell
Document_processor_service.import_processor_version source code Open in Cloud Shell
Document_processor_service.list_evaluations source code Open in Cloud Shell
Document_processor_service.list_processor_types source code Open in Cloud Shell
Document_processor_service.list_processor_versions source code Open in Cloud Shell
Document_processor_service.list_processors source code Open in Cloud Shell
Document_processor_service.process_document source code Open in Cloud Shell
Document_processor_service.review_document source code Open in Cloud Shell
Document_processor_service.set_default_processor_version source code Open in Cloud Shell
Document_processor_service.train_processor_version source code Open in Cloud Shell
Document_processor_service.undeploy_processor_version source code Open in Cloud Shell
Document_service.batch_delete_documents source code Open in Cloud Shell
Document_service.get_dataset_schema source code Open in Cloud Shell
Document_service.get_document source code Open in Cloud Shell
Document_service.import_documents source code Open in Cloud Shell
Document_service.list_documents source code Open in Cloud Shell
Document_service.update_dataset source code Open in Cloud Shell
Document_service.update_dataset_schema source code Open in Cloud Shell
Quickstart source code Open in Cloud Shell

The Document AI Node.js Client API Reference documentation also contains samples.

Supported Node.js Versions

Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js. If you are using an end-of-life version of Node.js, we recommend that you update as soon as possible to an actively supported LTS version.

Google's client libraries support legacy versions of Node.js runtimes on a best-efforts basis with the following warnings:

  • Legacy versions are not tested in continuous integration.
  • Some security patches and features cannot be backported.
  • Dependencies cannot be kept up-to-date.

Client libraries targeting some end-of-life versions of Node.js are available, and can be installed through npm dist-tags. The dist-tags follow the naming convention legacy-(version). For example, npm install @google-cloud/documentai@legacy-8 installs client libraries for versions compatible with Node.js 8.

Versioning

This library follows Semantic Versioning.

This library is considered to be stable. The code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against stable libraries are addressed with the highest priority.

More Information: Google Cloud Platform Launch Stages

Contributing

Contributions welcome! See the Contributing Guide.

Please note that this README.md, the samples/README.md, and a variety of configuration files in this repository (including .nycrc and tsconfig.json) are generated from a central template. To edit one of these files, make an edit to its templates in directory.

License

Apache Version 2.0

See LICENSE

changelog

Changelog

9.1.0 (2025-03-19)

Features

  • [documentai] Added config options to enable LLM layout parsing (#6180) (f0f3e3a)

9.0.0 (2025-03-18)

⚠ BREAKING CHANGES

  • upgrade to Node 18 (#6096)

Features

  • [Many APIs] add request/response debug logging to gapics (afcb5c0)

Miscellaneous Chores

8.12.0 (2024-10-30)

Features

  • [documentai] Updated the exponential backoff settings for the Document AI ProcessDocument and BatchProcessDocuments methods (#5762) (267abc2)

8.11.0 (2024-10-10)

Features

  • [documentai] Removed deprecated api versions (#5735) (05a5acf)

8.10.0 (2024-09-13)

Features

  • [documentai] Add API fields for the descriptions of entity type and property in the document schema (#5662) (c0995a2)

8.9.0 (2024-08-09)

Features

  • [documentai] A new field imageless_mode is added to message .google.cloud.documentai.v1.ProcessRequest (#5593) (2393568)

8.8.0 (2024-06-03)

Features

  • [documentai] Make Layout Parser generally available in V1 (#5393) (b6b04db)
  • [documentai] Make Layout Parser generally available in V1 (#5411) (750a6ac)

8.7.0 (2024-05-21)

Features

  • [documentai] update Nodejs generator to send API versions in headers for GAPICs (#5334) (62922e2)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5351) (01f48fc)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5354) (a9784ed)

8.6.0 (2024-04-23)

Features

  • [documentai] Support Chunk header and footer in Doc AI external proto (#5275) (b7ce5da)

8.5.0 (2024-04-18)

Features

  • [documentai] A new message FoundationModelTuningOptions is added (#5242) (71b7139)

8.4.0 (2024-04-16)

Features

  • [Many APIs] add several fields to manage state of database encryption update (#5188) (c5ccdf9)

8.3.0 (2024-04-05)

Features

  • [documentai] Support a new Layout Processor in Document AI (#5213) (ab38a59)

8.2.0 (2024-02-07)

Features

  • [documentai] expose model_type in v1 processor, so that user can see the model_type after get or list processor version (#5004) (83e2e5c)

8.1.2 (2024-01-05)

Bug Fixes

  • [documentai] correct long audio synthesis HTTP binding (#4884) (cca21ed)

8.1.1 (2023-12-11)

Bug Fixes

8.1.0 (2023-10-18)

Features

  • [documentai] added field Processor.processor_version_aliases (#4689) (effcc26)
  • [documentai] Added SummaryOptions to ProcessOptions for the Summarizer processor (#4696) (d02e044)

8.0.1 (2023-09-06)

Bug Fixes

  • [Many APIs] simplify logic for HTTP/1.1 REST fallback option (#4585) (b6cea45)

8.0.0 (2023-08-06)

⚠ BREAKING CHANGES

  • migrate to Node 14 (#4443)

Bug Fixes

  • [documentai] fix typings for IAM methods (#4423) (963accc)

Miscellaneous Chores

7.4.0 (2023-06-28)

Features

  • [documentai] add StyleInfo to document.proto (512ef37)
  • [documentai] add StyleInfo to document.proto (#4352) (512ef37)
  • Add IMPORTING enum to State in processor.proto (512ef37)
  • Add IMPORTING enum to State in processor.proto (512ef37)
  • Add PropertyMetadata and EntityTypeMetadata to document_schema.proto (512ef37)
  • Add REPLACE enum to OperationType in document.proto (512ef37)

7.3.1 (2023-04-13)

Bug Fixes

7.3.0 (2023-03-28)

Features

  • [documentai] Add ImportProcessorVersion in v1beta3 (#4109) (764faca)

7.2.0 (2023-03-13)

Features

  • [documentai] added hints.language_hints field in OcrConfig (#4062) (91b3811)

7.1.0 (2023-02-22)

Features

  • [documentai] Added Training and Evaluation functions, request, responses and metadata to document_processor_service.proto (#4008) (f81218f)

7.0.1 (2023-02-15)

Bug Fixes

  • [Many APIs] changing format of the jsdoc links (#3987) (07043a6)

7.0.0 (2023-02-09)

⚠ BREAKING CHANGES

  • gitignore: The TrainProcessorVersion parent was incorrectly annotated.

Miscellaneous Chores

  • gitignore: [documentai] feat: Added EvaluationReference to evaluation.proto (#3954) (9825fa9)

6.3.0 (2023-01-28)

Features

  • [documentai] added advanced_ocr_options field in OcrConfig (#3890) (babcf2f)
  • Added SuggestConversationSummary RPC (#3852) (d83d70a)
  • Exposed GetProcessorType to v1 (#3856) (1195e83)

6.2.0 (2022-12-16)

Features

  • Added process_options field in ProcessRequest in document_processor_service.proto (f9fc947)
  • Added process_options field in ProcessRequest in document_processor_service.proto (f9fc947)
  • Added sample_document_uris field in ProcessorType in processor_type.proto (f9fc947)
  • Added sample_document_uris field in ProcessorType in processor_type.proto (f9fc947)
  • Added sharding_config field in DocumentOutputConfig.GcsOutputConfig in document_io.proto (f9fc947)
  • Added sharding_config field in DocumentOutputConfig.GcsOutputConfig in document_io.proto (19e9aed)
  • Added sharding_config field in DocumentOutputConfig.GcsOutputConfig in document_io.proto (#3734) (19e9aed)
  • Added sharding_config field in DocumentOutputConfig.GcsOutputConfig in document_io.proto (#3750) (f9fc947)
  • Added TrainProcessorVersion, EvaluateProcessorVersion, GetEvaluation, and ListEvaluations v1beta3 APIs (#3632) (999b3d0)

Bug Fixes

6.1.0 (2022-11-11)

Features

  • Added font_family to document.proto (#377) (7ded563)
  • added ImageQualityScores message to document.proto (7ded563)
  • added PropertyMetadata and EntityTypeMetadata to document_schema.proto (7ded563)

Bug Fixes

6.0.0 (2022-09-22)

⚠ BREAKING CHANGES

  • Changed the name field for ProcessRequest and BatchProcessorRequest to accept * so the name field can accept Processor and ProcessorVersion.

Bug Fixes

Miscellaneous Chores

5.0.0 (2022-08-12)

⚠ BREAKING CHANGES

  • Changed the name field for ProcessRequest and BatchProcessorRequest to accept * so the name field can accept Processor and ProcessorVersion.

Documentation

4.1.0 (2022-07-01)

Features

4.0.0 (2022-06-20)

⚠ BREAKING CHANGES

  • update library to use Node 12 (#333)
  • mark Document.Entity.type as REQUIRED in all versions (#326)

Features

Bug Fixes

  • deps: update dependency @google-cloud/storage to v6 (#335) (0259e67)
  • mark Document.Entity.type as REQUIRED in all versions (#326) (8ef4cca)

Build System

3.3.0 (2022-02-22)

Features

  • add symbols field, and auto-format comments (ec5edb7)
  • add symbols field, and auto-format comments (ec5edb7)
  • add question_id field in ReviewDocumentOperationMetadata (#304) (e10cd03)
  • add question_id field in ReviewDocumentOperationMetadata (#308) (675aff9)

3.2.1 (2022-01-13)

Bug Fixes

  • documentai: add ancillary service bindings to service_yaml (#296) (14d1b31)

3.2.0 (2021-12-14)

Features

3.1.0 (2021-09-21)

Features

  • turns on self-signed JWT feature flag (#258) (e39c909)

Bug Fixes

3.0.2 (2021-08-17)

Bug Fixes

  • build: migrate to using main branch (#253) (ac879c4)
  • deps: google-gax v2.24.1 (#257) (4ff6b2d)
  • Update DocumentUnderstandingService default_host in v1beta2 from a regional endpoint, to non-regional (#252) (79b8d82)

3.0.1 (2021-07-21)

Bug Fixes

  • deps: google-gax v2.17.1 (#243) (a9e4efd)
  • Updating WORKSPACE files to use the newest version of the Typescript generator. (#245) (06bf319)

3.0.0 (2021-07-01)

⚠ BREAKING CHANGES

  • The default version of the API has changed from v1beta3 to v1.

Features

  • default to v1 of the API (#220) (5b370d0)
  • update document.proto, add the processor management methods. (#236) (ddc147f)
  • update ReviewDocumentRequest to allow set priority and enable validation (#238) (a39d9d5)

Bug Fixes

  • deps: google-gax v2.17.0 with mTLS (#241) (2576761)
  • GoogleAdsError missing using generator version after 1.3.0 (#224) (087b081)
  • make request optional in all cases (#237) (10e12bb)
  • update default host to documentai.googleapis.com (#217) (2d30ad0)

2.10.0 (2021-05-12)

Features

  • add confidence field to the PageAnchor.PageRef in document.proto (#206) (f3154a9)

Bug Fixes

2.9.0 (2021-04-26)

Features

  • add confidence field to the PageAnchor.PageRef in document.proto (#204) (580150b)

2.8.0 (2021-04-13)

Features

2.7.0 (2021-04-12)

Features

  • add EVAL_SKIPPED value to the Provenance.OperationType enum (#191) (161b3e0)

2.6.0 (2021-03-15)

Features

  • remove the translation fields in document.proto (#181) (d76272a)

2.5.0 (2021-02-24)

Features

  • Update documentai/v1beta3 protos: add support for boolean normalized value (#154) (4ae8c57)

2.4.0 (2021-02-17)

Features

2.3.0 (2021-01-09)

Features

2.2.2 (2020-12-01)

Bug Fixes

2.2.1 (2020-11-10)

Bug Fixes

2.2.0 (2020-10-17)

Features

2.1.1 (2020-07-09)

Bug Fixes

2.1.0 (2020-06-28)

Features

  • move ts target to es2018 from es2016 (#60) (73ab3b1)

Bug Fixes

  • proper fallback option handling (b4aef10)

2.0.0 (2020-05-15)

⚠ BREAKING CHANGES

  • The library now supports Node.js v10+. The last version to support Node.js v8 is tagged legacy-8 on NPM.

Features

  • check status of long running operation by its name (#54) (d88c23c)
  • drop node8 support (#30) (67ff888)

Bug Fixes

  • remove eslint, update gax, fix generated protos, run the generator (#37) (9e6cf01)
  • remove unused dependencies (#41) (27077e9)
  • deps: update dependency @google-cloud/storage to v5 (#56) (934f317)
  • samples: do not pass location (#39) (2ef2db5)

1.0.0 (2020-03-20)

⚠ BREAKING CHANGES

  • initial generation of beta client (#5)

Features

  • samples: adds Document AI parse table, forms samples (#8) (2fdf913)
  • adds beta samples (30a5392)
  • initial generation of beta client (#5) (e5a34f1)
  • updated library from protos (#9) (5ef7220)