Skip to content

Local API reference ​

Every method of the cms object you get from useEasyCMS() (Nuxt), getEasyCMS(config) (Next.js) or createEasyCMS(config). A test checks this list against the EasyCMS class. For examples, see Local API.

The Local API trusts its caller and skips access rules. Pass { user, overrideAccess: false } to apply them, as the REST API does.

Options ​

OptionOn
overrideAccessevery methodfalse applies access rules for user. Default true.
userevery methodThe user to check access for; null = not logged in.
depthreads and writesLevels of relationships to populate. Default 1, at most 3.
localereads and writesA content locale, or 'all' for { [locale]: value }. Default: the default locale.
draftreadsInclude drafts. Default: published documents only.
fallbackLocalereadsUse the default locale's value when a value is empty. Default localization.fallback.
wherefind, countFilter: { field: { equals, not_equals, in, not_in, gt, gte, lt, lte, like, exists } }, with and / or.
sortfindField path; - for descending. Default -createdAt.
limitfindDefault 10; 0 returns every match.
pagefind1-based. Default 1.

find returns { docs, totalDocs, limit, page, totalPages, hasNextPage, hasPrevPage }.

Methods ​

Documents ​

MethodReturns
find(collection, options?)a page of documentsList with where, sort, limit, page.
findById(collection, id, options?)document | nullOne document.
count(collection, options?)numberHow many match where.
create(collection, data, options?)documentValidates, runs hooks, saves.
update(collection, id, data, options?)documentChanges the given fields.
delete(collection, id, options?)the deleted documentAlso deletes its versions and scheduled jobs.

Media ​

MethodReturns
upload({ data, name }, fields?, options?)media documentStores a file; images get dimensions and resized copies.
mediaURL(key)stringPublic URL of a stored file.

Drafts and versions ​

MethodReturns
unpublish(collection, id, options?)documentTakes a document off the site.
discardDraft(collection, id, options?)documentThrows away the pending draft of a published document.
findVersions(collection, id, options?)a page of versionsNewest first. Needs update access.
findVersion(collection, id, versionId, options?)version | nullOne version with its data.
restoreVersion(collection, id, versionId, options?)documentMakes an old version current (as a draft with drafts).

Globals ​

MethodReturns
findGlobal(slug, options?)globalRead a global.
updateGlobal(slug, data, options?)globalChange a global.
unpublishGlobal(slug, options?)globalTake it off the site.
discardGlobalDraft(slug, options?)globalThrow away its pending draft.
findGlobalVersions(slug, options?)a page of versionsNewest first.
findGlobalVersion(slug, versionId, options?)version | nullOne version.
restoreGlobalVersion(slug, versionId, options?)globalMake an old version current.

Live preview ​

MethodReturns
preview(collection, id | null, data, options?){ doc, url }The document as if data were saved; nothing is written.
previewGlobal(slug, data, options?){ doc, url }The same for a global.
createPreviewToken(target, { expiresIn? })stringA token that opens one draft without a login (default one hour).
verifyPreviewToken(token)target | nullWhat a token opens, or null when invalid or expired.

Scheduling ​

MethodReturns
schedule(collection, id, { action, at }, options?)jobPublish or unpublish at at.
scheduled(collection, id, options?)jobsPending jobs of a document, soonest first.
cancelSchedule(collection, id, jobId, options?)—Cancel a pending job.
scheduleGlobal(slug, { action, at }, options?)jobThe same for a global.
scheduledGlobal(slug, options?)jobs
cancelGlobalSchedule(slug, jobId, options?)—
upcomingJobs({ limit?, user?, overrideAccess? })jobsThe next pending jobs across everything.

Jobs and webhooks ​

MethodReturns
runJobs(now?){ ran, failed, webhooks }Due scheduled jobs and webhook retries (what cron and the timer call).
runScheduled(now?){ ran, failed }Only the due scheduled jobs.
retryWebhooks(now?){ sent, failed }Only the due webhook retries.
startScheduler(interval?)—Runs runJobs every interval ms (default one minute). createEasyCMS starts it when something is scheduled or webhooks are set.
flushWebhooks()—Waits for deliveries in progress; call before a serverless function returns.
sendEmail(message)—Queues an email for the config's email adapter and sends it in the background. Email
flushEmails()—Waits for emails being sent; call before a serverless function returns.

Other ​

MethodReturns
createApiKey({ name, permissions?, expiresAt?, user? }, options?){ key, doc }A new API key (with apiKeys: true); the key is returned only here.
documentPermissions(collection, id, user){ update, delete }What a user may do with one document.
destroy()—Stops the scheduler, waits for webhook deliveries and closes the database.

The cms object also has config (the resolved config), auth (login and session checks), db, storage, logger and cwd.

Released under the MIT License.