SDKs overview

DatoCMS provides Software Development Kits (SDKs) and client libraries designed to facilitate interaction with its Content Delivery API (CDA) and Content Management API (CMA). These SDKs abstract the underlying HTTP requests and response parsing, enabling developers to integrate content into applications using familiar programming language constructs. DatoCMS offers official SDKs for popular languages such as JavaScript, Ruby, PHP, and Python, alongside community-contributed libraries. The SDKs typically handle tasks like authentication, request formatting, and data deserialization, reducing the boilerplate code required for content operations.

The DatoCMS Content Delivery API supports both GraphQL and RESTful interfaces, allowing developers to choose the query method that best suits their project requirements. The Content Management API (CMA) operates primarily via REST, enabling programmatic creation, updating, and deletion of content. SDKs serve as wrappers around these APIs, streamlining tasks such as fetching published content, managing drafts, or uploading assets. This approach aims to reduce development time and potential errors when building applications that consume or manage content from DatoCMS.

Official SDKs by language

DatoCMS maintains official SDKs for several programming languages to ensure reliable and well-supported integration with its platform. These SDKs are developed and maintained by the DatoCMS team, offering comprehensive coverage of the API functionalities and adherence to best practices for each language environment. The official libraries are typically the recommended starting point for developers due to their stability and direct support from the vendor.

Language Package Name Install Command Maturity
JavaScript datocms-client npm install datocms-client or yarn add datocms-client Stable
Ruby dato-cms-client gem install dato-cms-client Stable
PHP datocms/datocms-php-client composer require datocms/datocms-php-client Stable
Python python-datocms pip install python-datocms Stable

Installation

Installing DatoCMS SDKs involves using the standard package managers for each respective programming language. The process is designed to be straightforward, allowing developers to quickly add the necessary libraries to their projects. Detailed installation instructions and prerequisites are available within the DatoCMS scripting documentation.

JavaScript

For JavaScript environments, the datocms-client package is installed via npm or yarn:

npm install datocms-client
# or
yarn add datocms-client

Ruby

Ruby projects typically use Bundler for dependency management. Add the dato-cms-client gem to your Gemfile:

# Gemfile
gem 'dato-cms-client'

# Then run:
bundle install

Alternatively, install it directly:

gem install dato-cms-client

PHP

PHP applications use Composer for package management. Add the DatoCMS PHP client to your project:

composer require datocms/datocms-php-client

Python

Python projects use pip for package installation:

pip install python-datocms

Quickstart example

This example demonstrates how to fetch content using the DatoCMS JavaScript SDK. The process typically involves initializing the client with an API token and then making a query to the Content Delivery API. This example uses a GraphQL query to retrieve published records.

First, ensure you have the datocms-client installed:

npm install datocms-client graphql

Then, create a JavaScript file (e.g., get-content.js) and add the following code. Replace YOUR_READ_ONLY_API_TOKEN with an actual read-only API token from your DatoCMS project. You can find your API tokens in the DatoCMS administrative interface under "Settings > API tokens".

import { SiteClient } from 'datocms-client';
import { buildClient } from '@datocms/cma-client-node'; // For CMA, if needed

const API_TOKEN = 'YOUR_READ_ONLY_API_TOKEN'; // Use a read-only token for CDA

async function fetchContent() {
  // Initialize the CDA client
  const client = new SiteClient(API_TOKEN);

  // Example: Fetch all records of a specific model using GraphQL
  // For more complex queries, define your GraphQL query string.
  const query = `
    query MyPageQuery {
      allPosts {
        title
        slug
        _firstPublishedAt
      }
    }
  `;

  try {
    const response = await client.request(query);
    console.log('Fetched content:', JSON.stringify(response, null, 2));

    // Example: Fetch a single item by ID (REST API equivalent)
    // const item = await client.items.find('ITEM_ID');
    // console.log('Fetched single item:', item);

  } catch (error) {
    console.error('Error fetching content:', error);
  }
}

fetchContent();

To run this example, execute it using Node.js:

node get-content.js

This script initializes the DatoCMS client with your API token and then executes a GraphQL query to retrieve all posts, printing the results to the console. For detailed API methods and more complex use cases, refer to the DatoCMS Content Delivery API documentation.

Community libraries

Beyond the official SDKs, the DatoCMS ecosystem benefits from various community-contributed libraries and integrations. These tools often address specific use cases, integrate with particular frameworks, or provide alternative ways to interact with the DatoCMS APIs. While not officially maintained by DatoCMS, community libraries can offer valuable functionality and extend the platform's reach into different development environments or workflows.

For example, integrations exist for popular front-end frameworks and static site generators, often providing components or plugins that simplify content fetching and rendering. Developers might find libraries that streamline image optimization with DatoCMS assets or offer custom tools for content migration.

When considering community libraries, it is advisable to check their maintenance status, documentation, and community support channels. Resources like DatoCMS's GitHub organization or community forums (e.g., GitHub topics related to DatoCMS) can be starting points for discovering these contributions. Developers should evaluate these libraries based on their project requirements, ensuring compatibility and ongoing support.

It is important to note that while community libraries can enhance development workflows, only the official SDKs receive direct support and guarantees from DatoCMS. Integrating with a headless CMS like DatoCMS involves understanding how to fetch data effectively, and modern best practices often involve a combination of official SDKs for core API interactions and purpose-built integrations for specific frameworks or tools, as supported by the broader headless CMS ecosystem.