Recent searches

in

Access and list files, Collections, and Datasets with the Visual Editor API

Last modified: August 11th, 2026

On this page

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 exposes files, Collections, and Datasets from your Site code. This article covers how to access and list each one, and how to apply type guards before you call type-specific methods on mixed values.

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 *.files() on the API Object returns an array of every file across your Site. To access an individual file, use *.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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call await *.files() for an array of every file in your Site.
JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call *.currentFile() for the file open in the preview, or *.file('/path/example.md') with a path relative to your Site root.
JavaScript
Copied to clipboard
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:

JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call await *.collections() for an array of every configured Collection.
JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call *.collection('example-key'), where example-key is a Collection key under collections_config in your CloudCannon Configuration File.
  4. Await *.items() on the returned object for an array of file objects.
JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call await *.datasets() for an array of every configured Dataset.
JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. Call *.dataset('example-key'), where example-key is a Dataset key under data_config in your CloudCannon Configuration File.
  4. 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.
JavaScript
Copied to clipboard
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:

  1. Open your website files in your local development environment.
  2. Open the JavaScript file where your Visual Editor API integration runs.
  3. 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.

JavaScript
Copied to clipboard
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.

Related Resources

Open in a new tab