Show examples in:
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.

Example Publish entire record (all locales & non-localized content)
Example Selective publishing (only specified locales or non-localized content)

Query parameters

recursive  Optional  boolean  Examples: true, false

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<string>  Examples: ["en"], ["en","it"]
non_localized_content  Required  boolean  Examples: true, false

Whether non-localized content will be published

Returns

Returns a item resource object.

Other examples

Example Known issue with legacy client: 'id' is not a permitted key

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();