Blog

Home / Blog

Drupal 9 rest api example

This Drupal 9 REST API example showcases how to create, read, update, and delete content using RESTful web services. Learn how to leverage Drupal's powerful backend to build dynamic, interactive web applications with ease.

Drupal api

Drupal API is a powerful set of tools that allows developers to interact with and customize their Drupal sites through programmable interfaces.
It provides a way to connect external services and data sources to Drupal, enabling seamless integration and extending the platform's functionality.

Drupal 9, the latest version of the popular content management system, has introduced several new features and improvements to make building websites even easier and more efficient. One of the most exciting new features is the REST API, which allows developers to interact with Drupal content and services using standard HTTP requests.

In this article, we will explore how to use the Drupal 9 REST API with a simple example. We will create a custom content type called Book and expose its data through the REST API.

Setting up the Drupal 9 REST API

Before we can start using the REST API in our Drupal 9 site, we need to enable the necessary modules. The RESTful Web Services module comes bundled with Drupal core, so there is no need to install any additional modules.

To enable the REST API, go to the Extend page in your Drupal 9 admin interface and enable the RESTful Web Services module. Once the module is enabled, you can configure the REST API settings by going to Configuration > Web Services > REST in the admin menu.

In the REST settings page, you can configure permissions for different REST resources, such as nodes, users, and taxonomy terms. You can also set up authentication methods, such as basic authentication or OAuth, to secure your REST endpoints.

Creating a custom content type

To demonstrate how the Drupal 9 REST API works, we will create a custom content type called Book with fields for the title, author, and publication date. To create a new content type, go to Structure > Content types > Add content type in your Drupal 9 admin interface.

In the Add content type form, enter Book as the name of the content type and add fields for the title, author, and publication date. You can customize the field settings, such as the field type and display format, to suit your requirements.

After creating the Book content type, you can add some sample content to test the REST API. Go to Content > Add Content > Book in the admin menu and fill in the title, author, and publication date fields with some test data.

Exposing content through the REST API

Now that we have created a custom content type in Drupal 9, we can expose its data through the REST API. By default, Drupal exposes REST endpoints for nodes, users, and taxonomy terms, but you can create custom endpoints for your custom content types as well.

To create a custom REST endpoint for the Book content type, we need to define a REST resource plugin in a custom module. Create a new custom module in your Drupal 9 site, such as book_api, and define a new REST resource plugin in the src/Plugin/rest/resource directory.

Here is an example code snippet for the BookResource class:

```php
namespace Drupalook_apiPlugin est esource;

use DrupalCoreEntityEntityTypeManagerInterface;
use Drupal estModifiedResourceResponse;
use Drupal estPluginResourceBase;
use Drupal estResourceResponse;
use SymfonyComponentDependencyInjectionContainerInterface;

/**
* Provides a resource for books.
*
* @RestResource(
* id = book_resource,
* label = @Translation(Book resource),
* uri_paths = {
* canonical = /api/book/{id}
* }
* )
*/
class BookResource extends ResourceBase {

protected $entityTypeManager;

public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats);
$this->entityTypeManager = $entityTypeManager;
}

public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('entity_type.manager')
);
}

public function get($id) {
$book = $this->entityTypeManager->getStorage('node')->load($id);

if (!$book || $book->bundle() !== 'book') {
return new ResourceResponse('Book not found', 404);
}

return new ModifiedResourceResponse($book, 200);
}

}
```

In the BookResource class, we define a REST resource plugin for the Book content type with a canonical URI path /api/book/{id}. The get() method loads a book node by its ID and returns a ResourceResponse object with the book data.

To test the custom REST endpoint, you can make a GET request to the /api/book/{id} URL, where {id} is the ID of a book node. For example, you can access the book with ID 1 by visiting /api/book/1 in your browser.

Conclusion

In this article, we have explored how to use the Drupal 9 REST API with a simple example. By creating a custom content type and exposing its data through a custom REST endpoint, you can interact with Drupal content and services using standard HTTP requests.

The RESTful Web Services module in Drupal 9 provides a flexible and powerful way to build web applications and integrate with external systems. With the REST API, you can create custom endpoints for your content types, users, and taxonomy terms, and retrieve or manipulate data using RESTful conventions.

If you are developing a decoupled application or a mobile app that needs to interact with Drupal content, the REST API is a valuable tool that can streamline your development process and make your site more accessible and extensible. Give it a try in your Drupal 9 projects and see the benefits for yourself!

Drupal development india

Top-notch Drupal development services in India, delivering custom solutions for businesses of all sizes and industries. Trusted by clients worldwide for innovative websites and applications powered by Drupal.