Obelism Content JS SDK
npm i @obelism/content
ContentClient
Reads one space environment through the delivery API. The token decides the
space, the environment and whether drafts are visible: a delivery token
(cms_d_…) sees published content, a preview token (cms_p_…) drafts too.
Keep preview tokens server-side.
import { ContentClient } from '@obelism/content'
const content = new ContentClient({
token: process.env.CONTENT_DELIVERY_TOKEN,
// url: 'https://content.obelism.studio',
// locale: 'nl', depth: 1, richText: 'json', timeout: 10_000,
// init: { next: { revalidate: 60 } }, // passed to every fetch
})
await content.getPage('blog/hello') // Entry | null
await content.getPage('blog/hallo', { locale: 'nl' }) // looked up in nl's paths
await content.findPage('blog/old') // { entry } | { redirect: { path } } | null
await content.getRedirects({ locale: 'nl' }) // { items: [{ locale, from, to }], total, limit, skip }
for await (const redirect of content.getAllRedirects()) {
}
await content.getEntry('entry_…') // Entry | null
await content.getEntries({ contentType: 'post', limit: 20, skip: 0 }) // { items, total, limit, skip }
for await (const post of content.getAllEntries({ contentType: 'post' })) {
}
await content.getFirstEntry({
contentType: 'navigation',
fields: { key: 'main' },
}) // Entry | null
await content.getEntries({
contentType: 'post',
fields: { category: 'news', 'date[gte]': '2026-01-01', tags: ['a', 'b'] }, // a list: one of them
order: '-fields.date',
})
await content.getContentModel() // the model file
Each method takes { locale, depth, richText, drafts, release, init } to
override the defaults.
release previews a release (release_…): every entry as publishing it now
would leave it, with the release's own unpublished pages and without the ones
it takes down. It is how a site checks a launch before it goes live, so the
token must read drafts (a preview token, a cms_token, a site or plugin
token, or a host token with drafts): a delivery token that names a release
is refused, 403 release_needs_preview. It holds for every read the client
makes, getRedirects and getContentModel too.
Without a stored token
On Vercel, or in GitHub Actions, the client can send the identity token its
host gives it instead of a delivery or preview token. Connect the deployment
first, under Connected deployments on the space's page on
www.obelism.studio: each of its environments (Vercel's production, preview,
development) maps to one environment of the space, with published content,
drafts too, or management. Then pass a token source:
import { ContentClient } from '@obelism/content'
import { vercelOidc } from '@obelism/content/oidc' // needs @vercel/oidc installed
const content = new ContentClient({
token: vercelOidc(), // or githubOidc() in a workflow
space: process.env.CONTENT_SPACE_ID, // picks one when the deployment is connected to several
environment: process.env.CONTENT_ENVIRONMENT, // refused if the rule maps it elsewhere
})
// A preview route: drafts, when the connection's rule has them
const draft = await content.getPage(path, { drafts: true })
A host token reads published content unless the client or the request sets
drafts: true, so a site's pages and its preview can share one deployment.
token takes any function that gives a token per request. Requests with a
host token skip the edge cache, since the token changes every hour or two.
Locally, vercelOidc() works after vercel link and vercel login: it takes
the token vercel env pull wrote, or asks Vercel for a new one with your
sign-in. That token's environment is development, so the connection needs a
rule for it, and environment names the space environment that rule maps to.
Anyone outside the Vercel team uses a delivery token.
Pages and folders can have a slug per locale. getPage looks the path up
in the requested locale's paths, entry.path is the path in that locale and
entry.paths the path in every locale ({ en: 'about-us', nl: 'over-ons' }),
for hreflang links and language switchers. The locale is never part of a
path: whether it is /nl/over-ons or nl.example.com/over-ons is your
routing. A page that moved keeps its old path as a redirect in each locale:
findPage answers it with { redirect: { path } }, so your route can send
a 301 instead of a 404, and getAllRedirects lists them for redirect rules
built ahead of time. Missing pages and entries are null; any other failure throws a
ContentApiError with status and the API's message. Type your content
with generics: content.getPage<Entry<{ title: string }>>('home').
Preview
The visual editor loads your site with
?cms_preview=1&cms_entry=…&cms_environment=…&cms_token=…. cms_token is
made for the person editing and reads drafts for 8 hours; read the page with
it, server-side, naming your space and environment: anyone with a space of
their own can make a cms_token for it, and only a request that names yours
refuses one for another. Never show drafts because cms_preview is there:
anyone can add it to a URL.
A release's Preview button opens the same parameters on the release's
first page plus cms_release=release_…, with a cms_token made for that
release: the site previews that release alone. readPreviewParams answers it
as release; pass it to the client to ask for the release with a preview
token of your own, or to tell which release is being shown.
import {
ContentApiError,
ContentClient,
readPreviewParams,
} from '@obelism/content'
const preview = readPreviewParams(request.url)
const content = preview?.token
? new ContentClient({
token: preview.token,
space: process.env.CONTENT_SPACE_ID,
environment: process.env.CONTENT_ENVIRONMENT,
release: preview.release ?? undefined,
init: { cache: 'no-store' },
})
: deliveryClient
const page = await content.getPage(path).catch((error) => {
// A made-up, expired or other space's token: show what's published.
if (error instanceof ContentApiError && error.status === 401) {
return deliveryClient.getPage(path)
}
throw error
})
Visual editor bridge
import { getContentBridge } from '@obelism/content/bridge'
const bridge = getContentBridge()
bridge.on('update', ({ entry }) => render(entry)) // unsaved edits, as typed
Mark blocks so editors can click them: <section {...editable(block)}>
(adds data-cms-uid). A listener added after an update, like a component
streamed in after hydration, is called right away with each entry's latest
one. Without an update listener the page reloads after each save. React
sites use @obelism/content-react.
Without a bundler, the same bridge (prompt box included) is one script tag:
<script src="https://content.obelism.studio/bridge.js"></script>
<script>
ObelismBridge.on('update', ({ entry }) => render(entry))
</script>
The CMS serves this package's dist/bridge.global.js there; npm CDNs serve
it too (then add data-url if your CMS isn't content.obelism.studio).
data-prompt="false" leaves the prompt box out. The tag puts the bridge on
window.ObelismBridge and getContentBridge / mountPromptBox on
window.ObelismContent; a bundled copy on the same page shares its bridge.
Prompt editing
The prompt box is plain DOM in a shadow root, no framework. Start the
bridge with prompt: false to leave it out, and mount it yourself later:
import { getContentBridge, mountPromptBox } from '@obelism/content/bridge'
const bridge = getContentBridge({ prompt: false })
const unmount = mountPromptBox(document, bridge, { labels: { open: 'Ask' } })
actions puts controls of your own in the box: an element of your page,
shown next to the closed box and in the open box's footer (by a <slot>, so
your CSS styles it). signOut: false leaves Sign out out. A bridge with no
page (entryId null) normally offers only New page; promptWithoutPage: true keeps the prompt there too, for a bridge whose prompt acts on
something else (Content's canvas: the whole site), with New page beside it.
Content's canvas uses all three, with Save all in actions.
const controls = document.createElement('div')
mountPromptBox(document, bridge, {
actions: controls,
signOut: false,
promptWithoutPage: true,
})
Inside the visual editor, people who may edit get a prompt box at the bottom
of the page: they type what to change and it shows on the page right away,
unsaved, with undo. It acts on the selected block, or the whole entry. The
bridge adds it by default; it needs nothing from your site.
getContentBridge({ prompt: false }) // leave it out
getContentBridge({ prompt: { labels: { open: 'Bewerk met een prompt' } } })
// Your own UI instead:
const bridge = getContentBridge({ prompt: false })
bridge.on('change', ({ features, prompting, canUndo, selectedUid }) => …)
const { summary } = await bridge.prompt('Make the heading shorter')
bridge.undo()
On a site signed in to edit (@obelism/content/site), the box also has New
page: a path (/pricing, or under an existing page) and what the page is
for. The CMS writes the page like the one the editor is on and stores it as a
draft, and the bridge opens it at / + its path. The path is the one in
the page's language (<html lang>). When your routes put CMS pages
elsewhere, say where:
getContentBridge({ pagePath: (path, locale) => `/${locale}/${path}` })
await bridge.createPage('/pricing', 'Our plans, for agencies') // or your own UI
On a product route, mark the Shopify product the page shows. When the
product has no product page yet (nothing on the page is editable(entry)),
the box offers Add a product page: the CMS makes a draft of the space's
product page type for it (its handle as slug, in the products folder)
and the page reloads, now finding it. The page list opens product pages at the environment's product path (Settings →
Preview URL in the CMS, /products/{handle} by default), or at your own
productPath:
import { editable, editableProduct } from '@obelism/content'
// app/products/[handle]/page.tsx: the store's product, and its page if any.
;<main {...editableProduct(product)} {...editable(productPage)}>
…
</main>
getContentBridge({ productPath: (handle, locale) => `/${locale}/p/${handle}` })
await bridge.createProductPage() // or your own UI; bridge.product says which
A collection route works the same with editableCollection(collection):
Add a collection page makes a draft of the space's collection page type
(in the collections folder), and the page list opens collection pages at
the environment's collection path (/collections/{handle} by default), or
at your own collectionPath:
import { editable, editableCollection } from '@obelism/content'
;<main {...editableCollection(collection)} {...editable(collectionPage)}>
…
</main>
await bridge.createCollectionPage() // bridge.collection says which
On the site the prompt box is the on-site editor instead
(mountSiteEditor, mounted by getContentBridge with the same options): a
panel docked on the right, the site reflowed beside it (a sheet over the page
on small screens). It opens on Edit fields, the CMS's own form for the
page — or for the entry around the selected block — right in the panel: the
fields, and blocks as cards to open and add, as in the dashboard. Edits apply
as you make them: they land in the unsaved changes, show on the page, and
undo a burst of typing at a time. The small Ask button in the header
switches to prompts for the selected block — or the whole page — and shows
the answer with what it changed, before and after; the change shows on the
page straight away, outlined as a preview, and needs Keep it before it
can be saved (Try another and Undo take it back). Hovered blocks
show their name; Escape — or the link above the form, Up to the block
around it and Whole page at the top — goes up a level at a time, back to
the whole page. The header
names the page and opens every page of the space, drafts included, to go to
one (at pagePath, in the page's language when it has a path there), and
shows the environment. A launcher at the bottom left minimizes the panel,
hides the outlines and exits. Its words are labels
(SiteEditorLabels, defaults in
DEFAULT_SITE_EDITOR_LABELS).
The site makes room for the panel with a margin on <html>, which moves
everything in the page's flow. Elements with position: fixed (or sticky)
stay on the viewport, so a fixed header or cart button would sit under the
panel. Give them the room yourself with the --obelism-dock custom property
the editor sets on <html> (436px while docked, 0px otherwise), or add
data-obelism-dock to the element and the editor sets its right and
max-width while docked:
.site-header {
position: fixed;
right: var(--obelism-dock, 0px);
}
<button class="cart" data-obelism-dock>Cart</button>
The form, the pages and saving work as below:
const form = bridge.openForm(element, { onReady, onError }) // an iframe
form.close()
const pages = await bridge.pages() // { entryId, name, status, path, url }[]
bridge.openPage(pages[0]) // asks first when changes aren't saved
The form needs createSiteEditing's /cms/api/form, which gets the CMS
page a one-time address. For the raw content instead, as a language model
sees it (one locale, rich text as Markdown, blocks with _uid and
_type), there is JSON:
const view = await bridge.view() // or { uid } for a block
const { changed } = await bridge.applyJson({ ...view, title: 'Pricing' })
The CMS runs prompts on Workers AI. To use your own model, set a prompt
endpoint under Space settings → Prompt editing and answer it with
createPromptHandler (it verifies the signed request; you call the model):
// app/api/content-prompt/route.ts
import { createPromptHandler } from '@obelism/content/prompt'
export const POST = createPromptHandler({
secret: process.env.CONTENT_PROMPT_SECRET, // whsec_… from Space settings
generate: async ({ system, messages, schema }) => {
// call any model; return its JSON answer (object or text)
},
})
Webhooks
A webhook tells your site that what the delivery API answers has changed
(Space settings → Webhooks). Its requests are signed (Standard Webhooks);
createWebhookHandler verifies them and hands you the typed event:
// app/api/revalidate/route.ts
import { revalidatePath, revalidateTag } from 'next/cache'
import { createWebhookHandler } from '@obelism/content/webhooks'
export const POST = createWebhookHandler({
secret: process.env.CONTENT_WEBHOOK_SECRET!, // whsec_…, shown once
onEvent: (event, { id }) => {
if (event.type === 'entry.published' && event.data.entry.path) {
revalidatePath(`/${event.data.entry.path}`)
} else {
revalidateTag('content')
}
},
})
It answers 401 to anything not signed with the secret or with a
webhook-timestamp more than 5 minutes off (tolerance, in seconds), 204
once onEvent is done, and 500 when it throws, so the CMS tries again. id
is the delivery's webhook-id, the same on every retry.
Elsewhere, verifyWebhook(request, secret, { tolerance? }) returns the event
or throws WebhookVerificationError. It takes a Request, or the raw body
and headers ({ body, headers }; Node's req.headers work as they are), the
body exactly as it was sent:
import { verifyWebhook } from '@obelism/content/webhooks'
const event = await verifyWebhook(request, process.env.CONTENT_WEBHOOK_SECRET!)