After you have set up your Google Cloud account and created a processor, you can send a request to your Document AI processor.
The code used to send the request is the same for all processors. You see differences in processor functioning in the information each processor outputs.
Using with the v1 API version of Document AI or in Google Cloud console, you
can send processing requests to that specific processor version. If you don't
specify a processor version, the default version is used.
For more information, see Managing processor versions.
Online processing
Online (synchronous) requests let you send a single document for processing.
Document AI immediately processes the request and returns a document.
Send request to a processor
The following code samples show you how to send a request to a processor.
REST
This sample shows you how to provide document content (raw document content in bytes
via a base64 encoded string) in the rawDocument
object.
Alternatively, you could also specify inlineDocument,
which is the same Document JSON format returned
by Document AI. This allows you to chain requests by passing the same format back and forth
(for example, if you classify a document and then extract its content).
Before using any of the request data, make the following replacements:
- LOCATION: your processor's location, for example:
us- United Stateseu- European Union
- PROJECT_ID: Your Google Cloud project ID.
- PROCESSOR_ID: the ID of your custom processor.
- skipHumanReview: A boolean to disable human review (Supported by Human-in-the-Loop processors only.)
true- skips human reviewfalse- enables human review (default)
- MIME_TYPE†: One of the valid MIME type options.
- IMAGE_CONTENT†: One of the valid
Inline document content, represented as
a stream of bytes. For JSON representations, the base64
encoding (ASCII string) of your binary image data. This string should look similar to the
following string:
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
- FIELD_MASK: Specifies which fields to include in the
Documentoutput. This is a comma-separated list of fully qualified names of fields inFieldMaskformat.- Example:
text,entities,pages.pageNumber
- Example:
- INDIVIDUAL_PAGES: A list of individual pages to process.
† This content can also be specified using base64-encoded content in the
inlineDocument object.
HTTP method and URL:
POST https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID:process
Request JSON body:
{
"skipHumanReview": skipHumanReview,
"rawDocument": {
"mimeType": "MIME_TYPE",
"content": "IMAGE_CONTENT"
},
"fieldMask": "FIELD_MASK",
"processOptions": {
"individualPageSelector" {
"pages": [INDIVIDUAL_PAGES]
}
}
}
To send your request, choose one of these options:
curl
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID:process"
PowerShell
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID:process" | Select-Object -Expand Content
If the request is successful, the server returns a 200 OK HTTP status code and the
response in JSON format. The response body contains an instance of
Document.
Send request to a processor version
Before using any of the request data, make the following replacements:
- LOCATION: your processor's location, for example:
us- United Stateseu- European Union
- PROJECT_ID: Your Google Cloud project ID.
- PROCESSOR_ID: the ID of your custom processor.
- PROCESSOR_VERSION: the processor version identifier. Refer to Select a processor version for more information. For example:
pretrained-TYPE-vX.X-YYYY-MM-DDstablerc
- skipHumanReview: A boolean to disable human review (Supported by Human-in-the-Loop processors only.)
true- skips human reviewfalse- enables human review (default)
- MIME_TYPE†: One of the valid MIME type options.
- IMAGE_CONTENT†: One of the valid
Inline document content, represented as
a stream of bytes. For JSON representations, the base64
encoding (ASCII string) of your binary image data. This string should look similar to the
following string:
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
- FIELD_MASK: Specifies which fields to include in the
Documentoutput. This is a comma-separated list of fully qualified names of fields inFieldMaskformat.- Example:
text,entities,pages.pageNumber
- Example:
† This content can also be specified using base64-encoded content in the
inlineDocument object.
HTTP method and URL:
POST https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION:process
Request JSON body:
{
"skipHumanReview": skipHumanReview,
"rawDocument": {
"mimeType": "MIME_TYPE",
"content": "IMAGE_CONTENT"
},
"fieldMask": "FIELD_MASK"
}
To send your request, choose one of these options:
curl
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION:process"
PowerShell
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION:process" | Select-Object -Expand Content
If the request is successful, the server returns a 200 OK HTTP status code and the
response in JSON format. The response body contains an instance of
Document.
C#
For more information, see the Document AI C# 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.