Use Sanity with Astro

Query content in Astro

Use the @sanity/astro integration to fetch content with GROQ in your Astro components. Covers basic queries, dynamic routes, typed queries, and the loadQuery helper for Visual Editing.

The @sanity/astro integration provides a preconfigured Sanity client through the sanity:client virtual module. This guide shows how to query Sanity content with GROQ in Astro, from basic fetches to a loadQuery helper for Visual Editing.

Before you start, add the @sanity/astro integration to your Astro project and configure it with your project ID and dataset. For details, see Configuring @sanity/astro.

Basic queries

Import the preconfigured client from the sanity:client virtual module and call fetch() in your component's frontmatter:

The client is available in any .astro file's frontmatter, in API routes, and in server-side scripts. Queries use GROQ, Sanity's query language.

Dynamic routes with getStaticPaths

For statically generated pages, use Astro's getStaticPaths to create a page for each document:

Astro calls getStaticPaths at build time and generates one HTML file per route. Content changes require a rebuild to appear on the site.

Parameterized queries

Use GROQ parameters to avoid string interpolation in queries. Pass parameters as the second argument to fetch():

The client sends parameters to the API separately from the query, as JSON-encoded values, so a parameter value can't change the structure of the query. This prevents GROQ injection. Always prefer parameters over template literal interpolation.

This example reads Astro.params.slug without a getStaticPaths function, so the page must be rendered on demand: add export const prerender = false to the page's frontmatter, or set output: 'server' in your Astro config. For details, see Static and server rendering in Astro.

Use Sanity TypeGen

For generated types that stay in sync with your schema, use Sanity TypeGen. Define queries with defineQuery in separate .ts files and import them into your Astro components:

To generate the result types, run Sanity TypeGen against your extracted schema. Once the types exist, sanityClient.fetch(postsQuery) returns the generated result type without a generic. For the steps, see Type generation workflows in the Sanity TypeGen docs.

Query references and joins

GROQ supports following references inline with the -> operator:

This fetches the post along with its referenced author and category documents in a single query. For more GROQ query patterns, see the GROQ query cheat sheet.

Type query results

For basic type safety, use a generic on the fetch() call:

For generated types that stay in sync with your schema, use Sanity TypeGen with defineQuery queries instead of a hand-written interface.

The loadQuery helper for Visual Editing

When using Visual Editing, queries need additional configuration: a viewer token, the perspective, source maps, and stega encoding. Rather than adding these options to every fetch() call, create a shared loadQuery helper:

Then use it in your components:

To avoid maintaining Post by hand, define the query with defineQuery, generate its result type with Sanity TypeGen, and pass the generated type to loadQuery instead.

The helper transparently switches between published and draft content based on the PUBLIC_SANITY_VISUAL_EDITING_ENABLED environment variable. Visual Editing only works on server-side rendered pages. For a full setup, see Visual Editing with Astro.

Was this page helpful?