Sanity Mutation icon

Sanity Mutation

Create, Read, Update, and Delete documents in Sanity.io

Get Documents

AI-generated

Summary

Fetch documents from Sanity.io using various modes: by document ID, by custom GROQ query, by document type with pagination, or an automatic mode that selects between query and ID based on input.

Inputs

  • Get Mode (required) — Choose how to fetch documents: 'auto' (uses GROQ query if provided, otherwise by document ID), 'byId' (single document by ID), 'byQuery' (custom GROQ query), or 'byType' (documents by _type with pagination).
  • Document ID — The ID of the document to fetch; required for 'byId' mode or used in 'auto' mode if no query is given.
  • GROQ Query — Custom GROQ query string to fetch documents; required for 'byQuery' mode and optionally used in 'auto' mode.
  • Document Type — Sanity _type value to fetch documents by, required for 'byType' mode.
  • Limit — Maximum number of documents to return; applies when fetching by document type.
  • Offset — Number of matching documents to skip before returning results; applies when fetching by document type.
  • API Version — Sanity API version to use; defaults to 'v2024-06-21'.

Output shape

A JSON array of document objects fetched from Sanity.io.

The output array contains zero or more documents depending on the query result. In 'byId' or 'auto' modes fetching by ID, the array will have one document or be empty if not found. When an error occurs, the execution halts unless 'Continue On Fail' is enabled, in which case error details appear in the output for the respective input item.

Examples

Example 1: Fetch a single document by ID

Set Get Mode to 'By Document ID' and specify the Document ID.

Example 2: Fetch multiple documents by GROQ query

Set Get Mode to 'By GROQ Query' and provide a valid GROQ query string.

Example 3: Fetch paginated documents by type

Set Get Mode to 'By Document Type' and provide the document type, limit, and offset parameters.

Links

Discussion