Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

REST Easy simplifies custom API development in Drupal by providing base classes and plugin attributes to unify authentication, endpoints, and parameters. It automatically generates OpenAPI documentation and can create Postman collections for your APIs.

Developing a custom API within Drupal can be a cluttered process, requiring separate files for auth plugins, controllers, endpoint routes, OpenAPI documentation, and more. REST Easy simplifies the process by defining base classes for APIs, endpoints, and parameters that provide sensible defaults and unify many of the moving parts under simple plugin attributes.

Features

REST Easy provides three related plugin types: API, Endpoint, and Parameter. These define where your endpoints live, what parameters they accept, how they're validated, what output they produce, and all of the accompanying OpenAPI documentation.

Start by defining an API plugin:

<?php

namespace Drupal\YOUR_MODULE\Plugin\rest_easy\API;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest_easy\Attribute\RESTEasyAPI;
use Drupal\rest_easy\Plugin\APIBase;

/**
 * Class YourAPI
 *
 * @package Drupal\YOUR_MODULE\Plugin\rest_easy\API
 */
#[RESTEasyAPI(
  id: "your_api_id",
  label: new TranslatableMarkup("Your API's Name"),
  auth: ["cookie"],
  base_path: "/your-api-base-path",
  description: new TranslatableMarkup("A description for your API."),
  permission: "a default permission shared across endpoints",
  produces: ["application/json"],
  tags: [
    "Tag 1" => new TranslatableMarkup("A tag you'll use to group endpoints in your documentation."),
    "Tag 2" => new TranslatableMarkup("Another tag."),
  ],
  version: "1"
)]
class YourAPI extends APIBase {

}

Next, define each of the endpoints for your API. Each endpoint is a separate class:

<?php

namespace Drupal\YOUR_MODULE\Plugin\rest_easy\Endpoint;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest_easy\Plugin\EndpointBase;
use Drupal\rest_easy\Attribute\RESTEasyEndpoint;

/**
 * Class YourEndpoint
 *
 * @package Drupal\YOUR_MODULE\Plugin\rest_easy\Endpoint
 */
#[RESTEasyEndpoint(
  api: "your_api_id",
  id: "your_endpoint_id",
  label: new TranslatableMarkup("Your Endpoint's Name"),
  path: "/your-endpoint-path",
  description: new TranslatableMarkup("A description for your endpoint."),
  methods: ["GET"],
  parameters: ["your_parameter", "your_other_parameters"],
  responses: [200, 400],
  tags: ["Tag 1"]
)]
class YourEndpoint extends EndpointBase {

  /**
   * {@inheritdoc}
   */
  public function call() {
    $data = [];
    
    // Add the logic that produces the contents of the response. By default, this will be translated into JSON.

    return $this->jsonResponse($data);
  }
}

Finally, define each of the parameters for your endpoints. These are also separate classes. Note that you can reuse the same parameter across multiple endpoints if you choose.

<?php

namespace Drupal\YOUR_MODULE\Plugin\rest_easy\Parameter;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest_easy\Attribute\RESTEasyParameter;
use Drupal\rest_easy\Plugin\ParameterBase;

/**
 * Class YourParameter
 *
 * @package Drupal\YOUR_MODULE\Plugin\rest_easy\Parameter
 */
#[RESTEasyParameter(
  api: "your_api_id",
  id: "your_parameter",
  type: "string",
  default: "A default value if none is supplied",
  description: new TranslatableMarkup("A description for your parameter."),
  in: "query",
  required: TRUE
)]
class YourParameter extends ParameterBase {

  /**
   * {@inheritdoc}
   */
  public function validate($value): array {

    // Basic validation will check the parameter's type and verify that required parameters are included.
    $errors = parent::validate($value);
    
    // Add your own validation logic by testing the value and appending to the errors array. If you don't have any, you can remove this function entirely.

    return $errors;
  }
}

When you're done, enable your module. REST Easy will register the routes defined by your API's base path and endpoint paths. If you enable the OpenAPI Integration sub-module, you'll find your API's documentation under Configuration > Web services > OpenAPI (/admin/config/services/openapi). If you enable the Postman sub-module, you'll find a link to download your API's Postman collection under Configuration > Web services > REST Easy Postman Collections (/admin/config/services/rest-easy-postman).

Post-Installation

This module is intended to accelerate and simplify API development. It should only be installed if listed as a dependency for another module or as an aid to custom API development.

This module includes a sub-module to integrate with the OpenAPI module.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
18
Tracked since
Jan 2025
Latest release
8 months ago
Releases (12 mo)
4 ▼ from 14
Maintenance
Slowing

Release Timeline

Releases

Version Type Core Release date
1.1.5-alpha1 Pre-release 10–11 Dec 31, 2025
1.1.4-alpha1 Pre-release 10–11 Dec 31, 2025
1.1.3-alpha1 Pre-release 10–11 Sep 18, 2025
1.x-dev Dev 10–11 Sep 18, 2025
1.1.2-alpha1 Pre-release 10–11 Mar 24, 2025
1.1.1-alpha1 Pre-release 10–11 Feb 4, 2025
1.1.0-alpha1 Pre-release 10–11 Feb 3, 2025
1.0.9-alpha1 Pre-release 10–11 Jan 30, 2025
1.0.8-alpha1 Pre-release 10–11 Jan 30, 2025
1.0.7-alpha1 Pre-release 10–11 Jan 29, 2025
1.0.6-alpha1 Pre-release 10–11 Jan 28, 2025
1.0.5-alpha1 Pre-release 10–11 Jan 22, 2025
1.0.4-alpha1 Pre-release 10–11 Jan 22, 2025
1.0.3-alpha1 Pre-release 10–11 Jan 20, 2025
1.0.2-alpha1 Pre-release 10–11 Jan 16, 2025
1.0.1-alpha1 Pre-release 10–11 Jan 7, 2025
1.0.x-dev Dev 10–11 Jan 7, 2025
1.0.0-alpha1 Pre-release 8–11 Jan 7, 2025