DatoCMS Content Management API Client (Deprecated)

3.5.24 · deprecated · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates basic initialization and fetching item types using the deprecated `datocms-client`. Includes a strong warning about its deprecation and outlines the recommended migration path.

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

view raw JSON →