Client
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?)
fetch to use. Default: the global fetch.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:
statusis the HTTP status.bodyis the parsed answer. For invalid data,body.errorsholds one message per field path, likeseo.title.
A 204 answer resolves to null.
Differences to the local API
The shared methods take the same options, with these differences:
| Method | Client | Local API |
|---|---|---|
findById, update, publish, unpublish, duplicate, copyLocale, removeLocale, schedule | Throws a CmsError with 404 for a missing document | Returns undefined |
delete, discard | Resolves to null | undefined |
create, update, setGlobal | No draft option. Use createDraft(), updateDraft() or setGlobalDraft() | { draft: true } |
find | limit: 0 returns every document | limit: 0 throws |
find | A field named like a read option, such as limit or locale, cannot be filtered | Any field |
populate | At most populate.maxDepth hops | No limit |
export | A Blob in memory | A stream |
import | No dryRun. Use checkImport(). Takes onProgress and signal | { dryRun: true } |
media.upload | Takes a Blob and { folder, filename, onProgress, signal } | Bytes or a stream |
jobs.run(id) | Runs one job now and waits for it | jobs.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
| Method | Route |
|---|---|
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
| Method | What 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
| Method | What 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
| Method | What 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
| Method | What 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.
Related resources
- HTTP routes: the routes behind each method.
- API keys: a client for another service.