DatoCMS Content Management API Client (Deprecated)
The `datocms-client` npm package is a JavaScript client library for interacting with the DatoCMS Content Management API (CMA). It allows developers to programmatically manage content, schemas, and assets within a DatoCMS project. This package is **officially deprecated** as of April 2022, with the last stable version being 3.5.24, published approximately two years ago. The DatoCMS team strongly recommends migrating to the new, fully TypeScript-ready client, `@datocms/cma-client-node` for Node.js environments or `@datocms/cma-client-browser` for browser environments. The new clients offer better type safety, ESM readiness, a smaller package size, and improved file upload methods. This deprecated client will not receive further updates and may experience breaking changes due to ongoing API evolution.
Common errors
-
#/data: failed schema #/definitions/item/links/13/schema/properties/data: "id" is not a permitted key.
cause This error occurs when attempting to use `item.publish()` with the deprecated `datocms-client` and passing an empty object as the request body, which triggers an old serialization bug.fixWith the deprecated client, pass `{ serializeRequest: false }` as the fourth argument to `client.items.publish(itemId, {}, {}, { serializeRequest: false })`. For a long-term solution, migrate to `@datocms/cma-client-node` and use `client.items.publish(itemId)` without any body. -
Error: ENODEV: no such device or address, unlink './image.png'
cause The deprecated `datocms-client` may have inconsistent or limited support for file system operations in various environments, particularly when handling local file uploads.fixThe new `@datocms/cma-client-node` offers specialized helper methods like `createFromLocalFile()` and `createFromUrl()` that are robustly designed for Node.js file system access. For browser environments, `@datocms/cma-client-browser` provides `createFromFileOrBlob()`. Use the appropriate modern client for reliable file uploads.
Warnings
- breaking The `datocms-client` package is officially deprecated and is no longer maintained. It will not receive new features, bug fixes, or security updates. Developers should migrate to `@datocms/cma-client-node` or `@datocms/cma-client-browser` immediately.
- breaking Direct creation, duplication, updating, or deletion of modular block items using legacy endpoints (`POST /items`, `PUT /items/:id`, etc.) was deprecated as of February 1st, 2020. Operations on modular block items must now be done by updating the modular content field on the parent item.
- gotcha When using `item.publish()` with the deprecated `datocms-client`, a known issue exists where passing an empty body (`{}`) can result in an error: "`id` is not a permitted key". The deprecated client's internal serialization rules may cause this.
- deprecated The `exists` filter for string, text, and Structured Text fields in the DatoCMS Content Delivery API (CDA) is deprecated. It previously returned empty strings instead of `null` values, leading to confusing behavior.
Install
-
npm install datocms-client -
yarn add datocms-client -
pnpm add datocms-client
Imports
- SiteClient
const SiteClient = require('datocms-client').SiteClient;import { SiteClient } from 'datocms-client'; - buildModularBlock
import { buildModularBlock } from 'datocms-client';
Quickstart
import { SiteClient } from 'datocms-client';
// ⚠️ WARNING: This package is DEPRECATED. Please migrate to @datocms/cma-client-node.
// See: https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients
const API_TOKEN = process.env.DATOCMS_API_TOKEN ?? '';
if (!API_TOKEN) {
console.error('DATOCMS_API_TOKEN environment variable is not set.');
process.exit(1);
}
async function createItemWithDeprecatedClient() {
try {
const client = new SiteClient(API_TOKEN, {
// 'logApiCalls' is an option available in the deprecated client
// It logs API requests and responses.
logApiCalls: true
});
console.log('--- Using deprecated datocms-client ---');
// Example: Fetch existing item types
const itemTypes = await client.itemTypes.all();
console.log(`Found ${itemTypes.length} item types. Example: ${itemTypes[0]?.api_key}`);
// Example: Create a new item (simplified, usually requires more fields)
// This operation might be subject to the 'id' not permitted key issue.
// const newItem = await client.items.create({
// itemType: itemTypes[0].id, // Use an existing item type ID
// title: `My Deprecated Item ${Date.now()}`,
// // ... other fields
// });
// console.log('Created item:', newItem.id);
} catch (error) {
console.error('Error using deprecated datocms-client:', error);
if (error.findError) {
console.error('API Error details:', error.findError('INVALID_FIELD'));
}
}
}
createItemWithDeprecatedClient();
/*
// Recommended alternative using @datocms/cma-client-node (install separately):
// npm install @datocms/cma-client-node
// import { buildClient } from '@datocms/cma-client-node';
// async function createItemWithNewClient() {
// try {
// const client = buildClient({ apiToken: API_TOKEN });
// console.log('\n--- Using recommended @datocms/cma-client-node ---');
// const itemTypes = await client.itemTypes.list(); // Note: method names might differ (all vs list)
// console.log(`Found ${itemTypes.length} item types. Example: ${itemTypes[0]?.api_key}`);
// // Example: Create a new item (requires a valid item type ID from your DatoCMS project)
// // const modelId = itemTypes.find(mt => mt.api_key === 'article')?.id; // Find an existing model
// // if (modelId) {
// // const newItem = await client.items.create({
// // item_type: { type: 'item_type', id: modelId },
// // attributes: { title: `My New Item ${Date.now()}` }
// // });
// // console.log('Created item with new client:', newItem.id);
// // }
// } catch (error) {
// console.error('Error using new DatoCMS client:', error);
// }
// }
// createItemWithNewClient();
*/