Sorry, no results found for "".

Show examples in:
Javascript HTTP

Content Management API > Record

Publish a record

When the draft/published system is enabled for a model, records will remain in a Draft status until they are Published.

When publishing a record, you can choose to either publish the whole record, or just some of its locales / non-localized content. This is similar to how the "Publish" dropdown button in the UI works.

This is the default behavior when you don't provide a request body.

This will publish the entire record, including all its localized and non-localized fields.

Do not include a request body at all — not even an empty object {}.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
const client = buildClient({ apiToken: "API_TOKEN" });
const itemId = "WxrWMPl3TjeSJYcl6lNCbg";
const publishedRecord = await client.items.publish(itemId);
console.log(publishedRecord);
}
run();
{
"id": "WxrWMPl3TjeSJYcl6lNCbg",
"type": "item",
"localized_title": {
"en": "New English title",
"es": "New Spanish title",
"it": "New Italian title"
},
"non_localized_field": "New lorem ipsum",
"item_type": {
"id": "CcW1FAf4Sd20dAZijsbzuQ",
"type": "item_type"
},
"creator": {
"id": "627975",
"type": "organization"
},
"meta": {
"created_at": "2024-01-04T23:02:07.204+00:00",
"updated_at": "2024-01-04T23:03:53.121+00:00",
"published_at": "2024-01-04T23:03:21.566+00:00",
"publication_scheduled_at": null,
"unpublishing_scheduled_at": null,
"first_published_at": "2024-01-04T23:03:21.566+00:00",
"is_valid": true,
"is_current_version_valid": true,
"is_published_version_valid": true,
"status": "updated",
"current_version": "WEU_H0i2S8mkaV7ML8OTqw",
"stage": null
}
}

Selective publishing is used when you don't want to publish the entire record. Instead, you can publish a combination of:

In this example, we will only publish the en locale. The it and es versions of localized_title will not be published, and will retain their previously published titles. non_localized_field will also keep its previously published value.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
const client = buildClient({ apiToken: "API_TOKEN" });
const itemId = "WxrWMPl3TjeSJYcl6lNCbg";
const publishedRecord = await client.items.publish(itemId, {
content_in_locales: ["en"],
non_localized_content: false,
});
console.log(publishedRecord);
}
run();
{
"id": "WxrWMPl3TjeSJYcl6lNCbg",
"type": "item",
"localized_title": {
"en": "New English title",
"es": "Old Spanish title",
"it": "Old Italian title"
},
"non_localized_field": "Old lorem ipsum",
"item_type": {
"id": "CcW1FAf4Sd20dAZijsbzuQ",
"type": "item_type"
},
"creator": {
"id": "627975",
"type": "organization"
},
"meta": {
"created_at": "2024-01-04T23:02:07.204+00:00",
"updated_at": "2024-01-04T23:03:53.121+00:00",
"published_at": "2024-01-04T23:03:21.566+00:00",
"publication_scheduled_at": null,
"unpublishing_scheduled_at": null,
"first_published_at": "2024-01-04T23:03:21.566+00:00",
"is_valid": true,
"is_current_version_valid": true,
"is_published_version_valid": true,
"status": "updated",
"current_version": "WEU_H0i2S8mkaV7ML8OTqw",
"stage": null
}
}

Query parameters

recursive boolean

When recursive is true, if the record belongs to a tree-like collection, and any of the parent records aren't published, those parent records will published as well. When recursive is false or not specified, an UNPUBLISHED_PARENT error will occur in such cases.

Body parameters

For this endpoint, the body is not required and can be entirely omitted.
content_in_locales Required

Array of valid locale codes in this project to publish.

Type: Array<string>
Examples: ["en"] , ["en", "it"]
non_localized_content boolean Required

Whether non-localized content will be published

Returns

Returns a resource object of type item

Other examples

If you're using the the older, now-deprecated datocms-client instead of the current @datocms/cma-client, there is a known issue with the item.publish() method. It will return an error like:

{
"data": [
{
"id": "abcdef",
"type": "api_error",
"attributes": {
"code": "INVALID_FORMAT",
"details": {
"messages": [
"#/data: failed schema #/definitions/item/links/13/schema/properties/data: \"id\" is not a permitted key."
]
}
}
}
]
}

The workaround is to add {serializeRequest: false} as the third parameter of that method, like:

await client.item.publish(
"1234567890", // record ID
{}, // body
{}, // query string
{ serializeRequest: false } // this is the actual workaround
);

This tells the deprecated client to skip some of its internal serialization rules (which used to work, but no longer) and instead just send the raw syntax that you provide.

While this should allow that method to continue working for the time being, it is important that you upgrade to the modern client as soon as possible. As of 2024, the old client has been deprecated for more than 2 years and will not receive any further updates. It is possible that this method and others will further break over time, possibly impacting production workflows.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
const client = buildClient({ apiToken: "API_TOKEN" });
const itemId = "WxrWMPl3TjeSJYcl6lNCbg";
const publishedRecord = await client.items.publish(
itemId,
{}, // body
{}, // query string
{ serializeRequest: false }, // this is the actual workaround
);
console.log(publishedRecord);
}
run();
{
id: "hWl-mnkWRYmMCSTq4z_piQ",
title: "My first blog post!",
content: "Lorem ipsum dolor sit amet...",
category: "24",
image: {
alt: "Alt text",
title: "Image title",
custom_data: {},
focal_point: null,
upload_id: "20042921",
},
meta: {
created_at: "2020-04-21T07:57:11.124Z",
updated_at: "2020-04-21T07:57:11.124Z",
published_at: "2020-04-21T07:57:11.124Z",
first_published_at: "2020-04-21T07:57:11.124Z",
publication_scheduled_at: "2020-04-21T07:57:11.124Z",
unpublishing_scheduled_at: "2020-04-21T07:57:11.124Z",
status: "published",
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "4234",
stage: null,
},
item_type: { type: "item_type", id: "DxMaW10UQiCmZcuuA-IkkA" },
}