APIs and SDKs

Querying content with @sanity/client

Learn how to fetch content from your Sanity dataset using GROQ queries, perspectives, and document lookup methods with @sanity/client.

The Sanity client provides several ways to fetch content from your dataset: GROQ queries for flexible filtering and projections, direct document lookups by ID, and perspective controls for switching between published, draft, and release versions.

This article covers how to query with client.fetch(), use query parameters, control document versions with perspectives, and fetch documents by ID.

Prerequisites

Before querying content, make sure you have @sanity/client installed and configured. See Getting started with @sanity/client for setup instructions. To query draft or version documents, you also need an API token with read access.

Querying with client.fetch()

The fetch() method executes a GROQ query against your dataset and returns the results. It accepts the query string as the first argument, an optional parameters object as the second, and an optional options object as the third.

Replace <your-project-id> with your Sanity project ID, which you can find in your project's management dashboard.

Using query parameters

Query parameters prevent injection attacks and make queries reusable. Prefix parameters with $ in the GROQ query and pass their values as the second argument to fetch().

Controlling document versions with perspectives

Perspectives control which version of a document your query returns. This is how you switch between showing published content on your production site and showing draft or release versions in preview environments. You can set a default perspective when creating the client or override it per query.

Published perspective

The published perspective returns only published documents, excluding drafts and version documents. This is the default and is what you typically use for production sites.

Drafts perspective

The drafts perspective returns draft versions when they exist, falling back to published versions otherwise. Use this for preview environments where editors need to see their unpublished changes. It requires an API token and should not use the CDN, since drafts are not cached.

Raw perspective

The raw perspective returns every version of every matching document as separate entries: published, drafts, and release versions all appear in the results. This is useful when you need to compare versions or build custom editorial tooling that shows all document states at once.

Release perspectives

Beyond the built-in perspectives, you can query content as it would appear if a specific release were published. This uses a perspective stack, where release versions take priority over drafts, which take priority over published documents. See perspectives for details on configuring perspective stacks with release IDs.

Fetching documents by ID

When you already have a document's ID, use getDocument() for a single document or getDocuments() for multiple documents in one request. These methods return the full document without needing a GROQ query.

Use getDocuments() when you have a list of known IDs to resolve, such as IDs stored in an external system. For fetching related documents within your dataset, a GROQ query with references() or joins is usually more efficient since it avoids multiple round trips.

Visual editing

When visual editing is enabled, the client can enrich query responses with Content Source Maps, which are metadata that tracks where each piece of content came from. This powers click-to-edit overlays in Sanity Studio, allowing editors to select content on a preview page and jump directly to the corresponding field.

Next steps

Now that you can fetch content, you may want to learn how to modify it. See Creating mutations with @sanity/client for creating, patching, and deleting documents, or Creating transactions with @sanity/client for atomic multi-document operations. For request tagging, cancellation, and other configuration, see Advanced client patterns.

Was this page helpful?