Sorry, no results found for "".
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 {}
.
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.
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.
Array of valid locale codes in this project to publish.
["en"]
, ["en", "it"]
Whether non-localized content will be published
Returns a resource object of type item
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:
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" },}