⚠️ Work in progress — createCMS is pre-1.0 and not production-ready (not tested in production). Expect breaking changes.
createCMS
Reference

Variables

Reusable values substituted into content, via cms.api.variables.

Variables are reusable values referenced in content as {{key}} and substituted on read (in getBlockTree and getPublishedContent, unless raw: true). Methods live under cms.api.variables and are mirrored on the client as client.variables.<method> with identical types.

A key is alphanumeric plus underscores. Updating a variable's value revalidates the published roots that use it. Deleting one that is in use throws VARIABLE_IN_USE.

Reference a variable in any string property, for example { title: 'Hello {{siteName}}' }. An unknown key is left literal.

Client-side substitution

The {{key}} helpers the server uses on read are exported as pure functions, so an editor can preview substitution from a variables map it already loaded (for example from variables.list) without another request. Import them from @createcms/core on the server and from @createcms/core/react in the browser.

import { extractVariableKeys, resolveTemplateString } from '@createcms/core/react';

const { variables } = await client.variables.list();
const vars = new Map(variables.map((v) => [v.key, v.value]));
resolveTemplateString('Hello {{siteName}}', vars); // 'Hello Acme'
extractVariableKeys('{{a}} and {{b}}'); // ['a', 'b']

VAR_PATTERN (the {{key}} regex, global) is exported as well; reset its lastIndex before a manual exec or test, or use extractVariableKeys.

Methods

List Variables

List every variable, ordered by key, so you can browse or search what's defined. All query fields are optional — call it with no arguments to page through everything from the top.

variable:read
GET/variables/list
const { data, error } = await client.variables.list({
  query: { search: 'site', limit: 20 },
});
Parameters
limitnumber= 50

Page size, 1 to 100.

offsetnumber= 0

Rows to skip.

searchstring

Case-insensitive substring match against the variable key.

Returns
variablesVariable[]

The variables on this page, ordered by key. Each row is `{ id, key, value, description, createdBy, updatedBy, createdAt, updatedAt }`.

totalnumber

Total variables matching the search, ignoring limit/offset.

hasMoreboolean

Whether more variables exist after this page.

Get a Variable

Fetch a single variable by its key, and find out in the same call whether it's currently referenced anywhere. Throws VARIABLE_NOT_FOUND if no variable has that key.

variable:read
GET/variables/getVariable
const { data, error } = await client.variables.getVariable({
  query: { key: 'siteName' }, // required
});
Parameters
keystringrequired

The variable key to retrieve.

Returns
variableVariable

The variable row: `{ id, key, value, description, createdBy, updatedBy, createdAt, updatedAt }`.

inUseboolean

Whether the variable is currently referenced by live content or a template.

Create a Variable

Create a new variable with a unique key. You get back the created variable, including its generated id and timestamps. A duplicate key throws VARIABLE_KEY_EXISTS.

variable:create
POST/variables/createVariable
const { data, error } = await client.variables.createVariable({
  body: {
    key: 'siteName', // required
    value: 'Acme', // required
    description: 'Public site name',
  },
});
Parameters
keystringrequired

Unique key, 1 to 100 characters, alphanumeric and underscores only.

valuestringrequired

The variable value.

descriptionstring

Optional description of the variable's purpose.

Returns
variableVariable

The newly created variable, with its generated id, timestamps, and creator metadata.

Update a Variable

Update a variable's value and/or description. Changing the value revalidates every published root that references it, so the new value goes live automatically.

variable:update
POST/variables/updateVariable
const { data, error } = await client.variables.updateVariable({
  body: {
    key: 'siteName', // required
    value: 'Acme Corp',
  },
});
Parameters
keystringrequired

The variable key to update.

valuestring

New value. Changing it triggers revalidation of published content.

descriptionstring

New description.

Returns
variableVariable

The updated variable.

Delete a Variable

Delete a variable by key. It fails with VARIABLE_IN_USE if the variable is still referenced in any live content or template, so you can't leave a dangling {{key}} behind.

variable:delete
POST/variables/deleteVariable
const { data, error } = await client.variables.deleteVariable({
  body: { key: 'siteName' }, // required
});
Parameters
keystringrequired

The variable key to delete.

Returns
variableIdstring

The id of the deleted variable.

List Variable Usages

Find every place a variable is referenced before you change or delete it: the distinct live block usages in branch heads and the template usages, each with a total count.

variable:read
GET/variables/getVariableUsages
const { data, error } = await client.variables.getVariableUsages({
  query: { key: 'siteName' }, // required
});
Parameters
keystringrequired

The variable key to inspect.

Returns
blockUsageCountnumber

Number of distinct live block usages.

templateUsageCountnumber

Number of template usages.

blockUsages{ rootId, blockId, propertyKey }[]

Each distinct live block property (in a branch head) that references the variable.

templateUsages{ templateId, collection, blockType, propertyKey }[]

Each template property that references the variable.

On this page