A Beginners Guide to the Drupal Services Module

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:

Author

  • Steve Burge

    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.

0 0 votes
Article Rating
Subscribe
Notify of
11 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Bevan Rudge
Bevan Rudge
9 years ago

I recommend against Services module. Its concepts are quickly becoming antiquated as more RESTful conventions become more common and accepted as best practices. Plus, its security releases this year show it leaves much to be desired: [url=https://www.drupal.org/node/2184843]https://www.drupal.org/node…[/url] [url=https://www.drupal.org/node/2189509]https://www.drupal.org/node…[/url] [url=https://www.drupal.org/node/2344389]https://www.drupal.org/node…[/url]
Instead I recommend Rest WS ([url=https://www.drupal.org/project/restws)]https://www.drupal.org/proj…[/url] maybe RESTful ([url=https://www.drupal.org/project/restful)]https://www.drupal.org/proj…[/url] or a full “headless” implementation that has a pure REST API (the API server does not render HTML, and might not even be Drupal) and pure static resources for the front end (JavaScript, CSS, HTML).
So I think this tutorial is useful for existing Drupal websites that already uses services. In that case, the priority would be to secure the website’s code, audit it for malicious attacks and consider switching to something other than Services.

pepperstreet
9 years ago
Reply to  Bevan Rudge

Hello Bevan, many thanks for your thoughts and related links.

Stefanos Petrakis
Stefanos Petrakis
9 years ago
Reply to  Bevan Rudge

Hallo Bevan,
Just a quick question:

Would you consider [url=http://Weather.com]Weather.com[/url]’s use of Services a bad call?
Best regards,
Stefanos

Bevan Rudge
Bevan Rudge
9 years ago

I do not know anything about [url=http://Weather.com]Weather.com[/url]’s usage of Services module, their reasons for using it, or whether they considered alternatives. So I could not comment.
I do know that [url=http://Weather.com]Weather.com[/url] has been in development for many months (years?). Meanwhile alternatives to Services have only recently matured, and are still probably not as mature as Services. And Services module’s security issues are fairly recent.
So it would not be surprising for me if [url=http://Weather.com]Weather.com[/url] had considered alternatives and still chose Services.

Eric Steinborn
Eric Steinborn
8 years ago
Reply to  Bevan Rudge

I’m sorry, Services module is maintained by Acquia and [url=http://Examiner.com]Examiner.com[/url]. Both modules you linked to are minimally installed RESTful (422 installs) and Rest WS (4,911 installs). Not to mention that Rest WS, the highest installed module, is “Seeking a New Maintainer.”
I’ll skip your suggestion and go with D8 Core’s choice of Services. Services is in D8 Core.

Eric Steinborn
Eric Steinborn
8 years ago
Reply to  Eric Steinborn

A reply to my reply. I just learned that Webchick recommends restWS for web services, as it most closely resembles the D8 port of services.
[url=https://twitter.com/webchick/status/623876722044334080]https://twitter.com/webchic…[/url] (Slide 65, and you may need to download the full version to see web services at the bottom)
My initial comment was incorrect.

Vincent
Vincent
9 years ago

Thank you for your post very much.
I’d translated this article into Chinese and posted on a Drupal community ([url=http://is.gd/EYhLsI)]http://is.gd/EYhLsI)[/url].
Please let my know if you don’t like it. I’ll remove it as you will, thanks again.

RT
RT
9 years ago

I’ve done all the steps as stated, but for a webform, and I am getting only the sid while I have included all my other fields to be displayed? Any help on this plz?

Vilash Varghese
Vilash Varghese
8 years ago

Can we use FBlogin with services module. When the user logins with with from mobile app then the user should be logined/registered into drupal site.

pratika
pratika
6 years ago

I got this only “This XML file does not appear to have any style information associated with it. The document tree is shown below.
View myrestapi could not be found

Is this correct?

MARCO AURELIO ROCCA
MARCO AURELIO ROCCA
4 years ago

very nice! Thanks!

11
0
Would love your thoughts, please comment.x
()
x