Sorry, no results found for "".
For Modular Content, Structured Text and Single Block fields. If set, returns full payload for nested blocks instead of IDs
Whether you want the currently published versions (published
, default) of your records, or the latest available (current
)
"published"
Returns a resource object of type item
import { buildClient } from "@datocms/cma-client-node";
async function run() { const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const itemId = "hWl-mnkWRYmMCSTq4z_piQ";
const item = await client.items.find(itemId);
// Check the 'Returned output' tab for the result ☝️ console.log(item);}
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" },}
Sometimes, you may wish to fetch a record that has embedded blocks inside Structured Text or Modular Content fields.
By default, those nested blocks are returned as references, like:
[ { item: 'ahxSnFQEQ02K3TjttWAg-Q', type: 'block' }, { item: 'AppHB06oRBm-er3oooL_LA', type: 'block' }]
But if you add the nested: true
query parameter, we'll embed the blocks content inline for you.
import { buildClient } from "@datocms/cma-client-node";
async function run() { // Make sure the API token has access to the CMA, and is stored securely const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const records = await client.items.find("FEzWmQhjQgeHsCrUtvlEMw", { nested: true, // Retrieve its nested block content as well });
// Instead of console.log, console.dir will fully expand nested objects // See https://nodejs.org/api/console.html#consoledirobj-options // This way, we can better see the embedded blocks in the response console.dir(records, { depth: null });}
run();
{ id: "FEzWmQhjQgeHsCrUtvlEMw", type: "item", structured_text_field: { schema: "dast", document: { children: [ { item: { type: "item", attributes: { button_label: "Example button", button_url: "https://www.example.com", }, relationships: { item_type: { data: { id: "SkVjHJSGR5CyK16E8TfJxg", type: "item_type" }, }, }, id: "ahxSnFQEQ02K3TjttWAg-Q", }, type: "block", }, { item: { type: "item", attributes: { nested_structured_text_field: { schema: "dast", document: { children: [ { children: [ { type: "span", value: "This is a " }, { marks: ["emphasis"], type: "span", value: "nested", }, { type: "span", value: " structured text block inside the parent structured text field.", }, ], type: "paragraph", }, { item: { type: "item", attributes: { button_label: "And this is a button inside the nested structured text block", button_url: "https://www.example2.com", }, relationships: { item_type: { data: { id: "SkVjHJSGR5CyK16E8TfJxg", type: "item_type", }, }, }, id: "CGqwjPDsTHKGFy1IbC0RAQ", }, type: "block", }, ], type: "root", }, }, }, relationships: { item_type: { data: { id: "Ty4S40cbQH6_VMNnGdd9KA", type: "item_type" }, }, }, id: "AppHB06oRBm-er3oooL_LA", }, type: "block", }, ], type: "root", }, }, item_type: { id: "UVa_hHEBSeefLEUnwoQFig", type: "item_type" }, creator: { id: "104280", type: "account" }, meta: { created_at: "2024-03-13T17:01:19.243+00:00", updated_at: "2024-03-13T17:14:17.444+00:00", published_at: "2024-03-13T17:14:17.597+00:00", publication_scheduled_at: null, unpublishing_scheduled_at: null, first_published_at: "2024-03-13T17:01:19.326+00:00", is_valid: true, is_current_version_valid: true, is_published_version_valid: true, status: "published", current_version: "DLtyHZ2MTDqYMg7g5mgYEw", stage: null, },}