List processors (v1)

List all processors in a specified project and location.

Code sample

Go

For more information, see the Document AI Go 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 (
	"context"
	"errors"
	"fmt"
	"io"

	documentai "cloud.google.com/go/documentai/apiv1beta3"
	"cloud.google.com/go/documentai/apiv1beta3/documentaipb"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

// listProcessors lists all processors in the specified project and location.
func listProcessors(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	endpoint := fmt.Sprintf("%s-documentai.googleapis.com:443", locationID)
	client, err := documentai.NewDocumentProcessorClient(ctx, option.WithEndpoint(endpoint))
	if err != nil {
		return fmt.Errorf("NewDocumentProcessorClient failed: %w", err)
	}
	defer client.Close()

	req := &documentaipb.ListProcessorsRequest{
		Parent: parent,
	}

	it := client.ListProcessors(ctx, req)
	for {
		processor, err := it.Next()
		if errors.Is(err, iterator.Done) {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to list processors: %w", err)
		}

		fmt.Fprintf(w, "Processor Name: %s\n", processor.GetName())
		fmt.Fprintf(w, "Display Name: %s\n", processor.GetDisplayName())
		fmt.Fprintf(w, "Type: %s\n", processor.GetType())
		fmt.Fprintf(w, "State: %s\n", processor.GetState())
	}

	return nil
}

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');

/**
 * Lists Document AI processors in a given project and location.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud location (for example, 'us-central1')
 */
async function listProcessors(projectId, locationId) {
  const client = new DocumentProcessorServiceClient({
    apiEndpoint: `${locationId}-documentai.googleapis.com`,
  });

  try {
    const parent = client.locationPath(projectId, locationId);
    const request = {
      parent,
    };

    const [processors] = await client.listProcessors(request);

    if (processors.length === 0) {
      console.log('No processors found.');
      return;
    }

    for (const processor of processors) {
      console.log(`Processor Name: ${processor.name}`);
      console.log(`Processor Type: ${processor.type}`);
      console.log(`Display Name: ${processor.displayName}`);
    }
  } catch (err) {
    if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid argument provided for listing processors:', err.message);
    } else if (err.code === status.NOT_FOUND) {
      console.error('Specified location or project was not found:', err.message);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error('Insufficient permissions to list processors:', err.message);
    } else {
      console.error('An error occurred while listing processors:', err);
    }
  }
}

module.exports = { listProcessors };

What's next

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