Search + K

Command Palette

Search for a command to run...

Sign In

Google Docs is centered on a single resource: a Document identified by a documentId. Most work is: create a document (or receive one from a user), read it to learn where content sits, and issue a single atomic set of edits via batchUpdate to change it. There is no built-in document listing or search here—if you need to find an existing file by name you must get the documentId from the user or from an external Drive/query step outside this API.

Key entities and how they relate

  • A Document is the root resource and is addressed by documentId. The document contains structural content (body, structural elements, named ranges) and metadata (title, styles). Most interactions require that documentId be provided as the path parameter.

  • docs.documents.create returns a Document resource containing the new documentId. Use that ID for subsequent reads and updates.

  • docs.documents.get returns the Document resource. The response is where you inspect content ranges, startIndex/endIndex values, and structural nodes you will target for edits.

  • docs.documents.batchUpdate takes a single request object that contains an ordered requests array. The whole array is applied atomically: either all edits succeed or none are applied. The response contains per-request replies; it is not a full refreshed Document snapshot. If you need a fresh view of the document after edits, call get again.

  • Documents that use tabs (multi-tab layout) can surface content in Document.tabs instead of the text fields. Use the includeTabsContent flag to control which fields are populated by get.

What to call first (entry points)

  • If the user gives you a doc URL or documentId, call docs.documents.get to retrieve the current structure and content. Inspect body.content (or Document.tabs when includeTabsContent=true) to find startIndex/endIndex values and structural nodes to target.

  • If the user asks to create a new document, call docs.documents.create with a Document resource containing at least a title. The response includes the new documentId.

  • To modify a document, call docs.documents.batchUpdate with a requests array that describes the edits. Prefer composing multiple logical changes into one batchUpdate call so changes are applied atomically.

There is no API here to list documents or search by title—if you must find an existing doc and the user cannot provide its documentId, you need to obtain the ID from the user or use a Drive API call outside this scope.

Common user tasks and how to accomplish them

Each example below assumes you have a documentId. Use get first when you need the document's structure or numeric content indices.

Create a new document with a title (and optional initial text)

  • Call docs.documents.create with a Document resource that sets at least title. If you want initial content, include the corresponding body structure in the create request. The response includes the created Document and its documentId.

Read a document to show or analyze content

  • Call docs.documents.get with the documentId. If the document uses tabs and you specifically need per-tab content, set includeTabsContent=true; otherwise the first tab's content will populate the top-level body fields. Use suggestionsViewMode when you need to view suggestions inline vs. previewing accepted changes.

Append or insert text without computing numeric indices

  • Prefer ReplaceAllText (when you want to update text occurrences) or use InsertText at a location derived from get results. If you can avoid index arithmetic, do so: ReplaceAllText works on matching text and is safer for concurrent edits.

Make multiple edits atomically (recommended for non-trivial updates)

  • Build a requests array with the individual edit requests (InsertText, DeleteContentRange, ReplaceAllText, UpdateParagraphStyle, etc.) and send them in a single docs.documents.batchUpdate call. The array order matters for how edits are applied.

Apply formatting to a range

  • Read the document to find the startIndex/endIndex for the target range. Then include style-update requests (e.g., UpdateTextStyle or UpdateParagraphStyle) in the batchUpdate requests array that reference those numeric indices.

Replace text across the document (idempotent pattern)

  • Use ReplaceAllText in batchUpdate to match and replace text globally. This avoids fragile index calculations and is safer across concurrent edits.

Insert structural elements (tables, lists, named ranges)

  • Use the corresponding request types inside batchUpdate (InsertTable, CreateParagraphBulletedList, CreateNamedRange, etc.). If the request requires numeric indices, take them from the get response.

After applying edits and needing the canonical current document

  • Call docs.documents.get again to fetch the up-to-date Document resource. batchUpdate replies summarize effects per request but are not a guaranteed full snapshot.

Non-obvious behaviors and gotchas

  • documentId is mandatory for read and write operations. This API does not provide a document listing endpoint. If the user cannot supply a documentId, create a new document or obtain the ID from an external Drive search.

  • includeTabsContent switches where content appears in the returned Document: when true, Document.tabs is populated and the top-level body/text fields are not. Use it only when you specifically need per-tab structures; otherwise rely on the default behavior that populates top-level content for the first tab.

  • suggestionsViewMode affects how suggestions are presented in get responses (inline, preview accepted, or default for the user's access). If a user asked to view or export suggested changes, set the appropriate mode on get.

  • Edits that use numeric startIndex and endIndex require a fresh document snapshot. These numeric offsets are present on elements returned by get (inspect body.content items for their indices). If you compute indices from an old snapshot, concurrent edits may invalidate them and cause unexpected results.

  • Prefer request types that do not rely on numeric indices when possible (for example, ReplaceAllText) because they are more robust across concurrent edits and avoid index arithmetic.

  • batchUpdate applies its requests array atomically. If you need multiple logical steps to succeed together, include them in a single batchUpdate. The per-request replies explain what changed but are not a substitute for fetching the full updated Document.

  • The create operation returns the newly created Document in the response body—including its documentId—so capture and return that ID to the user after creating a document.

Practical checklist for handling a user request

  1. Determine the target document: get documentId from the user or create one via docs.documents.create.
  2. If you will use index-based edits or need to inspect structure, call docs.documents.get to read body.content (or Document.tabs when appropriate) and obtain indices and node locations.
  3. Compose a single docs.documents.batchUpdate call with an ordered requests array for all edits you want applied together. Where possible favor ReplaceAllText or request forms that avoid fragile index math.
  4. After batchUpdate, if you need to present the post-edit canonical document to the user, call docs.documents.get again.

Follow these patterns and you will handle the majority of user requests for creating, reading, and updating Google Docs documents with predictable, atomic results.