Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Introduction to Building Services with Drupal 7 Series

Video loading...

  • 0:07
    Building Web Services with Drupal 7 Introduction: Building Web Services
  • 0:13
    with Joe Shindelar
  • 0:17
    Welcome to Building Web Services with Drupal.
  • 0:20
    This series is all about helping you get up to speed quickly on what web
  • 0:23
    services are, how they relate to a Drupal site,
  • 0:26
    and how with just a couple of modules from
  • 0:29
    we can create a full featured and customized
  • 0:31
    REST API for our Drupal-based applications.
  • 0:34
    Web services, APIs, structured data, these things
  • 0:37
    are all the rage right now and with good reason.
  • 0:40
    As more and more internet enabled devices start wanting to make use
  • 0:43
    of the data in our websites, we need to give them some way
  • 0:46
    to interact with that data, that isn't point and click in a browser.
  • 0:50
    And that's exactly what Web Services do--
  • 0:52
    provide a way for applications, machines,
  • 0:55
    or maybe even robots to access and make sense of our data
  • 0:58
    without having to infer meaning from things like visual location
  • 1:01
    on the page, which we as humans often take for granted.
  • 1:06
    Apps are without a doubt the elephant in the room here,
  • 1:09
    and the number one reason why most people are
  • 1:11
    interested in building a web services API.
  • 1:14
    So that they or someone else can then build native applications
  • 1:17
    that make use of the data contained within Drupal.
  • 1:20
    Here's an example.
  • 1:21
    I carry around this little device in my pocket
  • 1:24
    that tracks how many steps I take each day
  • 1:26
    and then logs that information to a website.
  • 1:30
    The website in question provides a services API that this little guy
  • 1:35
    can easily send data to and read data from,
  • 1:38
    but it only gets exactly the data that it needs and nothing more.
  • 1:43
    When I log into the website, however, that data
  • 1:46
    is all made available to me in fancy charts and graphs.
  • 1:50
    But at the end of the day, the data is still just a simple number
  • 1:53
    that's displayed on the less sophisticated UI of the tracker.
  • 1:58
    I could probably go on for awhile about all the reasons
  • 2:01
    you should build a web service API, but what you're probably really
  • 2:04
    interested in is how to build a web services API.
  • 2:08
    In this series, we're going to kick things off with a couple of lessons
  • 2:12
    that are more theory-oriented and attempt to explain what exactly
  • 2:16
    a web services API is and why you might want to build one.
  • 2:20
    And since we're going to be creating a REST-based service,
  • 2:22
    I'll also demystify some of the fundamental components of REST.
  • 2:27
    What are HTTP verbs and what does it mean to have a resource-based API?
  • 2:34
    With that basic knowledge in place, we'll use the rest of the series
  • 2:37
    to walk through creating and configuring a RESTful web service
  • 2:42
    using the powerful version three of the Services Module.
  • 2:46
    Drupal is a great place to start for any application that wants
  • 2:49
    to expose structured data as a web service.
  • 2:53
    Because of the fields and content type system
  • 2:55
    that permeates throughout all of Drupal 7,
  • 2:58
    we're already accustomed to storing structured data
  • 3:00
    and working with it via Tools like views and the entity API.
  • 3:04
    The Services Module builds off of this existing structure
  • 3:08
    and allows us to retrieve data like users and nodes in JSON or XML
  • 3:13
    or tons of other formats without really much work at all.
  • 3:18
    Services will also allow us to expose actions that would normally
  • 3:22
    be considered internal to Drupal, things
  • 3:24
    like triggering Drupal to send a password reset email for a user
  • 3:28
    right from within your third-party app,
  • 3:31
    making it so that these third-party apps have the same level
  • 3:33
    of functionality as the website itself.
  • 3:37
    It's really pretty amazing what you can accomplish with Services
  • 3:40
    in just a little bit of basic configuration.
  • 3:42
    Just a few lessons into the series, we'll already have a functioning
  • 3:45
    endpoint up and running, and we'll be
  • 3:47
    able to retrieve nodes from Drupal as JSON objects.
  • 3:52
    But there's more to creating a killer API
  • 3:54
    than just enabling the Services Module,
  • 3:57
    so we'll also talk about the various ways
  • 3:58
    that you can tweak your services endpoint configuration to customize
  • 4:02
    it for your API and for your use case.
  • 4:05
    I also think it's important to understand how
  • 4:07
    a client is expected to make use of that API.
  • 4:11
    And it helps to visualize what the different changes we make
  • 4:14
    in our configuration actually mean to someone
  • 4:17
    trying to access our API.
  • 4:20
    So, we'll take the time to explore making queries against our API
  • 4:24
    using both the cURL command line application
  • 4:27
    and the Chrome REST console plugin.
  • 4:30
    We'll use these tools to do things like adjust the HTTP headers sent
  • 4:35
    in our request to demonstrate that Services is capable of returning
  • 4:39
    a response in various different formats simply based
  • 4:42
    on the incoming request illustrating some
  • 4:44
    of the more powerful features of the module.
  • 4:47
    Then, we'll look at using Session Authentication with our Web Service
  • 4:51
    in order to allow clients to make authenticated requests.
  • 4:55
    Web API speak for logging in a user.
  • 4:58
    The set-up for this on Services side is pretty simple.
  • 5:02
    But making authenticated queries from a mobile app or other client
  • 5:06
    can be tricky.
  • 5:07
    So we've got an entire lesson about performing
  • 5:09
    this authentication handshake.
  • 5:11
    As you're probably well aware, Views is
  • 5:14
    one of the most popular and powerful modules for Drupal 7.
  • 5:17
    And that's true when it comes to building Web Services as well.
  • 5:21
    We'll look at using these Services Views Module to expose the lists we
  • 5:25
    create with Views as resources to our API.
  • 5:28
    And then take that even further by making use of the Views
  • 5:31
    Expose Filters feature to allow more control
  • 5:34
    to clients implementing our API.
  • 5:36
    Having built numerous applications and written lots of code that needs
  • 5:40
    to interact with a third-party web service,
  • 5:42
    I feel that documentation for your API is of the utmost importance.
  • 5:47
    If you don't document your API, how is anyone that doesn't have access
  • 5:50
    to your code base going to know how to interact with it?
  • 5:53
    We'll end this series with a discussion of best practices
  • 5:57
    for documenting RESTful APIs.
  • 6:00
    There's a whole lot to cover, but by the end of this series,
  • 6:03
    you should have all of the tools you need to start creating
  • 6:05
    your own RESTful Web Services using the Services Module in Drupal 7.

Introduction to Building Services with Drupal 7 Series


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: