Sites have truly unlimited opportunities in Drupal 8 — they are not even limited by the “website” concept! They can go far beyond it, and easily exchange data with all imaginable applications. Special opportunities for that have appeared thanks to built-in web services, which are listed among Drupal 8’s main improvements. Let’s see what web services can give you, how they work, and what extra special options are added by other Drupal 8 modules.
Web services: benefits and principles of work
The main web services role is that of a “communicator” between a Drupal website and an external application. This may mean pulling data from a Drupal website, or, vice versa, pushing it there.
Could you imagine the possible gains from outputting your website content, say, to an iOS or Android app? In addition, payment gateways, CRMs, inventory management systems, smart watches and any other apps are ready to work with your Drupal 8 website. And that’s just the beginning of what is possible in the hands of good Drupal developers!
Web services help websites and apps find “common language” — Drupal’s HTML data is magically transformed into the formats that are easily understandable for apps (JSON, XML, HAL+JSON, and so on).
Thanks to web services, you can create an API on your site that other apps can ask to connect to, and you can grant permissions to them. Drupal nodes, taxonomy, collections of content (views) or other information can be fetched from your site.
A glimpse at headless Drupal
Web services in Drupal 8
Drupal 7 and even Drupal 6 had some support for web services, but that was in the form of contributed modules.
Thanks to Web Services and Context Core Initiative (WSCCI), Drupal 8 has web services built into core, and it has been greatly enhanced.
Core Drupal 8 modules for web services
So the big integration story begins with enabling the four core Drupal 8 modules responsible for web services.
- The Serialization module serializes and deserializes your Drupal website data to and from formats like JSON, XML, or HAL+JSON by normalizing and encoding it.
- The HAL module adds data normalization by means of the Hypertext Application Language.
- The RESTful Web Services module exposes your site’s entities or other data in particular API endpoints.
- The HTTP Basic Authentication (Basic Auth) module authenticates the username and password from a request.
Some contributed Drupal 8 modules for web services
In addition, there are extra opportunities brought by very helpful contributed Drupal modules.
This contributed module offers a handy user interface for using core web services. When it is installed and enabled, this interface appears in Configuration — Web services — REST.
Here is a module that lets API builders use GraphQL — a modern query language. This is a nice alternative to the REST architecture traditionally used for APIs. Queries are shaped by the client side precisely to get all they want immediately. Obtaining structured data (including that with a complex hierarchy) is based on the schema.
The JSON API contributed module also offers an alternative to REST. It generates an API server with the JSON API specification. According to Drupal founder Dries Buytaert, both JSON API and GraphQL couuld be promising modules in Drupal core some day.
The most popular module for web services in Drupal 7, the Services module has changed greatly for its 8th version. Its benefits include great API customization options, support for configuration entities, multiple response formats, etc.
While providing a RESTful API, this module offers advanced support for translations, file attachments, revisions, UUID references, opportunity for clients to subscribe to real-time change notifications, etc.
The Subrequests module we once described is a new and interesting module for Drupal 8. It groups any sets of requests together to improve performance. The JSON document named “blueprint” contains all the necessary information.
All these core modules, as well as these and other contributed modules, do an awesome job! However, unlike simple cases of setting up an API, more complex ones usually require some custom code writing.
Whatever your ideas about third-party integration are, you can contact our Drupal developers. They will handle even the most complicated projects with web services.
Let your website go beyond the boundaries and give you more!