Overview
The Google Docs API allows you to create, read, and update Google Docs documents programmatically.
API Name: docs
Version: v1
Official Documentation: Docs API Reference
Common Resources
Documents
Create and manage Google Docs.
The document ID (found in the URL)
Common Methods
Create Document
gws docs documents create --json '{
"title": "Project Proposal"
}'
The ID of the created document
Get Document
gws docs documents get --params '{
"documentId": "1a2b3c4d5e6f7g8h9i"
}'
The document body containing content
Batch Update
gws docs documents batchUpdate \
--params '{"documentId": "1a2b3c4d5e6f7g8h9i"}' \
--json '{
"requests": [
{
"insertText": {
"location": {"index": 1},
"text": "Hello, World!\n"
}
}
]
}'
Document Operations
Insert text
Format text
Insert table
Insert image
gws docs documents batchUpdate \
--params '{"documentId": "1a2b3c4d5e6f7g8h9i"}' \
--json '{
"requests": [{
"insertText": {
"location": {"index": 1},
"text": "This is a new paragraph.\n"
}
}]
}'
Create document response:
{
"documentId" : "1a2b3c4d5e6f7g8h9i" ,
"title" : "Project Proposal" ,
"body" : {
"content" : [
{
"endIndex" : 1 ,
"sectionBreak" : {
"sectionStyle" : {}
}
}
]
},
"revisionId" : "abc123" ,
"documentStyle" : {}
}
Get document response:
{
"documentId" : "1a2b3c4d5e6f7g8h9i" ,
"title" : "Project Proposal" ,
"body" : {
"content" : [
{
"startIndex" : 1 ,
"endIndex" : 20 ,
"paragraph" : {
"elements" : [
{
"startIndex" : 1 ,
"endIndex" : 20 ,
"textRun" : {
"content" : "Hello, World! \n "
}
}
]
}
}
]
}
}
+write - Append text to a document
Key Resources
documents - Create, read, and update documents
Common Request Types
The batchUpdate method accepts various request types:
insertText - Insert text at a location
deleteContentRange - Delete a range of content
updateTextStyle - Format text (bold, italic, color, etc.)
updateParagraphStyle - Change paragraph formatting
insertTable - Insert a table
insertInlineImage - Insert an image
createNamedRange - Create a named range
replaceAllText - Find and replace text
Advanced Examples
Replace All Text
gws docs documents batchUpdate \
--params '{"documentId": "1a2b3c4d5e6f7g8h9i"}' \
--json '{
"requests": [{
"replaceAllText": {
"containsText": {"text": "{{name}}"},
"replaceText": "Alice Smith"
}
}]
}'
Create Heading
gws docs documents batchUpdate \
--params '{"documentId": "1a2b3c4d5e6f7g8h9i"}' \
--json '{
"requests": [
{
"insertText": {
"location": {"index": 1},
"text": "Introduction\n"
}
},
{
"updateParagraphStyle": {
"range": {"startIndex": 1, "endIndex": 14},
"paragraphStyle": {"namedStyleType": "HEADING_1"},
"fields": "namedStyleType"
}
}
]
}'
Learn More
Schema Inspection
Before calling any method, inspect its schema:
gws schema docs.documents.create
gws schema docs.documents.batchUpdate