| title | Quick Start | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| id | quick-start | ||||||||
| order | 2 | ||||||||
| description | Add TanStack CMS to a TanStack Start app: install the packages, run tanstack-cms init, mount the CMS routes with the Vite plugins, and open the admin. | ||||||||
| keywords |
|
You want a schema-driven, agent-native CMS in your TanStack Start app. The TanStack CMS packages ship the store, the admin UI, the review queue, the HTTP and MCP APIs, and the routes. Your app supplies its content model in cms.definition.ts and the runtime config in cms.config.ts.
For a full walkthrough of the blog example, open Build a Blog & Page CMS.
For live in-place block editing and AI image generation, open On-Page AI Editing & Images.
For connecting Claude Desktop or a browser agent, open Connect MCP & WebMCP Agents.
For porting an existing Payload, Starlight, EmDash, or WordPress site, open Migration Overview.
- An existing TanStack Start app (React) with a
vite.config.tsand asrc/router.tsx. - Node.js 22.13 or newer. The default storage adapter is embedded Postgres (PGlite), which runs in-process with no separate database server.
Install the three runtime packages:
react: @tanstack/cms-core @tanstack/cms-api @tanstack/cms-ui-react
Install the CLI as a dev dependency:
pnpm add -D @tanstack/cms-cli
# or: npm install -D @tanstack/cms-cliFrom the app's root directory:
npx tanstack-cms initinit makes four changes and prints what it did:
- Creates
src/cms.definition.tsandsrc/cms.config.ts(in the app root when there is nosrc/directory): the definition holds a starterpostcollection, and the config adds one seededhello-worldpost and theRegisterdeclaration that types the rest of the CMS. If either file exists, both are left alone unless you pass--force. - Patches
vite.config.ts(also.mts/.js). If the file callstanstackStart()with no arguments,initadds thetanstackCms()plugin andcreateRoutes(definition). IftanstackStartalready has options, it prints the snippet for you to add by hand. - Patches
src/router.tsx(orrouter.ts) to import./cms.configfor its side effect. If there is no router module, it prints the import line instead. - Adds
.data/to.gitignore, where the database and uploads live in development.
After init, your vite.config.ts should include tanstackCms() and pass createRoutes(definition) to TanStack Start:
import { defineConfig } from 'vite'
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
import { nitro } from 'nitro/vite'
import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { createRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'
import { definition } from './src/cms.definition'
export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
virtualRouteConfig: createRoutes(definition),
},
}),
nitro(),
viteReact(),
],
})tanstackCms()keeps the CMS packages (and@tanstack/ai*) out of Vite's SSR externals and dependency pre-bundling. The packages ship TanStack Start server functions and route files that your app's Start compiler must transform, so this plugin is required.createRoutes(definition)registers the CMS public, admin, and API routes without taking over your app'sroutesDirectory. Your own routes insrc/routeskeep working alongside them.vite.config.tsimports onlysrc/cms.definition.ts, never the runtime config.
A second argument says what to mount. Each surface (public, admin, api) takes true, false, a list of route keys, or an object that drops (false) or replaces (a file path) single routes, and custom adds your own routes:
createRoutes(definition, {
public: false,
admin: { agent: false },
custom: [route('/pricing', 'pricing.tsx')],
})The admin base path comes from the definition. To serve the admin at /admin/content, set it there:
// src/cms.definition.ts
defineCmsDefinition({ /* ... */ admin: { basePath: '/admin' } })See Routes in a Package for every route key and option.
src/router.tsx must import ./cms.config before ./routeTree.gen, so defineCms() runs before any route loader or server function reads the config:
import { createRouter } from '@tanstack/react-router'
import './cms.config'
import { routeTree } from './routeTree.gen'
export const getRouter = () => {
return createRouter({
routeTree,
scrollRestoration: true,
defaultPreloadStaleTime: 0,
})
}Because the router runs on both the client and the server, cms.config.ts is part of the browser bundle. Never put secrets in it directly; read them lazily inside functions such as AI adapter factories or webhook secret: () => process.env.WEBHOOK_SECRET.
init splits the CMS into two files. src/cms.definition.ts is the content model: collections, the admin base path, and plugin definitions. It imports only @tanstack/cms-core/definition, so vite.config.ts and client code can load it without pulling in the server:
import {
defineCmsDefinition,
defineCollection,
} from '@tanstack/cms-core/definition'
export const definition = defineCmsDefinition({
collections: [
defineCollection({
name: 'post',
label: 'Posts',
titleField: 'title',
summaryField: 'excerpt',
bylineField: 'author',
bodyField: 'body',
path: (_doc, id) => `/post/${id}`,
admin: { columns: ['author'] },
fields: [
{ name: 'title', label: 'Title', type: 'text', max: 120 },
{ name: 'slug', label: 'Slug', type: 'slug', required: false },
{ name: 'author', label: 'Author', type: 'text', required: false },
{ name: 'excerpt', label: 'Excerpt', type: 'textarea', required: false },
{ name: 'body', label: 'Body', type: 'markdown', required: false },
],
}),
],
})src/cms.config.ts registers the CMS with everything else: block types, seed data, auth, storage, AI, ops, and the server-bound hooks and access rules of each collection, keyed by collection name (collections: { post: { hooks, access } }):
import { defineCms } from '@tanstack/cms-core'
import { definition } from './cms.definition'
export const cms = defineCms(definition, {
blockTypes: [],
seed: {
documents: {
post: {
'hello-world': {
title: 'Hello world',
slug: 'hello-world',
body: 'Your first post. Edit it at /content.',
published: true,
},
},
},
},
})
declare module '@tanstack/cms-core' {
interface Register {
cms: typeof definition
}
}pathgives each document its public URL, used by the sitemap and the admin's "View" links.admin.columnspicks the extra columns shown in the content list.seedis written the first time the store is empty (or on demand withtanstack-cms seed).- The
Registerdeclaration typesRegisteredDoc<'post'>, the typed client, andadminPath()across your app.
Add collections with defineCollection() and block types with defineBlock(); see Collections & Fields.
The two-file split is recommended. A small app can keep everything in one cms.config.ts with defineCms({ collections, blockTypes, … }); that form keeps working. To split it later, run pnpm codemod definition-split <path-to-your-app> from a TanStack CMS checkout.
pnpm devOn first run the store creates an embedded Postgres (PGlite) database under .data/pglite, writes the seed, and serves:
/- Public home page/post/hello-world- The seeded post/content- Admin content list with search, filters, sorting, and bulk actions/edit/post/hello-world- Admin editor with drafts, revisions, and publishing/review- Review queue and audit trail/settings- API key management (admins only)/agent- Conversational content agent (only when a text AI adapter is configured)/api/cms/v1- HTTP API catalog (/api/cms/v1/openapi.jsonfor the OpenAPI document)/api/mcp- Streamable HTTP MCP server for external agents
Open /content and you get the content list for every collection, generated from your schema:
In development there is nothing to log in to. With no auth in defineCms(), every request is treated as a local admin while NODE_ENV is development (as under vite dev). Open /content and start editing.
To try the other roles (viewer, author, editor, admin) before wiring real auth, add createMockAuth(). It renders a persona switcher in the header, backed by a cms_mock_role cookie:
// src/cms.config.ts
import { defineCms } from '@tanstack/cms-core'
import { createMockAuth } from '@tanstack/cms-ui-react'
import { definition } from './cms.definition'
export const cms = defineCms(definition, {
blockTypes: [],
auth: createMockAuth(),
})An author, for example, can edit content but cannot publish or delete; their saves land in /review for an editor or admin to approve.
Auth is secure by default. In production, createMockAuth() signs no one in, and an app with no auth disables admin access entirely. Before deploying, pass auth: { getUser: async ({ request }) => ... } backed by your auth provider; see Authentication & Authorization.
TanStack CMS is fully functional without AI. AI features are opt-in: setting an environment variable such as ANTHROPIC_API_KEY does nothing on its own. You pass @tanstack/ai adapter factories to defineCms(), and those factories decide which keys to read:
// src/cms.config.ts
export const cms = defineCms(definition, {
// ...
ai: {
textAdapter: async () => {
if (!process.env.ANTHROPIC_API_KEY) return null
const { anthropicText } = await import('@tanstack/ai-anthropic')
return anthropicText('claude-sonnet-4-6')
},
imageAdapter: async () => {
if (!process.env.GEMINI_API_KEY) return null
const { geminiImage } = await import('@tanstack/ai-gemini')
return geminiImage('gemini-3.1-flash-image')
},
},
})Install @tanstack/ai-anthropic and @tanstack/ai-gemini (or any other @tanstack/ai adapters) yourself. Factories run on the server only, and the dynamic import() keeps provider SDKs out of the browser bundle. A factory that returns null means "not configured".
- A text adapter powers the content agent (
/agent), the editing agent in the document editor, and the on-page AI rewriter. - An image adapter powers AI image generation and in-place image editing.
When an adapter is missing, the matching AI controls are hidden from the UI and the AI operations respond with NOT_CONFIGURED.
The tanstack-cms CLI finds src/cms.config.ts and loads .env:
npx tanstack-cms migrate --status # show storage migrations (migrate applies them)
npx tanstack-cms seed # write defineCms({ seed }) documents (--force to re-apply)
npx tanstack-cms export --out content.json
npx tanstack-cms import content.json --mode merge
npx tanstack-cms backup --out backups/todayBefore deploying, configure real auth, pick a storage adapter (embedded PGlite on a persistent disk, or a Postgres server via createPostgresStorage() for production) and a media adapter (local disk or S3/R2), and review the security settings. The Production Deployment guide walks through each step. If you are moving from an earlier version, read Upgrading.
- Collections & Fields for field types, singletons, relations, and access rules
- Building Pages & Custom Routes for custom routes,
BlocksView,cmsHead, and server loaders - On-Page Visual & AI Editor for inline selection and scoped AI rewrites
- Authentication & Authorization to replace
createMockAuth()with Better Auth, Clerk, or OAuth
