Sanity and Astro
Build content-driven Astro websites with Sanity. The integration, key features, and resources to get started.
Sanity gives Astro developers a structured content backend with real-time collaboration, a customizable editing environment, and APIs designed for both static and server-rendered sites. Sanity provides the content infrastructure while Astro handles the frontend.
Get started
- Quick start: get a working Astro + Sanity project running in minutes.
- Starter template: clone a preconfigured project with Sanity Studio and content fetching already wired up.
- Blog guide: build a complete blog with Astro and Sanity from scratch, with more details and explanation than the quick start.
The @sanity/astro integration
@sanity/astro is the official Astro integration for Sanity. It configures the Sanity client as a virtual module in your Astro project and optionally embeds Sanity Studio on a route.
Install it inside an existing Astro project:
npx astro add @sanity/astropnpm dlx astro add @sanity/astroyarn dlx astro add @sanity/astrobunx astro add @sanity/astroTo embed Sanity Studio in your project, also add @astrojs/react with npx astro add @astrojs/react. In @sanity/astro 4.0.0 and later, the Visual Editing component works without it.
This adds @sanity/astro to your astro.config.mjs and makes the Sanity client available through the sanity:client virtual module. For full configuration options, see Configuring @sanity/astro.
The integration provides:
- Sanity client (
sanityClient): a preconfigured client available throughimport { sanityClient } from "sanity:client". - Embedded studio: mount Sanity Studio as a route in your Astro app using
studioBasePath. - Visual Editing: click-to-edit overlays for draft content in the Presentation Tool (requires SSR).
- Stega encoding: invisible source mapping that connects rendered content back to its source documents.
- Content collections (experimental):
sanityLoaderfrom@sanity/astro/loaderloads a GROQ query into the Astro Content Layer, sogetCollection()andgetEntry()work with Sanity documents. Requires Astro 5 or later. - Multiple clients: the
clientsoption reads from several projects or datasets in one site, andsanity:clientaddsgetClient(name)to select one. - Runtime configuration: the
envoption readsprojectIdanddatasetfrom environment variables when the server runs, so one build can serve several projects or datasets. Requires@sanity/astro4.0.0 or later and Astro 5 or later. - Live collections (experimental): the
experimental_liveoption generates typed live collection loaders from your Sanity schema. Requires@sanity/astro4.0.0 or later and Astro 6 or later.
How Astro differs from Next.js
If you're coming from next-sanity, there are some differences worth understanding.
Astro defaults to static site generation. Pages are pre-rendered at build time, which means content is fetched once during the build rather than on every request. This is great for performance but means content updates require a rebuild unless you switch to server-side rendering.
The @sanity/astro integration is lighter than next-sanity. It focuses on client configuration, studio embedding, Visual Editing, and content collection loaders. Some features available in next-sanity don't have equivalents here:
- No Live Content API helpers:
next-sanityprovidesdefineLiveandSanityLivefor automatic real-time content updates and cache revalidation.@sanity/astrodoesn't have a stable equivalent. Content updates require a rebuild (static mode) or a page refresh (SSR mode) unless you implement the live content integration yourself. - Experimental live options:
@sanity/astro4.0.0 adds two experimental options that narrow the gap: live collection loaders for Astro 6 and later, which fetch content on every request, and an in-place refresh for Visual Editing previews. - No built-in cache revalidation:
next-sanityintegrates with the Next.js data cache for time-based, tag-based, and path-based revalidation. In Astro, caching behavior depends on your rendering mode and hosting adapter. - No webhook validation helper:
next-sanityexportsparseBodyfor secure webhook signature validation. For Astro, validate signatures in an API endpoint withisValidSignaturefrom@sanity/webhook.
Key features
Visual Editing
Visual Editing lets content editors click directly on rendered content in the Presentation Tool to open the corresponding field in the studio. It uses stega encoding to invisibly map rendered text back to its source document and field.
Visual Editing in Astro requires server-side rendering. Static pages can't encode stega strings at request time. Set output: 'server' in your Astro config for project-wide SSR, or keep the default output: 'static' and add export const prerender = false to individual pages that need server rendering. Either way, server-rendered pages need a deployment adapter for your hosting platform. See Visual Editing with Astro for setup instructions.
Embedded studio
You can mount Sanity Studio as a route inside your Astro application using the studioBasePath option. This means your content editing environment lives at a path like /admin in the same deployment as your frontend. See Embedding Sanity Studio in Astro for configuration details.
Portable Text and images
Sanity stores rich text as Portable Text, a JSON-based format that's framework-agnostic and queryable. The Sanity-maintained @portabletext/astro library renders Portable Text in Astro components with support for custom block types and annotations. For images, <SanityImage> renders image fields from the Sanity Image CDN through Astro’s image pipeline, with the studio crop and hotspot applied. See Images and Portable Text in Astro for usage patterns.
Next steps
- Astro quick start: set up a new project from scratch.
- Configure @sanity/astro: set up the client, environment variables, and integration options.
- Query content in Astro: fetch data with GROQ in
.astrocomponents. - Static and server rendering: choose the right rendering mode for your project.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?