I'm using the Content Management JS SDK, version 5.21.1
Trying to create a new image asset but getting an error. I've attempted uploading the image first and calling createAsset:
const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const upload = await environment.createUpload({ file: bytes });
const asset = await environment.createAsset({
fields: {
title: {
[this.locale]: title
},
description: {
[this.locale]: description
},
file: {
[this.locale]: {
fileName: fileName,
contentType: contentType,
uploadFrom: {
sys: {
type: 'Link',
linkType: 'Upload',
id: upload.sys.id
}
}
}
}
}
});
asset.processForAllLocales();
return await asset.publish();
I've also tried using createAssetFromFiles directly:
const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const asset = await environment.createAssetFromFiles({
fields: {
title: {
[this.locale]: title
},
description: {
[this.locale]: description
},
file: {
[this.locale]: {
fileName: fileName,
contentType: contentType,
file: bytes
}
}
}
});
asset.processForAllLocales();
return await asset.publish();
Here's the error I get (same for both calls):
{
"status": 422,
"statusText": "Unprocessable Entity",
"message": "Validation error",
"details": {
"errors": [
{
"name": "required",
"path": [
"fields",
"file",
"en-US",
"url"
],
"details": "The property \"url\" is required here"
}
]
},
"request": {
"url": "assets/01Ft5vBdTHzJPzIVJdBOlE/published",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/vnd.contentful.management.v1+json",
"X-Contentful-User-Agent": "sdk contentful-management.js/5.21.1; platform node.js/v13.12.0; os macOS/19.4.0;",
"Authorization": "Bearer ...",
"user-agent": "node.js/v13.12.0",
"Accept-Encoding": "gzip",
"X-Contentful-Version": 1
},
"method": "put",
"payloadData": null
},
"requestId": "07778ff81872ddb45d5e1a7266436e22"
}
From reading your docs, I was under the impression the URL gets automatically created after the asset is created so I'm not sure what this error means.
Any help is greatly appreciated!
So found the root cause:
asset.processForAllLocales();
return await asset.publish();
I needed to call publish on the processed asset:
const processedAsset = asset.processForAllLocales();
return await processedAsset.publish();
Unfortunately, SDK error messages are cryptic and documentation is pretty patchy.