Logo

Decoupled Drupal 8 with JSON API: a hot trend to explore

Blog image

Ultra speed, dynamic features, efficient data exchange —
all this and much more is the result of decoupled Drupal 8 with JSON API!

Many Drupal developers are head over heels in love with the concept of headless, or decoupled, Drupal. Our Drupal experts are not an exception — so they will create a perfect decoupled setup for you, or share useful information on this topic. We have already described the Waterwheel ecosystem, the GraphQL query language used in APIs, and, of course, the phenomenon of headless Drupal itself. Drupal founder Dries Buytaert says that Drupal’s API-first initiative brings new capabilities and implementing decoupled Drupal is easier than ever. One of the popular choices here is decoupled Drupal 8 with JSON API. That’s what we will discuss now.

For a start, what is decoupled Drupal?

According to the decoupled, or headless architecture, Drupal is used solely as a powerful backend. Its data is exposed to a front-end provided by some JavaScript framework. These include React.js, Angular.js, Vue,js, Ember.js, Backbone.js, and many more. The JavaScript family of products is characterized by incredible speed and amazing dynamic features for users. This allows for creating fantastic combinations.

To continue, what is JSON API?

Alongside with RESTful web services in Drupal 8 core and GraphQL, JSON API is one of the ways to implement a decoupled Drupal setup. It is a specification for building REST APIs in JSON through which your Drupal website can share its data to a JavaScript front-end.

JSON API defines a set of rules that determine how clients should request the resources they want to fetch or modify, and how servers should respond to these requests. It decides on the HTTP methods, the HTTP response codes, the response body formats, and more.

JSON API is mainly loved for:

  • high productivity
  • minimum requests to the server
  • efficient use of caching
  • great filtering, pagination, and sorting capabilities
  • the ability to fetch related resources along with the requested ones

and much more.

The JSON API module in Drupal 8

If something is cool, there must be a Drupal module for that. That’s an unbreakable rule ;)

So Drupal 8 got the JSON API module, which is very likely to get into Drupal 8 core in the near future. This module is a Drupal-centric implementation of the JSON API specification. While strictly following the latter, it does so in ways that are familiar and inherent to Drupal.

The JSON API module in Drupal 8

 

The JSON API Drupal module works with the core Serialization module (one of the four built-in Drupal 8 RESTful web services).

Decoupled drupal 8 with JSON API

As soon as you enable the JSON API module, you immediately get a ready REST API with zero customization. If you still need customization, you could also use the JSON API Extras module to enable and disable resources, override their names and paths, and so on.

Every unit of data is a resource when it comes to JSON API. It defines all Drupal entity types and bundles as resource types and assigns them URLs by which they will be accessed. Every resource type gets the “type” property fetched from the entity type’s and bundle’s machine name (e.g. node--article).

The URL path always begins with /jsonapi. Then comes the entity type and the bundle type /jsonapi/node/article. To address individual resources, you can optionally add their ID, which in JSON API always means the UUID: /jsonapi/node/article/{uuid}.

To define the relationships between the resources and fetch related resources, JSON API uses entity references.

HTTP requests and responses in JSON API

JSON API uses the standard HTTP methods, namely GET, POST, PATCH, and DELETE.

The request headers look like this:

Accept: application/vnd.api+json
Content-Type: application/
vnd.api+json

The responses look like this:

  • 200 OK (for all GET or PATCH requests that were successful)
  • 201 Created (for successful POST requests, with the newly created resource included in the response)
  • 204 No Content (for DELETE requests that were successful)

Decoupled Drupal 8 with JSON API: time to implement!

Does decoupled Drupal 8 with JSON API attract you? Our developers have a lot of experience in implementing it. Contact our website development team — and we will do it smoothly and reliably for you, or answer any of your questions!