Add to an Existing Site
If you already have an existing app, you can add Tenon to it. For specific examples and details to adding a database, storage, email and more, please look at the specific sections inside the docs. For demonstrative purposes and to keep it simple, this tutorial uses SQLite as the database and node as the target deployment.
To start from scratch instead, see the Quick Start.
Install the packages
Install Tenon and the SQLite driver (or use the built in node:sqlite and skip libsql):
npm install tenoncms @libsql/client
pnpm add tenoncms @libsql/client
yarn add tenoncms @libsql/client
bun add tenoncms @libsql/client
Tenon runs on the server, so your app needs a server build. Nuxt needs nothing else. Astro and TanStack Start need one more package:
npm install @astrojs/node
pnpm add @astrojs/node
yarn add @astrojs/node
bun add @astrojs/node
npm install -D nitro
pnpm add -D nitro
yarn add -D nitro
bun add -D nitro
Add a collection
A collection is a list of documents with the same fields. Create a file for a posts collection:
import { defineCollection } from "tenoncms";
export const posts = defineCollection({
titleField: "title",
fields: {
title: { type: "text", required: true },
slug: { type: "slug", from: "title", required: true, unique: true },
body: { type: "textarea" },
},
});
See Fields for information on field types and options.
Create the CMS instance
Create tenon.config.ts at the project root. It reads the secret and the database URL from environment variables. Each framework reads them in its own way:
import { DATABASE_URL, TENON_SECRET, TENON_SITE_URL } from "astro:env/server";
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";
export default defineCms({
collections: { posts },
database: libsql(DATABASE_URL),
secret: TENON_SECRET,
site: { url: TENON_SITE_URL, name: "My Site" },
});
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";
const config = useRuntimeConfig();
export default defineCms({
collections: { posts },
database: libsql(config.databaseUrl),
secret: config.tenonSecret,
site: { url: config.tenonSiteUrl || undefined, name: "My Site" },
});
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";
export default defineCms({
collections: { posts },
database: libsql(process.env.DATABASE_URL ?? "file:./cms.db"),
secret: process.env.TENON_SECRET!,
site: { url: process.env.TENON_SITE_URL, name: "My Site" },
});
For what each option does, see Configuration.
Connect the framework
The framework setup adds the API at /_tenon, the admin at /admin, and gives your pages the CMS instance:
Add the integration, and declare the variables in the env schema:
import node from "@astrojs/node";
import { defineConfig, envField } from "astro/config";
import tenonCms from "tenoncms/astro";
export default defineConfig({
adapter: node({ mode: "standalone" }),
integrations: [tenonCms()],
env: {
schema: {
TENON_SECRET: envField.string({ context: "server", access: "secret", min: 32 }),
DATABASE_URL: envField.string({
context: "server",
access: "secret",
default: "file:./cms.db",
}),
TENON_SITE_URL: envField.string({
context: "server",
access: "secret",
url: true,
optional: true,
}),
},
},
});
The integration finds tenon.config.ts at the project root.
Add the module, and declare the values in runtimeConfig:
export default defineNuxtConfig({
modules: ["tenoncms/nuxt"],
runtimeConfig: {
tenonSecret: "",
databaseUrl: "file:./cms.db",
tenonSiteUrl: "",
},
});
The module finds tenon.config.ts at the project root.
Add tenonAdmin() to the Vite config. It builds the admin. Then add route files that send requests to the CMS and serve the admin page. The sitemap and robots.txt routes are optional.
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import { nitro } from "nitro/vite";
import { tenonAdmin } from "tenoncms/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [tanstackStart(), nitro(), viteReact(), tenonAdmin()],
});
The brackets in the file names escape characters that TanStack Router treats as syntax. A plain _tenon file would be a layout route.
Set the secret
Tenon signs sessions with a secret of at least 32 characters. Generate one:
openssl rand -hex 32
Put it in .env at the project root:
TENON_SECRET=<your secret>
NUXT_TENON_SECRET=<your secret>
TENON_SECRET=<your secret>
Keep the secret, the database and the uploads out of git:
.env
cms.db*
.uploads
Create the admin account
Start the dev server, open the URL it prints and add /admin. Enter your name, email and a password, then select Create admin account.
The first account is the admin. Create a post, then select Publish. Every save is a draft until you publish it.
Show the posts on a page
Read the posts on a page. The page runs on the server and calls Tenon directly, without HTTP:
---
export const prerender = false;
const posts = await Astro.locals.tenon.find("posts");
---
<h1>Posts</h1>
<ul>
{posts.map((post) => <li>{post.title}</li>)}
</ul>
<script setup lang="ts">
const { data: posts } = await useTenon().find("posts");
</script>
<template>
<main>
<h1>Posts</h1>
<ul>
<li v-for="post in posts" :key="post.id">{{ post.title }}</li>
</ul>
</main>
</template>
import { createFileRoute } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import cms from "../../tenon.config";
const getPosts = createServerFn().handler(() => cms.find("posts"));
export const Route = createFileRoute("/blog")({
loader: () => getPosts(),
component: Blog,
});
function Blog() {
const posts = Route.useLoaderData();
return (
<main>
<h1>Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</main>
);
}
Open /blog. It lists the post you published. post.title is typed from the collection, so your editor completes it and flags a typo.
In Astro, prerender = false makes the page read the content on each request. A prerendered page reads it once, at build time.