Create processor (v1)

Create a new Document AI processor.

Code sample

Node.js

For more information, see the Document AI Node.js API reference documentation.

To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {DocumentProcessorServiceClient} = require('@google-cloud/documentai');
const {status} = require('@grpc/grpc-js');

const client = new DocumentProcessorServiceClient();

/**
 * Creates a new Document AI processor.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 */
async function createProcessor(projectId, locationId) {
  try {
    const parent = client.locationPath(projectId, locationId);
    const processor = {
      displayName: 'Form Processor',
      type: 'FORM_PARSER_PROCESSOR',
    };

    const request = {
      parent,
      processor,
    };

    const [response] = await client.createProcessor(request);
    console.log(`Processor created: ${response.name}`);
    console.log(`Display name: ${response.displayName}`);
    console.log(`Type: ${response.type}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.error('Processor already exists in the specified location.');
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid processor configuration provided.');
    } else {
      console.error(`Failed to create processor: ${err.message}`);
    }
  }
}

module.exports = { createProcessor };

Python

For more information, see the Document AI Python API reference documentation.

To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import google.api_core.exceptions
from google.cloud import documentai_v1

client = documentai_v1.DocumentProcessorServiceClient()


def create_processor(project_id: str, location_id: str) -> None:
    """Creates a Document AI processor.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id').
        location_id: The Google Cloud location or region where the processor is created (for example, 'us' or 'eu').
    """
    parent = f"projects/{project_id}/locations/{location_id}"
    processor = documentai_v1.Processor(
        display_name="Example OCR Processor",
        type_="OCR_PROCESSOR",
    )

    try:
        response = client.create_processor(parent=parent, processor=processor)
        print(f"Created processor: {response.name}")
        print(f"Display name: {response.display_name}")
        print(f"Type: {response.type_}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to create processor: {error}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.