🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
CMS & Integrations••By Komal Nakrani

Astro Keystatic Setup: Git-Based Editing Without a Server

Set up Astro Keystatic: install the integration, map collections to your content folders, read entries with content collections, and switch to GitHub mode.

#Astro#Keystatic#Git-Based CMS#Content Collections

Keystatic is a Git-based CMS that adds an editing UI at /keystatic to your Astro project and saves content as Markdown, Markdoc, MDX, YAML or JSON files in your repository. For an Astro Keystatic setup, install @keystatic/core and @keystatic/astro, describe your content in keystatic.config.ts, and keep reading the files with normal content collections. Locally it writes to disk; in production it commits to GitHub.

This guide sets it up step by step on an Astro 7 site and covers the parts the quick start skips: matching your collection schema, images, singletons and deploying the admin.

Key Takeaways

  • Keystatic needs the React integration because its admin UI is a React app.
  • Point each Keystatic collection’s path at the same folder your Astro glob() loader reads.
  • Local mode is free and needs no server; GitHub mode needs an adapter for the admin routes.
  • Keep Zod schemas in src/content.config.ts aligned with Keystatic fields so builds fail on bad content.
  • Use singletons for one-off content such as site settings or homepage copy.

What Keystatic is and when to use it

Keystatic, made by Thinkmill, is open source and stores everything as files. There is no database and no content API to host. That makes it a natural match for Astro: the build still reads local files, and the CMS is just a friendlier way to edit them.

It fits best when one or a few editors need a form-based UI on a single site, and you want content to stay in Git with full history. It fits less well when you need scheduled publishing, complex roles, or content shared across several sites and apps; an API-based CMS handles those better. For a side-by-side view, see Keystatic vs TinaCMS vs Decap.

Storage mode Where edits go Needs a server Good for
local Files on your disk No (dev only) Developers, solo editors
github Commits to a GitHub repo Yes, for the admin routes Non-technical editors in production
cloud GitHub via Keystatic Cloud Yes Teams that want hosted auth and images

Install Keystatic in Astro

Keystatic’s UI is built with React, and its default rich text field writes Markdoc. Add both integrations, then the Keystatic packages:

npx astro add react markdoc
npm install @keystatic/core @keystatic/astro

Register the integration:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
import markdoc from '@astrojs/markdoc';
import keystatic from '@keystatic/astro';

export default defineConfig({
  integrations: [react(), markdoc(), keystatic()],
});

The integration injects two routes, /keystatic for the admin and /api/keystatic for its API. Run npm run dev and open http://localhost:4321/keystatic. You’ll see an empty dashboard until you add a config file.

Define collections in keystatic.config.ts

Create keystatic.config.ts at the project root. Each collection maps to a folder, and the path must match where your Astro content collection reads files:

// keystatic.config.ts
import { config, fields, collection, singleton } from '@keystatic/core';

export default config({
  storage: { kind: 'local' },
  collections: {
    posts: collection({
      label: 'Posts',
      slugField: 'title',
      path: 'src/content/posts/*',
      format: { contentField: 'content' },
      entryLayout: 'content',
      schema: {
        title: fields.slug({ name: { label: 'Title' } }),
        description: fields.text({ label: 'Description', multiline: true }),
        pubDate: fields.date({ label: 'Published', validation: { isRequired: true } }),
        draft: fields.checkbox({ label: 'Draft', defaultValue: true }),
        cover: fields.image({
          label: 'Cover image',
          directory: 'src/assets/posts',
          publicPath: '../../assets/posts/',
        }),
        content: fields.markdoc({ label: 'Content' }),
      },
    }),
  },
  singletons: {
    settings: singleton({
      label: 'Site settings',
      path: 'src/content/settings',
      schema: {
        siteTitle: fields.text({ label: 'Site title' }),
        announcement: fields.text({ label: 'Announcement bar' }),
      },
    }),
  },
});

A few details that save debugging time:

  • fields.slug produces both the title and the file name. A post titled “Hello World” is saved as src/content/posts/hello-world.mdoc.
  • format: { contentField: 'content' } puts frontmatter fields in YAML and the body in the file content, which is what Astro’s glob loader expects.
  • Keystatic also offers fields.mdx() if your collection uses MDX instead of Markdoc. Pick one format per collection and use the matching file extension in your loader pattern.
  • The image field’s directory and publicPath decide where uploads go and what path is written into frontmatter. Storing them under src/assets/ lets Astro optimize them.

Read Keystatic content with content collections

Because Keystatic only writes files, Astro doesn’t need the Keystatic runtime to build the site. Define a normal collection over the same folder:

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

const posts = defineCollection({
  loader: glob({ pattern: '**/*.mdoc', base: './src/content/posts' }),
  schema: ({ image }) =>
    z.object({
      title: z.string(),
      description: z.string().optional(),
      pubDate: z.coerce.date(),
      draft: z.boolean().default(false),
      cover: image().optional(),
    }),
});

export const collections = { posts };

Singletons are different. One saved at src/content/settings is written as src/content/settings.yaml, a single object without an id. Astro’s file() loader expects an array or an object keyed by ID, so read singletons with Keystatic’s reader API (below) or a small custom loader. The content collections docs cover loaders and schemas in more detail.

Pages then use getCollection('posts') and render() exactly as they would with hand-written Markdown. Filter drafts in production:

// src/lib/posts.ts
import { getCollection } from 'astro:content';

export const getPublishedPosts = () =>
  getCollection('posts', ({ data }) => import.meta.env.DEV || !data.draft);

Or use the Keystatic reader API

Keystatic ships a reader that understands your config, which is handy for singletons and nested fields:

---
// src/components/Announcement.astro
import { createReader } from '@keystatic/core/reader';
import keystaticConfig from '../../keystatic.config';

const reader = createReader(process.cwd(), keystaticConfig);
const settings = await reader.singletons.settings.read();
---
{settings?.announcement && <p class="announcement">{settings.announcement}</p>}

The reader works at build time and in on-demand routes running on Node.js. For blog posts, content collections are still the better choice because you get Astro’s schema validation, render() and image handling.

Move from local mode to GitHub mode

Local mode only works where the files are, which in practice means astro dev on a developer’s machine. To let editors use the admin on the live site, switch storage to GitHub:

// keystatic.config.ts (storage section)
storage: import.meta.env.PROD
  ? { kind: 'github', repo: 'your-org/your-site' }
  : { kind: 'local' },

Then:

  1. Add a server adapter, such as @astrojs/node, @astrojs/netlify, @astrojs/vercel or @astrojs/cloudflare. The /keystatic and /api/keystatic routes render on demand; the rest of the site can stay prerendered. The adapters explainer helps pick one.
  2. Run the dev server with GitHub storage once and follow Keystatic’s setup screen. It creates a GitHub App and writes KEYSTATIC_GITHUB_CLIENT_ID, KEYSTATIC_GITHUB_CLIENT_SECRET, KEYSTATIC_SECRET and PUBLIC_KEYSTATIC_GITHUB_APP_SLUG to .env.
  3. Add the same variables to your host’s environment settings.

Editors sign in with GitHub, and each save becomes a commit (or a branch, if you enable that workflow). Your host rebuilds on push, so the site updates a minute or two later.

If you prefer a fully static deploy with no adapter, keep local mode and register the integration only in development, for example by building the integrations array conditionally on an environment variable. Editors then edit locally and push, which suits small teams.

Common problems

The admin loads but shows no entries. The collection path doesn’t match where files live, or the glob is missing the *. Compare path with the actual folder.

Build fails on images. The image field wrote a path that Astro’s image() helper can’t resolve. Make publicPath relative to the content file, as in the config above.

Dates fail validation. Keystatic’s date field writes 2026-11-02. Use z.coerce.date() in the Astro schema so the string becomes a Date.

The build complains about on-demand routes. You’re in GitHub mode without an adapter. Add one, or limit Keystatic to dev.

Who Keystatic suits

Keystatic is a good default for content-heavy marketing sites, blogs and small business sites where an editor changes text and images a few times a week. Because it reads the same folders a theme already uses, you can add it to an existing project in an afternoon. Komal Nakrani themes keep content in standard Astro folders, so a blog and editorial theme can be wired to Keystatic by pointing collection paths at its content directory.

The official Keystatic Astro installation guide and Astro’s Keystatic CMS guide are worth keeping open while you set it up.

FAQ

Is Keystatic free to use with Astro?

Yes. Keystatic is open source, and local and GitHub storage modes have no fees beyond your hosting. Keystatic Cloud is an optional hosted service for authentication and image handling; check its site for current plans.

Does Keystatic work with a static Astro site?

Your public pages can stay fully static. Only the admin routes need on-demand rendering, and only in GitHub or Cloud mode. In local mode you can run Keystatic in development and deploy a static build with no adapter.

Can Keystatic edit MDX files in Astro?

Yes. Use fields.mdx() for the content field and an .mdx glob pattern in your loader. Components inside MDX need matching Keystatic component definitions if editors should insert them from the UI.

Where does Keystatic store content?

In your repository, as files under the path you configure for each collection or singleton. In local mode it writes to disk; in GitHub mode it commits through the GitHub API. Nothing is stored in a separate database.

Keystatic or Decap CMS for Astro?

Keystatic has a typed TypeScript config, a modern React UI and a reader API. Decap is a single script with a YAML config and no adapter needed for its GitHub backend. Choose Keystatic for developer ergonomics and Decap for the simplest static setup.