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:

---
import { sanityClient } from 'sanity:client'

const posts = await sanityClient.fetch(
  `*[_type == "post" && defined(slug)] | order(publishedAt desc){
    _id,
    title,
    slug,
    publishedAt
  }`
)
---

<h1>Blog</h1>
<ul>
  {posts.map((post) => (
    <li>
      <a href={`/blog/${post.slug.current}`}>{post.title}</a>
    </li>
  ))}
</ul>

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:

---
import { sanityClient } from 'sanity:client'

export async function getStaticPaths() {
  const posts = await sanityClient.fetch(
    `*[_type == "post" && defined(slug)]{
      "slug": slug.current,
      title,
      body
    }`
  )

  return posts.map((post) => ({
    params: { slug: post.slug },
    props: { post },
  }))
}

const { post } = Astro.props
---

<h1>{post.title}</h1>

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():

---
import { sanityClient } from 'sanity:client'

const slug = Astro.params.slug

const post = await sanityClient.fetch(
  `*[_type == "post" && slug.current == $slug][0]{
    _id,
    title,
    body,
    "author": author->{name, image}
  }`,
  { slug }
)
---

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:

npm install groq
import { defineQuery } from 'groq'

export const postsQuery = defineQuery(
  `*[_type == "post" && defined(slug)] | order(publishedAt desc){
    _id,
    title,
    slug,
    publishedAt
  }`
)

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:

*[_type == "post" && slug.current == $slug][0]{
  title,
  body,
  "author": author->{
    name,
    "imageUrl": image.asset->url
  },
  "categories": categories[]->{
    title,
    slug
  }
}

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:

---
import { sanityClient } from 'sanity:client'

interface Post {
  _id: string
  title: string
  slug: { current: string }
  publishedAt: string
}

const posts = await sanityClient.fetch<Post[]>(
  `*[_type == "post" && defined(slug)] | order(publishedAt desc)`
)
---

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:

import type { QueryParams } from 'sanity'
import { sanityClient } from 'sanity:client'

const visualEditingEnabled =
  import.meta.env.PUBLIC_SANITY_VISUAL_EDITING_ENABLED === 'true'
const token = import.meta.env.SANITY_API_READ_TOKEN

export async function loadQuery<T>({
  query,
  params,
}: {
  query: string
  params?: QueryParams
}) {
  if (visualEditingEnabled && !token) {
    throw new Error(
      'The `SANITY_API_READ_TOKEN` environment variable is required during Visual Editing.'
    )
  }

  const perspective = visualEditingEnabled ? 'drafts' : 'published'

  const { result, resultSourceMap } = await sanityClient.fetch<T>(
    query,
    params ?? {},
    {
      filterResponse: false,
      perspective,
      resultSourceMap: visualEditingEnabled
        ? 'withKeyArraySelector'
        : false,
      stega: visualEditingEnabled,
      ...(visualEditingEnabled ? { token } : {}),
      useCdn: !visualEditingEnabled,
    }
  )

  return {
    data: result,
    sourceMap: resultSourceMap,
    perspective,
  }
}

Then use it in your components:

---
import { loadQuery } from '../../lib/load-query'

interface Post {
  _id: string
  title: string
  body: unknown
}

const { slug } = Astro.params
if (!slug) return Astro.redirect('/404')

const { data: post } = await loadQuery<Post>({
  query: `*[_type == "post" && slug.current == $slug][0]{
    _id,
    title,
    body
  }`,
  params: { slug },
})
---

<h1>{post.title}</h1>

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?

Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change site theme