How I build Astro + Sanity sites (step by step)

My practical sequence for building an Astro frontend with Sanity content, including a small query example and the publishing checks that matter.

Last updated:

Blueprints, a paper website prototype, and numbered tiles illustrating an Astro and Sanity workflow

This Astro Sanity tutorial follows the workflow I use to build Astro and Sanity sites: define the content first, connect a small published-content query, and turn it into reusable page templates. Then I add metadata, previews, publishing automation, and frontend checks. The goal is a website that editors can operate confidently, not just a successful local build.

This tutorial describes that workflow and includes a minimal service-list example. It assumes separate Astro and Sanity Studio projects, an existing Sanity project and public dataset, and Node.js versions supported by the versions you install. It is a starting point rather than a complete production starter.

What does an Astro Sanity tutorial need to cover?

Step 1: decide what editors need to change

Write down the content types and their relationships. A service business might need services, projects, posts, reusable FAQs, and site settings. Separate shared content from page-specific copy so editors do not maintain the same information in several places.

Also define what should remain controlled by the design. Unlimited layout controls are not always helpful; a small set of clear fields can make editing faster and more consistent.

For this example, a service has a title, slug, and summary. Add richer content only after that basic path works.

Step 2: define and register the Sanity schema

In the Studio project, create a service schema and register it in the schema types array used by your Sanity configuration:

import {defineField, defineType} from 'sanity';

export default defineType({
  name: 'service',
  title: 'Service',
  type: 'document',
  fields: [
    defineField({name: 'title', type: 'string',
      validation: rule => rule.required()}),
    defineField({name: 'slug', type: 'slug',
      options: {source: 'title'},
      validation: rule => rule.required()}),
    defineField({name: 'summary', type: 'text', rows: 3,
      validation: rule => rule.required()}),
  ],
});

Create one sample service and publish it. A document saved only as a draft should not appear in the public-content query used below. Sanity's document documentation explains the distinction between stored content and the schema used by Studio.

For a real project, add reusable SEO fields, descriptive image alt text, and validation that helps editors. Avoid requiring fields that do not have a meaningful use on the website.

Step 3: connect Astro to published content

For a minimal example, install the JavaScript client in the Astro project:

npm install @sanity/client

Create src/lib/cms.ts with the following server-side client. Set SANITY_PROJECT_ID and SANITY_DATASET in your local environment and deployment environment:

import {createClient} from '@sanity/client';

const projectId = import.meta.env.SANITY_PROJECT_ID;
const dataset = import.meta.env.SANITY_DATASET;
if (!projectId || !dataset) {
  throw new Error('Missing Sanity project configuration');
}

export const cms = createClient({
  projectId,
  dataset,
  apiVersion: '2026-11-02',
  perspective: 'published',
  useCdn: false,
});

This example assumes a public dataset and makes the request in Astro frontmatter, not browser JavaScript. A private dataset requires a suitably scoped server-side read token. Never expose a privileged token through a public environment variable or a client-side script.

For a larger project, the official Sanity Astro integration provides another supported configuration path. Choose one clear client setup rather than accidentally maintaining two with different dataset or perspective settings.

Step 4: render a small page before building the whole site

Create src/pages/services.astro. The query selects only the fields used by this simple page:

```astro

What I would do in a real project

I prefer a small, well-tested implementation that solves the client’s actual workflow. The technology matters, but clear ownership, publishing checks, and a maintainable handover matter more than a fashionable stack.

Common questions

How should I start evaluating this approach?

Start with one representative page or workflow, use realistic content, and test the full path from editing or development through the public visitor experience.

Is this approach right for every project?

No. Compare it with the simplest setup that meets the requirements, including the team’s editing needs, integrations, hosting, and ongoing maintenance.

What should I test before launch?

Test mobile layouts, error states, performance, accessibility, metadata, forms or checkout flows, publishing, and recovery from a failed build or integration.

When should I bring in a developer?

Bring in help when the content model, integrations, migration, testing, or deployment process has risks that the current team cannot confidently own.

Put the idea into practice

Want a content-led website with a clear publishing workflow? I can help plan and build the Astro and Sanity setup.

Explore Astro + Sanity development ↗

Let’s connect

Currently open to collaborations

info@awais-javed.com

Open email ↗

Choose your mail app or copy the address.