Drupal REST Web Services - What, why and how

Open Source Drupal


Node Author

So, here at Lucius HQ we are planning on building a RESTful API (web services) on top on Drupal distribution OpenLucius.

We want to do this so all 3rd party programmers, thus 3rd party applications, can integrate with OpenLucius. And not only Drupal developers and Drupal modules. 

For example: integrate time tracking with Toggle, invoicing with Freshbooks or integrating with other case trackers like Jira, Asana or Basecamp. And there are a lot more apps out there with huge potential you can tap into.

So, a brief intro in web services in Drupal:

What is a web service API

W3C defines a web service as follows:

‘A Web service is a software system designed to support interoperable machine-to-machine interaction over a network’.

In other words: web services is a documented and defined way for two computers to communicate with each other over the internet. A computer can be anything connected to the internet. So even a Playstation, a smart watch or a thermostat. Think of: 'the internet of things'.

Application Programmer Interface

API means: Application Programmer Interface. API’s define how software and thus computers can communicate with each other. We are constantly using API’s without noticing it. For example, when you attach your laptop to an external monitor. This is done by means of an API. The programs are 'communicating' with each other without any human intervention.

Standardized and documented

An API can also be seen as a standardized and documented way to get access to content and functionality of an application. For example, as an independent developer you can get access to data from Facebook using the Facebook API. An example of Facebook’s ‘Graph API explorer’ that everyone can use:

API documentation

API Documentation is very important, otherwise nobody will know how to use the API to obtain the correct information. An API is worthless without proper documentation.

How does it basically work

An external application makes a request for data through a Drupal web service API. Drupal passes data back in the appropriate structured way (e.g. JSON), so that the external application can use the data. The external program can also create users, create a node, reset a password, etc.

Why web services

In most cases web services are used to provide mobile applications with data. When you look for example at the App nu.nl, then these news items will have to be managed somewhere. These same news items are posted on their website, but also on Android apps and in future maybe on smart TV’s, smart watches and anything else yet to be invented.

Future proof

Web services are future oriented: whatever will come after iOS or Android, the new application platform will also be able to retrieve and modify data via the desired web service API.

In other words: the internet of things can be centrally provided with content, users, etc.

Web services in Drupal

There are several modules in Drupal that can facilitate web services, the most famous are Restws and Services.

These two modules ensure that data and internal functions are openly served to other applications through a Drupal web services API. An external application can 'communicate with these modules' and receive structured data that can be used. Examples of external applications: an iOS or Android app, but also a Playstation, smart TV, smart watch or even a thermostat. In other words, all things in the internet of things.

Drupal Module: Restws

Restws is great in RESTful web services and the necessary CRUD action for all Drupal entities, but has no additional web services like SOAP, XML-RPC, etc. It is also not possible to define and configure ‘service endpoints’.

Drupal Module: Services

The Services module can do everything that Restws can do and more. It is a complete toolkit to provide Drupal with web services. It knows Drupal’s node, entity en CRUD system and provides opportunities to create and configure service endpoints yourself. The module also supports multiple interfaces like REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF and more.

It also provides a number of standard features, allowing you to quickly have the standard web services up and running, for example requesting node content details. This can be done within 10 minutes. Specific use cases are obviously requiring more effort, but with all custom needs this Service module is facilitating a large part of the required functions. Such as creating users, creating a node, reset a password, etc.

Drupal Module: Views data sources

This is a module that lets you create endpoints through Views and serve data through that endpoint. And all this without having to code a line. Relevant data can be configured in the View. Note that this is still an alpha version and can be handy for standard lists: for example the last 10 news items.

More complex use cases

But when the query becomes more complex this module is not working satisfactory yet. You will then have to create a custom endpoint in code and code your own queries. But these custom endpoints do let you hook into the Services module, which is facilitating many functions. So there is no need to code a Drupal web service from scratch.

Web services in Drupal 8

Drupal 8 incorporates web services in the Drupal core, so modules will not be needed anymore!

Wrap up

Ok, that's it for now. But since we are currently working enthusiastically on a major Drupal web services project, blogs will follow with specific use cases about this.

-- Cheers!

Sources

Source header image

This video is a very good resource, thanks Mkorostoff

Ow yeah.., and don't foget to check out other Youtube videos on Drupal web services