Local API
Import the generated API and call it directly without HTTP overhead.
import { cms } from "./cms/.generated/api";const posts = await cms.posts.find({ where: { category: "tech" }, sort: { field: "_updatedAt", direction: "desc" }, limit: 10, offset: 0, status: "published", locale: "fi",});| Option | Type | Default | Description |
|---|---|---|---|
where | Record<string, unknown> | — | Filter by field values |
sort | { field, direction } | — | Sort by field, "asc" or "desc" |
limit | number | — | Max documents to return |
offset | number | — | Skip N documents |
status | "draft" | "published" | "scheduled" | "any" | "published" (drafts collections) / "any" (others) | Filter by status |
locale | string | default locale | Language code for translations |
Find One
Section titled “Find One”const post = await cms.posts.findOne({ slug: "hello-world", locale: "fi", status: "any",});| Option | Type | Description |
|---|---|---|
| field filters | varies | Filter by any field (e.g. slug, email) |
locale | string | Language code |
status | "draft" | "published" | "scheduled" | "any" | Status filter |
Find by ID
Section titled “Find by ID”const post = await cms.posts.findById("abc123", { locale: "fi", status: "any",});| Option | Type | Description |
|---|---|---|
locale | string | Language code |
status | "draft" | "published" | "scheduled" | "any" | Status filter |
Create
Section titled “Create”const post = await cms.posts.create({ title: "New Post", body: { type: "root", children: [...] }, _status: "published", // optional, defaults to "draft" if drafts enabled});Pass field values as properties. Returns the created document.
Update
Section titled “Update”const post = await cms.posts.update("abc123", { title: "Updated Title",});Only include fields you want to change. Returns the updated document.
Delete
Section titled “Delete”await cms.posts.delete("abc123");Cascades: removes translations, versions, and the document.
Publish / Unpublish
Section titled “Publish / Unpublish”await cms.posts.publish("abc123");await cms.posts.unpublish("abc123");Schedule
Section titled “Schedule”await cms.posts.schedule( "abc123", "2025-06-01T00:00:00Z", // publishAt (required) "2025-07-01T00:00:00Z", // unpublishAt (optional));Sets status to "scheduled". Requires a cron job or the built-in dev scheduler to process.
const total = await cms.posts.count({ status: "published" });Accepts same filter options as find (without limit, offset, sort).
Versions
Section titled “Versions”const versions = await cms.posts.versions("abc123");// → [{ version: 5, createdAt: "...", snapshot: {...} }, ...]
await cms.posts.restore("abc123", 5);Translations
Section titled “Translations”const translations = await cms.posts.getTranslations("abc123");// → { fi: { title: "...", body: {...} } }
await cms.posts.upsertTranslation("abc123", "fi", { title: "Hei maailma", body: { type: "root", children: [...] },});upsertTranslation inserts or updates. Only include translatable fields.
Introspection
Section titled “Introspection”cms.meta.getCollections(); // All collections with metadatacms.meta.getFields("posts"); // Field definitions for a collectioncms.meta.getCollection("posts"); // Full collection configcms.meta.getRouteForDocument("posts", doc); // Public URL for a documentcms.meta.getLocales(); // { default, supported }cms.meta.isTranslatableField("posts", "title"); // true/falseUsage in Astro Pages
Section titled “Usage in Astro Pages”---import { cms } from "@/cms/.generated/api";
const post = await cms.posts.findOne({ slug: Astro.params.slug });if (!post) return Astro.redirect("/404");
Astro.cache.set({ tags: ["posts", `post:${post._id}`] });---
<h1>{post.title}</h1>