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.
#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.
- Click Edit Resources
- You’ll now be able to edit different settings for your server
#3. Server Settings
- The Server tab will allow you to choose the formats and parser types for the server:
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:
- Click the Authentication tab
- Choose your OAuth context:
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:
#4. Creating the View
- Go to Structure > Views > Add New View
- Create a view, using only the block format:
- Click Continue and Edit
- Click Add at the top of the View, then click Services.
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:
- Enter a Path such as “myrestapi”:
- Look down at your preview and you’ll see a preview of your your REST API.
- Save this view.
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:
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:
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.
Hello Bevan, many thanks for your thoughts and related links.
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
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.
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.
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.
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.
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?
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.
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?
very nice! Thanks!