| Drupal
A Beginners Guide to the Drupal Services Module
The Services module allows you to provide web services from your Drupal site.

Services is really popular and works with formats such as REST, XMLRPC, JSON and SOAP.

However, when asked about Services during a training class last week, I was realized that the students were asking me because there was little-to-no clear documentation available.

So, I sat down and decided to write a Beginners guide to the Services module.

Here's a 5-step guide to using Services creating a REST API for your Drupal site.

#1. Installation

Setting up the basic REST API requires these three modules:

If you want to also set up authentication for your API, you'll need to add a module such as OAuth.

It's worth noting that you may need to test different versions of these modules. The best documentation I could find on this on drupal.org recommended sometimes using older versions of these modules, in order to avoid bugs.

When you enable these modules, you'll be able to choose the types of server you want. In addition to REST and XMLRPC other modules are available on drupal.org, including SOAP.

media_1416855349642.png

#2. Server Set-up

  • Go to Structure > Services
  • Click Add
  • Enter a "Machine-readable name"
  • Choose your server
  • Enter a "Path to endpoint". This will become part of the URL for your server.
  • Click Save.
media_1416843691744.png
  • Click Edit Resources
  • You'll now be able to edit different settings for your server
media_1416843720710.png

#3. Server Settings

  • The Server tab will allow you to choose the formats and parser types for the server:
media_1416843798880.png

The Authentication tab will allow you to control access to the server. Here's an example with OAuth:

  • Install OAuth
  • Go to Configuration > OAuth > Add Context
  • Set up the details for your OAuth connection
  • Go to Structure > Services > Edit Resources
  • Check the OAuth authentication box:
media_1416844132426.png
  • Click the Authentication tab
  • Choose your OAuth context:
media_1416844177583.png

Finally, the Resources tab allows you to control what your server can do. In this example, we need to make sure we check the Views box:

media_1416855876429.png

#4. Creating the View

  • Go to Structure > Views > Add New View
  • Create a view, using only the block format:
media_1416844380223.png
  • Click Continue and Edit
  • Click Add at the top of the View, then click Services.
media_1416844424789.png

You'll see a message saying "Display "Services" uses a path but the path is undefined."

  • Solve this by clicking the forward slash next to Path:
media_1416844475950.png
  • Enter a Path such as "myrestapi":
media_1416844544032.png
  • Look down at your preview and you'll see a preview of your your REST API.
  • Save this view.
media_1416855041131.png

You can now customize this view, using all of the normal features of Views.

#5. View the server

The final step is to see your server output. There is no automatic link to the server, so you'll need to figure out the URL by using this pattern:

/path-to-endpoint/views/view-machine-name/

In my example, these were my settings:

  • path-to-endpoint: I set the "Path to endpoint" in Part 2 as "myrestapi".
  • view-machine-name: I set the name for view as "myrestapi". You can confirm this by editing your view and looking at the URL for the unique name of your view.

So, here is the URL to my server: /myrestapi/views/myrestapi/

And here is the output:

media_1416854995303.png

Find out more

The best documentation on Drupal.org is this article which goes into more detail than I did here. This is part of the general Services documentation.

If you want to import data to your Drupal site via a REST API, this is the best tutorial I could find.

There is also a really good video available on YouTube from Drupal Overeasy:


About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.