Building Web Services with Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Web services, APIs, structured data. These things are all the rage right now and with good reason. As more and more internet enabled devices start wanting to make use of the data in our websites we need to give them some way to interact with that data that isn't point and click in a browser. Enter the Drupal 7 services module.

In this series Joe Shindelar will walk through using the services module and teach you how to create a RESTful web services API that exposes Drupal's internal data like nodes and users as JSON or XML that can be easily consumed by a huge variety of clients and programming languages. Paving the way for allowing access to the data contained within your Drupal site to any number of native mobile applications, partner websites, or even internet enabled refrigerators.

The first few lessons in the series are focused more on theory and explaining the various terms that are used both in Drupal and the wider web development sphere to describe web services and all the pieces. Including a description of REST itself and an explanation for why it's a good fit. As well as some discussion about Drupal 7's content model and the ways that it lends itself nicely to serializing data into various formats via the services module in order to be consumed in structured and meaningful ways.

Throughout the series we'll see a couple of examples of how to first enable Drupal to return requested data elements as JSON and then subsequently how to properly format an HTTP request to retrieve those elements. Joe will demonstrate making requests to the API via both cURL and the Chrome REST Console. Doing so will help students to learn about both the Drupal configuration that is required and the way in which any 3rd party applications can request data from Drupal via concrete examples.

With the basics of creating an endpoint and making simple requests out of the way Joe will demonstrate how to enable authentication via the services module and then use both cURL and the REST Console to explain the authentication handshake. A somewhat complex exchange of a username & password for a session authentication token that takes place in HTTP requests and headers whenever you want to perform an action via the API that requires an authenticated user. Learning how this works will enable to you create 3rd party applications that can access Drupal as a specific user allowing for greater personalization.

After that Joe looks at ways to integrate the lists that site administrators create using the views module with services in order to output their contents as structured data. As wall as ways to use views to create entirely new custom services resources. Followed by an example of leveraging the power of views exposed filters via services.

Finally, for those scenarios where you simply can't point and click your way to an answer Joe will teach you how to implement your own custom services resources for both saving and retrieving data from within Drupal. Demonstrating the basic knowledge required to allow you to use services to solve all of your own custom API needs.

Things you'll be able to do after completion of this series:

  • Understand the basic principals of both web services and RESTful APIs.
  • Explain why Drupal is a good fit for web applications that want to allow 3rd party clients to access their data.
  • Create a RESTful web services API using the services module for Drupal 7.
  • Retrieve data from your Drupal site in both JSON and XML format.
  • Create new nodes in Drupal via web services.
  • Make authenticated requests to Drupal via web services.
  • Integrate the views module with services to retrieve views data in various formats.
  • Write your own module providing custom services data and actions.
  • Create meaningful documentation that will enable others to take advantage of your newly created service.

This series provides information about the services module for both intermediate and advanced Drupal users. Since this module makes use of basic Drupal site building skills and custom views users should be familiar with those topics. Also, while not required knowledge of running commands via the command line will be helpful. Finally, if you want to write your own custom services resources you'll need to understand basic module development.

Tutorials in this course
More information

Web services, APIs, and structured data are all the rage right now, and with good reason. As more and more internet enabled devices start wanting to make use of the data in our websites, we need to give them some way to interact with that data that isn't point and click in a browser. Enter the Drupal 7 services module.

In this series Joe Shindelar will walk through the services module and teach you how to create a RESTful web services API that exposes Drupal's internal data like nodes and users as JSON or XML. This paves the way for a huge variety of clients using a number of programming languages to access the data contained within your Drupal site, from native mobile applications and partner websites to internet enabled refrigerators.

The first few lessons in the series are focused more on theory. They explain the various terms that are used both in Drupal and the wider web development sphere to describe web services and all their components. Joe describes REST itself and explains why it's a good fit, and discusses Drupal 7's content model and the ways that it lends itself nicely to serializing data into various formats via the services module in order to be consumed in structured and meaningful ways.

Throughout the series we'll see a couple of examples of how to first enable Drupal to return requested data elements as JSON and then subsequently how to properly format an HTTP request to retrieve those elements. Joe will demonstrate making requests to the API via both cURL and the Chrome REST Console. This demonstration will help students to learn about both the Drupal configuration that is required and the way in which any third party applications can request data from Drupal via concrete examples.

With the basics of creating an endpoint and making simple requests out of the way, Joe will demonstrate how to enable authentication via the services module and then use both cURL and the REST Console to explain the authentication handshake: a somewhat complex exchange of a username and password for a session authentication token that takes place in HTTP requests and headers whenever you want to perform an action via the API that requires an authenticated user. This will enable to you create third party applications that can access Drupal as a specific user, allowing for greater personalization.

After that, Joe looks at ways to integrate the lists that site administrators create using the views module with services in order to output their contents as structured data, as well as ways to use views to create entirely new custom services resources, followed by an example of leveraging the power of views exposed filters via services.

Finally, for those scenarios where you simply can't point and click your way to an answer, Joe will teach you how to implement your own custom services resources for both saving and retrieving data from within Drupal, demonstrating the basic knowledge required to allow you to use services to solve all of your own custom API needs.

Things you'll be able to do after completion of this series:

  • Understand the basic principles of both web services and RESTful APIs.
  • Explain why Drupal is a good fit for web applications that want to allow third party clients to access their data.
  • Create a RESTful web services API using the services module for Drupal 7.
  • Retrieve data from your Drupal site in both JSON and XML format.
  • Create new nodes in Drupal via web services.
  • Make authenticated requests to Drupal via web serivces.
  • Integrate the views module with services to retrieve views data in various formats.
  • Write your own module providing custom services data and actions.
  • Create meaningful documentation that will enable others to take advantage of your newly created service.

This series provides information about the services module for both intermediate and advanced Drupal users. Since this module makes use of basic Drupal site building skills and custom views, users should be familiar with those topics. Also, while not required, knowledge of running commands via the command line will be helpful. Finally, if you want to write your own custom services resources you'll need to understand basic module development.

Additional resources

Drupal 7 services module

More information

This lesson covers what an API is and why you might want to build one. Joe also explains the basic concepts behind any API and provides real world examples of existing APIs that you're probably familiar with as well as use cases for when and why you might want to build your own.

More information

In this lesson Joe shows you where to locate the services module and it's documentation. Then walks through installing the services module and confirming that it's working. Followed by a quick overview of the module's codebase and a general overview of what the services module provides.

Additional resources

Services Module

More information

The API that we're going to be building through this series is a REST based API so we need to understand the basic tenants of RESTful web services. In this lesson Joe gives a short presentation explaining the basic terminology, workflow, and tennants of REST based web services.

Additional resources

The RESTful Cookbook

Roy Fielding's REST dissertation

More information

This lesson walks through defining the URL that will serve as the endpoint for your API as well as all the basic configuration for your new services endpoint with the exception of resource configuration which is covered in the following lesson.

Additional resources

Setting up a basic endpoint

More information

The services module exposes most of Drupal core's data as resources and provides CRUD operations and additional actions for each. In this lesson Joe will look at the available resources, talk about the various CRUD operations and actions and then enable the node resource and demonstrate retrieving node data as JSON from the services endpoint.

Additional resources

JSON Formatter - Chrome plugin to display results of a JSON request in a nicer way.

More information

While cURL may not be the simplest way to interact with a REST API it is the most ubiquitous and the one that is most often referenced in documentation through the web. So to ensure that students have at least a baseline of understanding, and will know how to read the documentation in the future, in this lesson Joe takes a quick look at how to test our API with cURL by retrieving, and creating data.

Example Commands:

`curl http://localhost/demos/services-7x/docroot/api/v1/node/1`

`curl --data '{"title":"hello world!","type":"page"}' --header "Content-Type:application/json" http://localhost/demos/services-7x/docroot/api/v1/node`

Additional resources

cURL Website

cURL Scripting Documentation

More information

Instead of using the CLI to test our API we can use the powerful Chrome REST Console plugin instead. This provides a nice GUI and makes it a little easier to both test and understand what is going on. In this lesson Joe looks at installing the plugin and making basic requests to our API using the REST Console interface.

Additional resources

The Chrome plugin demonstrated in this video is no longer available but there are many alternatives available. Use Postman or search for "REST client" to find tooling options.

More information

In this lesson Joe will demontrate using additional parameters and arguments to refine the returned data when making requests from our API. As well as walking through the code that shows where the various parameters are defined and how you can find what options are available. Finally, we install the services tools module which provides some additional documentation.

Additional resources

Services Tools

More information

Services implements resources for all of core's basic data types but sometimes the information returned in the response is either to much or to little and we want to make changes to the response data. In this lesson Joe will look at how we can write a simple custom module that implments basic hooks provided by the services module to allow us to alter the response returned by a request to the node resource.

More information

Most of the actions we want to perform and data we want to retrieve from an API is likely restricted to authenticated users. This lesson will outline the process of making an authenticated request and walk the user through the configuration that is required for our services endpoint in order to start allowing session based authentication.

More information

In this lesson Joe walks through making authenticated requests to our API with cURL. Although cURL can be a bit verbose when making authenticated requests it serves as a good way to talk about all the headers that are a required and a lowest common denominator for how you could accomplish authentication in just about any language or application.

Example commands

curl http://services-demo.lan/api/v1/user/login -d '{"name":"admin","pass":"admin"}' -H "Content-type: application/json" -H "Accept: application/json"

curl http://services-demo.lan/api/v1/system/connect -X POST

curl http://services-demo.lan/api/v1/system/connect -H "Cookie: SESS60f8c5b86739b7e326223b4ef35867b2=A86XHGJWlnDcMOGcArbOT-qHrsIi5P2NrcoNTXwWluw" -H "X-CSRF-Token: T77haXwD7JKOJsBlKP3p3kLbjQO96bQWvGJAE1_PUZM" -X POST

curl http://services-demo.lan/api/v1/user/login -X POST -H "Content-type: application/json" -H "Accept: application/json"

More information

In this lesson Joe walks through making authenticated requests using the Chrome REST Console Plugin

Additional resources

The Chrome plugin demonstrated in this video is no longer available but there are many alternatives available. Use Postman or search for "REST client" to find tooling options.

More information

Security note: As of Services Views 7.x-1.3 it is recommend that you only use the module to create services using the "Services" display method described in the 2nd 1/2 of this video. You can read more about this change here.

All the power of views made available to your API. Really, what else is there to say? In this lesson Joe walks through installation and basic configuration of the services views module which provides the ability to expose views as resources via the services module.

Additional resources

Services Views Module

More information

In addition to using views to expose lists of things via our API we can also take advantage of views exposed filters to allow API consumers more control over the data they are receiving in a request. In this lesson Joe looks at accessing views exposed filters via REST requests by adding an exposed filter to both of the perviously created views and then walking through how the configuration of the view changes the behavior of the filter when used via the services API.

More information

In addition to the built in support for core's data and actions the services module also provides a robust framework for exposing the data and actions of our custom modules as an API. In this lesson Joe writes a basic module which creates a custom resource for saving and retrieving a "checked in" status for an authenticated user.

Example commands:

Check a user's status

curl http://localhost/demos/services-7x-test/docroot/api/v1/drupalsquare/1 -H "Accept: application/json"

Check-in a user

curl http://localhost/demos/services-7x-test/docroot/api/v1/drupalsquare/checkin -X POST -H "Content-type: application/json" -H "Accept: application/json" -d '{"uid":1}'

Additional resources

Creating a resoruce for Services 3.x

More information

An API that exposes data and actions to a 3rd party is only as good as the documentation for that API. Without good documentation no one will be able to make use of the API you just spent so much energy creating. In this lesson Joe shows some of his favorite API documentation examples and then walks through creating some basic documentation for the resource we created in the previous lesson.

Additional resources

Example - GitHub API Documentation

Example - Recurly Documentation

http://apiary.io - tool to assit in writing good documentation