SDKs overview

Drupal, as an open-source content management system (CMS), offers various methods for developers to interact with its core functionality and data programmatically. While Drupal itself is built on PHP, its architecture supports headless implementations, allowing front-end applications built with other languages to consume content via its API. This section focuses on the Software Development Kits (SDKs) and libraries available for integrating with and extending Drupal API functionality.

The primary interaction methods with Drupal's API involve its built-in RESTful Web Services and JSON:API modules, which expose content entities, configurations, and other Drupal resources as structured data. Developers can leverage these endpoints directly, or utilize language-specific client libraries to simplify the process. The ecosystem includes both officially maintained components and a range of community-contributed libraries designed to facilitate common development patterns.

For server-side development within the Drupal environment, PHP is the foundational language, and Drupal's core itself serves as the most comprehensive SDK. It provides a vast array of classes, interfaces, and functions to build modules, themes, and custom functionalities. When building decoupled applications, client-side SDKs for JavaScript are frequently used to interact with Drupal's exposed API endpoints.

Official SDKs by language

Drupal's core philosophy centers on its modular PHP codebase. Therefore, the most direct and comprehensive "SDK" for Drupal development is Drupal Core itself, which provides an extensive API for PHP developers. For interacting with Drupal from external applications, particularly in headless configurations, the official approach is to utilize Drupal's built-in RESTful Web Services or JSON:API modules. These modules expose data and functionality through standardized HTTP endpoints, which can then be consumed by any language with an HTTP client. While there isn't a single, monolithic "Drupal API SDK" package in the conventional sense for all languages, the following table outlines the primary official approaches and components:

Language Primary Package/Approach Description Maturity
PHP Drupal Core API The entire Drupal framework, providing a rich set of APIs for module and theme development, database interaction, content management, and system integration. Stable (Core)
JavaScript Drupal JSON:API Client (Community-driven, but widely adopted) A client library for interacting with Drupal's JSON:API endpoints, simplifying data fetching and manipulation from JavaScript applications. Stable (Community)
Any (HTTP) Drupal RESTful Web Services / JSON:API Built-in Drupal modules that expose content and configuration entities via standardized HTTP endpoints, consumable by any programming language. Stable (Core)

The Drupal API documentation provides a detailed overview of the various APIs available within Drupal Core, covering topics from database abstraction to form processing and routing.

Installation

Installing Drupal's core components for PHP development typically involves Composer, the dependency manager for PHP. For client-side JavaScript interactions, npm or Yarn are the standard package managers.

PHP (Drupal Core)

To set up a new Drupal project, you would use Composer. This command downloads Drupal core and its dependencies:

composer create-project drupal/recommended-project my_drupal_site
cd my_drupal_site

This command creates a new project directory my_drupal_site containing the Drupal codebase. Further configuration and installation via a web browser or Drush (Drupal Shell) would follow. More detailed instructions are available in the Drupal Composer documentation.

JavaScript (Drupal JSON:API Client)

For front-end JavaScript applications interacting with a decoupled Drupal instance, a common approach is to use a community-maintained JSON:API client. One popular option is jsonapi-client (or similar libraries tailored for Drupal's JSON:API). You can install it using npm or Yarn:

npm install --save jsonapi-client

Or with Yarn:

yarn add jsonapi-client

This adds the client library to your JavaScript project's dependencies, allowing you to make structured requests to your Drupal site's JSON:API endpoints.

Quickstart example

PHP (Within Drupal Module)

This example demonstrates how to create a custom block in Drupal using PHP, which is a common way to extend functionality within the Drupal ecosystem. This code would reside in a custom module's src/Plugin/Block/ directory.

<?php

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'Custom Greeting' block.
 *
 * @Block(
 *   id = "my_module_greeting_block",
 *   admin_label = @Translation("Custom Greeting Block"),
 *   category = @Translation("Custom"),
 * )
 */
class GreetingBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    // Retrieve the configured name from block settings, or default to 'World'.
    $name = $this->configuration['name'] ?? 'World';
    return [
      '#markup' => $this->t('Hello, @name!', ['@name' => $name]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#description' => $this->t('Enter a name to greet.'),
      '#default_value' => $config['name'] ?? '',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $this->configuration['name'] = $form_state->getValue('name');
  }

}

This example demonstrates using Drupal's Block API to create a configurable block. It leverages BlockBase, translation functions ($this->t()), and form API elements (textfield) provided by Drupal Core. For further details on Drupal's Block API, refer to the official Drupal Block API documentation.

JavaScript (Decoupled Front-end)

This example shows how a JavaScript front-end might fetch content from a Drupal instance using its JSON:API endpoint. Assume a Drupal site is configured with the JSON:API module enabled and exposes a /jsonapi/node/article endpoint.

// Assuming jsonapi-client is installed and configured
import { JsonApi } from 'jsonapi-client';

const drupalApiUrl = 'https://your-drupal-site.com/jsonapi';
const client = new JsonApi(drupalApiUrl);

async function fetchArticles() {
  try {
    // Fetch all article nodes
    const response = await client.findAll('node--article', {
      params: {
        'fields[node--article]': 'title,body,status'
      }
    });
    
    console.log('Fetched Articles:', response.data);
    
    // Example: Render titles to a div
    const articlesDiv = document.getElementById('articles-list');
    if (articlesDiv) {
      response.data.forEach(article => {
        const titleElement = document.createElement('h3');
        titleElement.textContent = article.attributes.title;
        articlesDiv.appendChild(titleElement);
      });
    }

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

// Call the function when the page loads
document.addEventListener('DOMContentLoaded', fetchArticles);

This snippet illustrates a basic fetch operation using a hypothetical jsonapi-client. It targets the node--article resource, requesting specific fields. This approach is fundamental for building decoupled JavaScript applications that consume content from a Drupal backend. For a comprehensive guide on integrating with JSON:API, refer to the Drupal JSON:API module documentation.

Community libraries

Beyond the core Drupal API for PHP and direct REST/JSON:API consumption, the Drupal community has developed numerous libraries and tools to enhance developer experience and extend functionality. These often address specific use cases or provide higher-level abstractions.

  • Drush: While not strictly an SDK, Drush (Drupal Shell) is an indispensable command-line interface (CLI) tool for Drupal developers. It provides a vast array of commands for managing Drupal sites, including clearing caches, running database updates, generating code, and interacting with modules. Drush effectively acts as a developer's toolkit for speeding up common tasks. Its official documentation is available on the Drush website.
  • Drupal Console: Similar to Drush, Drupal Console is another CLI tool that assists with development and debugging. It focuses more on code generation, module scaffolding, and interactive commands, helping developers build custom modules and themes more efficiently.
  • PHPUnit for Drupal: For testing Drupal applications, the community heavily relies on PHPUnit. Drupal integrates with PHPUnit, allowing developers to write unit, kernel, and functional tests for their custom modules and themes. The Drupal documentation on PHPUnit details its setup and usage.
  • Headless Drupal Tooling: For JavaScript front-ends, several community-driven projects and libraries exist to streamline integration. These include:
    • Next.js with Drupal: Libraries and examples exist to facilitate building server-side rendered (SSR) or statically generated (SSG) React applications with Next.js, consuming content from Drupal's JSON:API.
    • Gatsby with Drupal: Similar to Next.js, Gatsby starters and plugins are available to source content from Drupal for highly optimized, static sites.
  • REST Client Libraries: While not Drupal-specific, general-purpose HTTP client libraries in various languages (e.g., Guzzle for PHP, Axios or Fetch API for JavaScript) are frequently used to interact with Drupal's RESTful endpoints when a dedicated JSON:API client isn't preferred or available for a specific scenario.

These community-driven tools and libraries demonstrate the flexibility and extensibility of the Drupal platform, allowing developers to choose the best tools for their specific project requirements, whether building within Drupal's backend or creating decoupled front-end experiences.

For additional context on API development best practices, the Mozilla Web Docs on Fetch API provide a foundational understanding of HTTP client interactions in JavaScript.