Skip to content

assets

For use with Netlify Visual Editor only.

connector.assets(opts)

Used to define how to manage assets stored on your data source.

Takes an object where the key is a function defined by your connector. The object contains the following property:

ParameterDescription
createUsed to upload an asset to your data source.

Get assets with connector.sync()

With the SDK, you don’t need to define separate logic using connector.assets to get assets from your data source. Instead, you can get assets during data syncs.

create

Receives a function to be used when uploading assets to your data source. The function is passed an object as the first parameter with the following properties:

PropertyDescription
base64(optional) String containing a base64 representation of the asset (provided when working locally using stackbit dev).
fileNameString representing the asset’s filename.
locale(optional) String representing the locale ID for the asset.
mimeTypeString representing the MIME type of the asset.
stateObject containing data stored in initState when addConnector runs.
url(optional) String representing the temporary URL that contains the asset.
userContext(optional) UserContext. An object that contains the user’s email, name, and optional OAuth access tokens defined by the UserContext generic type.

Your function should use the accessToken in userContext to verify and ensure that all changes in the data source are made on behalf of the user. Changes should not use a general management token that is used to read content.

Usage example

connector.assets({
create: async ({ base64, fileName, mimeType, state }) => {
if (base64 === undefined || base64.trim() === "") {
throw new Error("Asset cannot be empty");
}
const cmsAssetData = await state.cmsApiClient.uploadAsset({
base64,
fileName,
mimeType,
});
if (!createdAsset) {
throw new Error(`Failed to create asset`);
}
const { id, url, width, height, createdAt, updatedAt, title } =
createdAsset;
// returning the asset will save it in Visual Editor
return {
id,
title,
url,
_status: status,
_createdAt: new Date(createdAt),
_updatedAt: new Date(updatedAt),
width,
height,
};
},
});

Got it!

Your feedback helps us improve our docs.