API

Client

The options of createClient(), where its methods differ from the local API, and the methods only the client has.

The client from tenoncms/client calls the HTTP routes with the names of the local API. Use it in the browser, or in another service. For a guide, see HTTP API and client.

import { createClient } from "tenoncms/client";

const client = createClient({ baseUrl: "https://example.com" });
const posts = await client.find("posts", { limit: 3 });

Create a client

createClient(options?)

The origin of the site that runs Tenon. Default: the current origin.The fetch to use. Default: the global fetch.An API key, sent as x-api-key with every request. Server only.

createRequestClient(request, options)

A client for one request of a server page on another origin. It forwards the cookie of request, but only to baseUrl. baseUrl is required here. See Headless.

Errors

A status outside 2xx, or an answer without JSON, throws a CmsError from tenoncms/client:

  • status is the HTTP status.
  • body is the parsed answer. For invalid data, body.errors holds one message per field path, like seo.title.

A 204 answer resolves to null.

Differences to the local API

The shared methods take the same options, with these differences:

MethodClientLocal API
findById, update, publish, unpublish, duplicate, copyLocale, removeLocale, scheduleThrows a CmsError with 404 for a missing documentReturns undefined
delete, discardResolves to nullundefined
create, update, setGlobalNo draft option. Use createDraft(), updateDraft() or setGlobalDraft(){ draft: true }
findlimit: 0 returns every documentlimit: 0 throws
findA field named like a read option, such as limit or locale, cannot be filteredAny field
populateAt most populate.maxDepth hopsNo limit
exportA Blob in memoryA stream
importNo dryRun. Use checkImport(). Takes onProgress and signal{ dryRun: true }
media.uploadTakes a Blob and { folder, filename, onProgress, signal }Bytes or a stream
jobs.run(id)Runs one job now and waits for itjobs.run() is one tick of the clock

A write needs a session or an API key, unless an access rule allows it. See Access control.

Methods only on the client

Drafts

MethodRoute
createDraft(collection, data, options?)POST /collections/:name/draft
updateDraft(collection, id, data, options?)PUT /collections/:name/:id/draft
setGlobalDraft(name, data, options?)PUT /globals/:name/draft

Edit locks and versions

MethodWhat it does
lock(collection, id, { takeover? }), unlock(collection, id)Takes or releases the edit lock of a document. API keys cannot lock
lockGlobal(name, { takeover? }), unlockGlobal(name)The same for a global
versions(collection, id, options?), version(collection, id, version)Lists the versions of a document, or reads one
restoreVersion(collection, id, version)Makes an old version the draft
globalVersions(name), globalVersion(name, version), restoreGlobalVersion(name, version)The same for a global

See Edit conflicts and Version history.

Preview and schema

MethodWhat it does
preview.enter(perspective?), preview.exit()Sets or clears the preview cookie. Default: "drafts"
previewUrl(collection, data, options?), previewGlobalUrl(name, data, options?)The preview URL of unsaved data, or null
getSchema()The schema that the signed-in user may see
localization()The locales and the default locale. Public

See Live preview.

Sign-in and accounts

MethodWhat it does
signIn({ email, password }), signOut()Signs in with a password, or out
signInMagicLink(...), sendSignInCode(...), signInWithCode(...)Sign-in without a password, with magicLink() or emailOTP()
requestPasswordReset(...), resetPassword(...)The Forgot password? flow
getSession()The session, or null
signUp(data)Creates the first user. Throws 403 once an admin exists
setup(data)Creates the first user when password sign-in is off
invites.list(), create(), resend(), revoke(), check(), accept()Invites. check() and accept() are public
apiKeys.list(), create(), roll(), revoke(), delete()API keys. Admins only
device.code(), token(), verify(), approve(), deny()The device sign-in of tenon login

See Authentication.

Jobs, media and backups

MethodWhat it does
jobs.list(filter?), jobs.cron()The jobs and the cron entries the user may see
jobs.retry(id), jobs.cancel(id)Queues a failed job again, or deletes a job
media.folders()Every folder of the media library
exportLink(options?)A download link for a backup. With S3, it works for 12 hours
checkImport(manifest, options?)Checks an archive before an import

See Manage jobs and Backup and restore.

Any route

request(path, { method?, body?, query? }) calls any route under /_tenon, like a plugin route. The route MUST answer JSON or 204.

const stats = await client.request<{ count: number }>("/stats/count");

Methods only in the local API

alternatesAt(), withDrafts(), withSession(), jobs.queue(), email.send(), users.*, sitemap(), fetch() and the instance properties. There is no route for them. The client reads drafts through the preview cookie instead.