The Visual Editor API is designed for experienced CloudCannon developers. If you need help with custom integrations or your CloudCannon setup in general, please contact our friendly support team.
CloudCannon's Visual Editor API CloudCannon's Visual Editor API is a public JavaScript API for building custom integrations with the Visual Editor. It lets you run your own code inside the Visual Editor to read and write content, listen for changes, and upload files using code running inside your website files. A group of related files with a similar format (e.g., a folder of pages, blog posts, or data files). Once you group your files into Collections, they appear in the Site Navigation for easy access. A structured data file or folder defined under A website in CloudCannon that includes all the files, content, configuration, and settings needed to edit, build, and host a complete website.Visual Editor API
Collection
Dataset
data_config in your CloudCannon Configuration File. Datasets are used to store reusable data such as navigation links, locale strings, or site settings, and can be accessed and updated through CloudCannon's editing interfaces.Site
This article assumes you know how to initialize the Visual Editor API. For a complete list of API objects and methods, please read our reference documentation on the Visual Editor API.
Files#
Calling An API object is an object that exposes an API's methods, so you can work with a service or system in code. In CloudCannon, the Visual Editor API provides an API Object as its entry point: call *.files() on the API ObjectAPI Object
useVersion() on window.CloudCannonAPI to get it, then use its methods to access files, Collections, and Datasets, read and write content, listen for changes, and build custom integrations in the Visual Editor.*.currentFile(), which returns the file currently open in the Visual Editor, or *.file(path), which returns a specific file by its source path regardless of what is currently open.
To list all files:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
await *.files()for an array of every file in your Site.
const api = window.CloudCannonAPI.useVersion('v1', true);
const allFiles = await api.files();
for (const file of allFiles) {
const data = await file.data.get();
console.log(file.path, data?.title);
}Call api.files() to get an array of every file in your Site.
Iterate over the files. Each file object has the same interface, so you can call file.data.get(), file.data.set(), file.content.get(), and all other file methods on it.
To access a file:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
*.currentFile()for the file open in the preview, or*.file('/path/example.md')with a path relative to your Site root.
const current = api.currentFile();
const specific = api.file('/content/pages/about.md'); Access the file currently open in the Visual Editor.
Access the file /content/pages/about.md, regardless of what is currently open in the Visual Editor.
Not every page in the Visual Editor has an associated file. If *.currentFile() is called for a page without an associated file, it throws an error. Wrap *.currentFile() in a try/catch block to handle this gracefully.
If you call a method on a file object whose path does not back a real file (for example, *.file('/does-not-exist.md').data.get()), the method resolves to undefined instead of throwing. Check the return value before using it.
To handle a page with no associated file, wrap *.currentFile() in a try/catch and check the result before using it:
let file;
try {
file = api.currentFile();
} catch (error) {
console.warn('No file is associated with this page.');
}
if (file) {
const data = await file.data.get();
console.log(data);
}api.currentFile() throws when the current page has no associated source file, such as a generated pagination page, so call it inside a try/catch.
Handle the no-file case here, rather than letting the error propagate. This example logs a warning.
Only use file if it was assigned. The catch leaves it undefined when the page has no file.
Collections#
A Collection is a folder of files with a similar format, defined under collections_config in your CloudCannon Configuration File. Calling *.collections() on the API Object returns an array of every configured Collection on the Site, while *.collection() accesses one.
To list all Collections:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
await *.collections()for an array of every configured Collection.
const allCollections = await api.collections();
for (const collection of allCollections) {
console.log(collection.collectionKey);
}Call api.collections() to get an array of every configured Collection on the Site.
Log each collectionKey, which matches the Collections in your CloudCannon Configuration File.
To access a Collection and list its files:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
*.collection('example-key'), whereexample-keyis a Collection key undercollections_configin your CloudCannon Configuration File. - Await
*.items()on the returned object for an array of file objects.
const posts = api.collection('posts');
const files = await posts.items();
for (const file of files) {
const data = await file.data.get();
console.log(file.path, data?.title);
}Access the posts Collection. The key must match an entry under collections_config in your CloudCannon Configuration File.
Call posts.items() to get an array of file objects in the Collection. Collections are always configured with a folder path, so posts.items() always returns an array.
Iterate over the files. Each file object has the same interface as api.currentFile(), so you can call file.data.get(), file.data.set(), file.content.get(), and all other file methods on it.
Datasets#
A Dataset is a structured data file or folder defined under data_config in your CloudCannon Configuration File, such as navigation links, locale strings, or site settings. Calling *.datasets() on the API Object returns an array of every configured Dataset on the Site, while *.dataset() accesses one.
To list all Datasets:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
await *.datasets()for an array of every configured Dataset.
const allDatasets = await api.datasets();
for (const dataset of allDatasets) {
console.log(dataset.datasetKey);
}Call api.datasets() to get an array of every configured Dataset on the Site.
Log each datasetKey, which matches the Datasets under data_config in your CloudCannon Configuration File.
To access a Dataset and list its files:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Call
*.dataset('example-key'), whereexample-keyis a Dataset key underdata_configin your CloudCannon Configuration File. - Await
*.items()on the returned object. If the Dataset is a folder path, it returns an array; if it is a single file path, it returns one file object.
const locales = api.dataset('locales');
const result = await locales.items();
if (Array.isArray(result)) {
for (const file of result) {
const data = await file.data.get();
console.log(file.path, data?.title);
}
} else {
const data = await result.data.get();
console.log(result.path, data?.title);
}Access the locales Dataset. The key must match an entry under data_config in your CloudCannon Configuration File.
Await locales.items() on the Dataset handle. result is either an array of file objects or one file object, depending on whether the Dataset is a folder path or a single file path.
When result is an array, loop each file and call file methods such as file.data.get(), the same way you would with the files in a Collection.
When result is a single file object, call file methods on that object directly.
Because *.items(), *.files(), *.collections(), and *.datasets() return arrays, you can filter, sort, or transform the results with standard JavaScript. For worked examples, including filtering a Collection by a field, please read our documentation on common patterns for the Visual Editor API.
Type guards#
The API Object provides the *.isAPIFile(), *.isAPICollection(), and *.isAPIDataset() type guard functions to check whether an object is a file, Collection, or Dataset. These are useful when working with mixed results or when you need to narrow the type of an object before calling methods on it.
To branch on a value that might be a file, Collection, or Dataset:
- Open your website files in your local development environment.
- Open the JavaScript file where your Visual Editor API integration runs.
- Pass the value to
*.isAPIFile(),*.isAPICollection(), or*.isAPIDataset().
When a guard returns true, call only the methods that exist on that type (for example, *.data.get() on a file object, or await *.items() on a Collection or Dataset handle), so TypeScript and the runtime agree on the object shape.
if (api.isAPIFile(obj)) {
const data = await obj.data.get();
}
if (api.isAPICollection(obj)) {
const items = await obj.items();
}
if (api.isAPIDataset(obj)) {
const items = await obj.items();
}api.isAPIFile() returns true if obj is a file object, allowing you to safely call file methods on it.
api.isAPICollection() returns true if obj is a Collection object.
api.isAPIDataset() returns true if obj is a Dataset object.