Base64 encoding Stay organized with collections Save and categorize content based on your preferences.
When sending document files to the Document AI API, you can senddata directly in theRawDocument.content field withonline processingonly ifyour file is20 MB or less.The input file will be in a binary format, which must be encodedbefore sending to Document AI.
If your input file exceeds the online processing limits, it must be stored in aCloud Storage bucket in order to be sent for processing, which does notrequire encoding. Refer to thebatch processing documentation for details.
Using the command line
Within a gRPC request, you can simply write binary data out directly;however, JSON is used when making a REST request. JSONis a text format that does not directly support binary data, so you will need toconvert such binary data into text usingBase64 encoding.
Most development environments contain a nativebase64 utility toencode a binary into ASCII text data. To encode a file:
Linux
Encode the file using thebase64 command line tool, making sure to prevent line-wrapping by using the-w 0 flag:
base64INPUT_FILE -w 0 >OUTPUT_FILE
macOS
Encode the file using thebase64 command line tool:
base64 -iINPUT_FILE -oOUTPUT_FILE
Windows
Encode the file using theBase64.exe tool:
Base64.exe -eINPUT_FILE >OUTPUT_FILE
PowerShell
Encode the file using theConvert.ToBase64String method:
[Convert]::ToBase64String([IO.File]::ReadAllBytes("./INPUT_FILE")) >OUTPUT_FILECreate a JSON request file, inlining the base64-encoded data:
JSON
{"skipHumanReview":skipHumanReview,"rawDocument":{"mimeType":"MIME_TYPE","content":"BASE64_ENCODED_DATA"},"fieldMask":"FIELD_MASK"}
Using client libraries
Embedding binary data into requests through text editors is neitherdesirable or practical. In practice, you will be embedding base64 encoded fileswithin client code. All supported programming languages have built-in mechanismsfor base64 encoding content.
Python
# Import the base64 encoding library.importbase64# Pass the image data to an encoding function.defencode_image(image):withopen(image,"rb")asimage_file:encoded_string=base64.b64encode(image_file.read())returnencoded_stringNode.js
// Read the file into memory.varfs=require('fs');varimageFile=fs.readFileSync('/path/to/file');// Convert the image data to a Buffer and base64 encode it.varencoded=Buffer.from(imageFile).toString('base64');Java
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.StringencodedString=Base64.getEncoder().encodeToString(imageFile.getBytes());Go
import("bufio""encoding/base64""io""os")// Open image file.f,_:=os.Open("image.jpg")// Read entire image into byte slice.reader:=bufio.NewReader(f)content,_:=io.ReadAll(reader)// Encode image as base64.base64.StdEncoding.EncodeToString(content)Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.