Quickstart: Setup the Document AI API

This guide provides all required setup steps to start using Document AI.

About the GCP Console

The Google Cloud Console is a web UI used to provision, configure, manage, and monitor systems that use GCP products. You use the Cloud Console to set up and manage Document AI resources.

Create a project

To use services provided by GCP, you must create a project.

A project organizes all your GCP resources. A project consists of the following components:

  • a set of collaborators
  • enabled APIs (and other resources)
  • monitoring tools
  • billing information
  • authentication and access controls

You can create one project, or you can create multiple projects. You can use your projects to organize your GCP resources in a resource hierarchy. For more information on projects, see the Resource Manager documentation.

No Console do Google Cloud, na página do seletor de projetos, selecione ou crie um projeto do Google Cloud.

Acessar a página do seletor de projetos

Enable billing

A billing account defines who pays for a given set of resources. Billing accounts can be linked to one or more projects. Project usage is charged to the linked billing account. You configure billing when you create a project. For more information, see the Billing documentation.

Verifique se o faturamento está ativado para seu projeto na nuvem. Saiba como confirmar se o faturamento está ativado para o projeto.

Enable the API

You must enable the Document AI API for your project. For more information on enabling APIs, see the Service Usage documentation.

Ative a Document AI API.

Ative a API

Set up authentication

Any client application that uses the API must be authenticated and granted access to the requested resources. This section describes important authentication concepts and provides steps for setting it up. For more information, see the GCP authentication overview.

About service accounts

There are multiple options for authentication, but it is recommended that you use service accounts for authentication and access control. A service account provides credentials for applications, as opposed to end-users. Projects own their service accounts. You can create many service accounts for a project. For more information, see Service accounts.

About roles

When calling an API, GCP requires the calling identity (any applicable person, entity, or process and their defined attributes) to have the appropriate permissions. You can grant permissions by granting roles to a service account. For more information, see the Identity and Access Management (IAM) documentation.

For the purpose of trying the Document AI API, you can use the Project > Owner role in steps below. The Project > Owner role grants the service account full permission to resources in your project. If your service account does not require full permissions, you specify a more restrictive role using the Cloud Console. For a list of roles and permissions, see IAM permissions reference. For information on managing permissions using IAM roles, see granting roles to service accounts.

About service account keys

Service accounts are associated with one or more public/private key pairs. When you create a new key pair, you download the private key. The Cloud SDK uses your private key to generate credentials when calling the API. You are responsible for security of the private key and other management operations, such as key rotation.

Create a service account and download the private key file

Configurar a autenticação:
  1. No Console do Cloud, acesse a página Criar chave da conta de serviço.

    Acessar página "Criar chave da conta de serviço"
  2. Na lista Conta de serviço, selecione Nova conta de serviço.
  3. No campo Nome da conta de serviço, insira um nome.
  4. Na lista Papel, selecione Projeto > Proprietário.

  5. Clique em Criar. O download de um arquivo JSON que contém sua chave é feito no seu computador.

Use the service account key file in your environment

Forneça credenciais de autenticação ao código do aplicativo definindo a variável de ambiente GOOGLE_APPLICATION_CREDENTIALS. Substitua [PATH] pelo caminho do arquivo JSON que contém sua chave da conta de serviço. Essa variável só se aplica à sessão de shell atual. Assim, se você abrir uma nova sessão, precisará definir a variável novamente.

Linux ou macOS

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

Exemplo:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"

Windows

Com o PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

Exemplo:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

Com prompt de comando:

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

Install and initialize the Cloud SDK

If you plan to use the Document AI API, you must install and initialize the Cloud SDK. Cloud SDK is a set of tools that you can use to manage resources and applications hosted on GCP. This includes the gcloud command line tool. The following link provides instructions:

Instale e inicialize o SDK do Cloud..

Test the SDK and authentication

If you have set up authentication in previous steps, you can use the gcloud tool to test your authentication environment. Execute the following command and verify that no error occurs and that credentials are returned:

gcloud auth application-default print-access-token

That command is used by all Document AI API command line REST samples to authenticate API calls.

Install the Document AI API client library

You have three options for calling the Document AI API:

The client libraries are available for several popular languages. For information on installing the client libraries, see Document AI API Client Libraries.