Reference › Local API
Docs/Reference/Local API

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.
contextevery methodWhat the call is about besides its user, e.g. its tenant: given to access rules, hooks and filterOptions. From forRequest, or your own.
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. With where, only while the stored document matches it (compare and set): null when it doesn't, so one of two concurrent calls wins.
increment(collection, id, field, by, { min?, max? })number | nullAdds by to a top-level number field in one statement, safe with concurrent calls, e.g. stock. null when the result would pass min / max. Skips hooks and access.
delete(collection, id, options?)the deleted documentAlso deletes its versions and scheduled jobs. Deleting a user with auth.rbac: { transferTo: id } gives the documents they own to another user; otherwise they have no owner.

Media

MethodReturns
upload({ data, name }, fields?, options?)media documentStores a file; images get dimensions and resized copies.
uploadFromURL(url, fields?, options?)media documentDownloads a file from a link, then as upload. From a link
mediaURL(key)stringURL of a stored file: public, or the API's for private files.
signedMediaURL(doc, { expiresIn?, size? })stringA link to a private file that works without signing in until it expires (default '1h', at most '7d'). Private folders
verifyMediaSignature(key, expires, signature)booleanWhether a signed link is genuine and not expired.
storageFor(isPrivate)StorageAdapterThe storage for public or private files (upload.privateStorage).
createUpload({ name, size, type? }, fields?, options?){ ticket, upload }Starts an upload straight to the storage. Large files
completeUpload(ticket, options?)media documentChecks a file sent straight to the storage and makes its document.
mediaUsage(ids)numberHow many documents use these files in a top-level upload field.

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, emails, jobs }Due scheduled jobs, webhook and email retries, and the config's jobs (what cron and the timer call).
emit(event, data, about?)—Sends an event of the config's events (e.g. order.paid) to the webhooks that list it; about: { collection, id }. Webhooks
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.
uniqueScope(collection, field, { user?, context? }){ [field]: value }What a unique field's values differ within for a call (uniqueWithin, e.g. the tenant), as its hooks would set it; {} when unique everywhere. For finding documents by such a field.
forRequest(request | headers){ user, context }A request's user (session cookie or Bearer token) and context (onRequest), as REST works them out; pass both with overrideAccess: false.

The cms object also has config (the resolved config), auth (login and session checks), auth.sso (signing in with auth.providers), audit (with audit: audit.record({ action, target, doc }) writes an entry), roles (with auth.rbac: roles.allows(user, { collection }, operation) says what a user's role allows), db, storage, logger and cwd.