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: 

What are Web Services?

Video loading...

  • 0:01
    Building Web Services with Drupal 7 What Are Web Services?
  • 0:05
    with Joe Shindelar
  • 0:07
    You've probably heard the term "API" before or maybe
  • 0:10
    "web services."
  • 0:11
    But what are these things?
  • 0:12
    And why would you want your web application to have one?
  • 0:16
    In this lesson, I'm going to explain what an API is
  • 0:20
    and provide some examples of existing APIs
  • 0:22
    that you might be familiar with.
  • 0:24
    Then we'll look at some of the reasons
  • 0:26
    why you might want to add an API to your Drupal site
  • 0:29
    and the types of problems that you can solve by doing so.
  • 0:33
    Finally, we'll talk a little bit about the things that make Drupal
  • 0:36
    an ideal candidate for building an API to share
  • 0:38
    your structured data with the world.
  • 0:41
    By the end of this lesson, you should
  • 0:43
    be able to explain what an API is, why you want to create one,
  • 0:47
    and why Drupal is a great fit for web services.
  • 0:52
    The primary goal in this presentation
  • 0:53
    is to demystify some of the terminology
  • 0:56
    that we'll be using throughout the rest of the series, terms like "web
  • 0:59
    services" and "API", so that when I say them in future lessons
  • 1:02
    we're all on the same page.
  • 1:04
    In addition to that, I'd also like us to have a better understanding
  • 1:07
    of what exactly it is that the Services module in Drupal
  • 1:11
    is going to be doing for us.
  • 1:13
    So to start, let's define what web services mean.
  • 1:17
    The W3C defines web services as "a software system designed to support
  • 1:22
    interoperable machine-to-machine interaction over a network.
  • 1:26
    I think in human speak what that means is web services
  • 1:30
    is a documented and defined way for two computers to interact with one
  • 1:36
    another, while connected to the internet.
  • 1:38
    Though it doesn't necessarily need to be a computer in the sense
  • 1:41
    of a desktop or a laptop, an internet-connected device
  • 1:44
    could be anything like a fitbit tracker or, as the saying goes,
  • 1:48
    your internet fridge.
  • 1:50
    Web services allow those things to interact with one another
  • 1:54
    in the machine sort of compatible format.
  • 1:57
    In this series, we're going to be building a web service that allows
  • 2:02
    any client to interact with our Drupal site
  • 2:05
    and the data contained within.
  • 2:07
    API, we have to hear the term "web services API" or just "API",
  • 2:12
    or just "web service," for the most part in this series,
  • 2:15
    they're kind of interchangeable.
  • 2:16
    What it refers to is the system that we're building that will allow
  • 2:19
    some arbitrary client or piece of code
  • 2:22
    to talk to our Drupal-powered site.
  • 2:25
    The acronym API stands for "application programmer interface."
  • 2:29
    And APIs, essentially, they specify how different pieces of software,
  • 2:33
    different components should interact with one another.
  • 2:37
    And APIs are all over the place.
  • 2:40
    We make use of them all the time.
  • 2:42
    Pretty much any time we sit at our computer and do anything,
  • 2:44
    we're making use of one API or another.
  • 2:47
    And an example of an API that maybe isn't so obvious
  • 2:50
    would be whenever you copy some text from one application
  • 2:54
    and you paste it into another application.
  • 2:57
    The API there, that copy and paste API,
  • 2:59
    is sort of a system-level thing that any application or client running
  • 3:04
    on your machine can interact with.
  • 3:06
    It can tell that API by following the set of documented protocols,
  • 3:11
    hey, I've got some text that I'd like you to store in the clipboard.
  • 3:14
    And then later on, in a different application, when you hit Paste,
  • 3:19
    that application can ask the clipboard API and say,
  • 3:22
    do you have any text in the buffer that I can paste?
  • 3:25
    Things like that.
  • 3:26
    They make it easy for programs or things written in code to talk
  • 3:31
    to one another without human interaction.
  • 3:34
    Another common way of thinking of APIs
  • 3:36
    is a standardized and documented way to access and interact
  • 3:40
    with your application's content and features.
  • 3:44
    What you see here is a page of documentation from the Twitter API.
  • 3:48
    And it's showing the way that any third-party application
  • 3:52
    with the appropriate permission could access a Twitter status
  • 3:57
    update, meaning that it's possible for me
  • 4:00
    as an independent developer that doesn't work for Twitter to access
  • 4:04
    the full content of one of my Twitter updates and make use of it
  • 4:08
    in any way that I want to.
  • 4:11
    Honestly, I think the term "documented" here is also
  • 4:13
    really important and often overlooked, when it comes to APIs.
  • 4:18
    In my opinion, an API without documentation
  • 4:21
    is kind of a terrible API, because, as a developer, if I can't read
  • 4:25
    your code, since I maybe don't even work for your company
  • 4:28
    or I just don't have access to the source code,
  • 4:31
    and there's no documentation for your API,
  • 4:34
    there's really no way for me to know how to interact with that
  • 4:36
    or what's going on.
  • 4:37
    So I would say that any API is only as good
  • 4:41
    as its documentation and documentation is key.
  • 4:45
    So that brings us to the Services module--
  • 4:47
    when you take web services and API and put all of that together.
  • 4:51
    I mean, that's really where this module gets its name-- Services.
  • 4:53
    It's all about creating web services using Drupal
  • 4:56
    or exposing Drupal's data and internal actions
  • 5:00
    to other applications via an API.
  • 5:03
    The Services module understand Drupal and Drupal's internals,
  • 5:08
    and how it works.
  • 5:08
    It knows what a node is.
  • 5:10
    It knows what a user is.
  • 5:11
    And it knows how to retrieve those pieces of information.
  • 5:14
    And then in addition to that, it knows how to represent those pieces
  • 5:17
    of information in a sort of structured data way,
  • 5:20
    so it can return them as JSON or XML, or whatever we've got
  • 5:24
    configured depending on what the client requests.
  • 5:28
    And then this structured data, like JSON, for example,
  • 5:32
    is something that programming language can make sense of.
  • 5:36
    The Services module is a really great tool for making web APIs
  • 5:40
    with Drupal because it's pluggable.
  • 5:42
    It doesn't assume anything about what type of API
  • 5:46
    you're going to want to build.
  • 5:47
    You could build a REST-based service.
  • 5:49
    You could build a SOAP-based service.
  • 5:51
    You could build an XML-RPC-based service.
  • 5:54
    It provides the underlying framework for doing any of those.
  • 5:58
    It's also really extensible, too.
  • 6:00
    It provides resources or interaction for core's data an actions,
  • 6:06
    but it's really easy to extend it and add
  • 6:08
    your own resources to the web services.
  • 6:11
    So if my custom module or custom entity type
  • 6:14
    needs to be accessible via API, it's really easy for me
  • 6:18
    to write the code to do that with the Services module.
  • 6:21
    And finally, it's just a really popular solution right now.
  • 6:24
    You know, with Drupal, it's always best to be using the same tools
  • 6:27
    that everyone else is using, because you'll
  • 6:29
    find the best documentation and the most help.
  • 6:32
    So what does Services do for us?
  • 6:35
    The Services module, basically, handles incoming requests
  • 6:39
    from some external client and then formats and sends them a response.
  • 6:44
    Your external client asks the Services API for some piece of data
  • 6:49
    or to perform some action via a request, saying,
  • 6:52
    hey, return all of the data that makes up the user
  • 6:55
    object for the user with the ID of 1 or perform some action
  • 7:00
    like save a new email address for this user or log the user in.
  • 7:05
    And then the Service responds with the result
  • 7:07
    of that action or the data that you're requesting.
  • 7:11
    The mechanics of this request and response
  • 7:12
    are going to vary a little bit, depending
  • 7:14
    on the type of server that you're using or the service.
  • 7:16
    So a REST-based service is at a XML-RPC service.
  • 7:21
    Those requests and response are a little bit different,
  • 7:23
    but the end result is the same.
  • 7:25
    You ask Drupal to do something via Services,
  • 7:28
    and then Drupal responds with some output via Services.
  • 7:33
    What do we mean when we talk about Drupal's data and actions?
  • 7:36
    If you think about the applications that we built with Drupal,
  • 7:39
    we can really sum it up to there's data,
  • 7:41
    there's things like nodes and users, and comments,
  • 7:43
    and lists of data, views, files-- all the content of our site.
  • 7:49
    Services understands Drupal's data and its formatting
  • 7:53
    and allows us to access that data in a variety of different formats.
  • 7:57
    In addition to data like that, though, we also
  • 7:59
    have action, which is a little bit more ambiguous.
  • 8:02
    An action might be something like running the code that sends
  • 8:05
    an email, when somebody wants to reset their password.
  • 8:08
    And that kind of thing is normally reserved for PHP or module
  • 8:12
    that are tied in directly to Drupal.
  • 8:14
    Or with a web service, using the service's module,
  • 8:17
    we can actually ask Drupal to execute that code on our behalf
  • 8:21
    without actually having to write the code
  • 8:23
    and call the function directly.
  • 8:26
    I also think it's nice to point out that Drupal is a really
  • 8:29
    good platform for building web services.
  • 8:31
    And I believe that, because web services
  • 8:34
    are all about structured data.
  • 8:36
    Being able to represent a piece of content as JSON or XML,
  • 8:40
    rather than a giant blob of text, is way more useful for the clients
  • 8:45
    or the thing that's consuming your data.
  • 8:48
    Drupal, especially Drupal 7, already stores data in structured chunks.
  • 8:53
    If you think about fields and the content types,
  • 8:56
    we create our content is like a title,
  • 8:59
    author field, a posted date, status about whether the note is published
  • 9:04
    or not.
  • 9:04
    The body and description, and image field, all of those things
  • 9:07
    are individual sort of chunks of data that are really easy to be
  • 9:11
    serialized and sent as JSON, versus if it was just
  • 9:15
    a big static HTML blob, it would be harder
  • 9:17
    for a client to make any sense of that.
  • 9:21
    Imagine that you're a robot and you've
  • 9:23
    been handed a bowl of ice cream.
  • 9:25
    And it's got three different flavors of ice cream in it.
  • 9:27
    And your job is to do something with each
  • 9:30
    of those individual scoops of ice cream.
  • 9:32
    If they're on a big bowl together and they started to melt,
  • 9:36
    it can be hard to identify what each flavor of ice cream
  • 9:41
    is, especially, if they started to melt together.
  • 9:44
    Is this strawberry and vanilla or is it strawberry vanilla?
  • 9:48
    With structured data, what we end up with is,
  • 9:51
    instead of just a bowl of a bunch of scoops of ice cream
  • 9:53
    being sent to the client, you get each individual scoop of ice cream
  • 9:57
    in its own bowl with a nice clear label identifying where it came
  • 10:01
    from and what type of ice cream is in the bowl.
  • 10:03
    This kind of data is a lot easier for me
  • 10:06
    as a developer to deal with via a programming language.
  • 10:10
    So what are some examples of existing
  • 10:12
    APIs that you may be familiar with?
  • 10:14
    Twitter, which is possibly the most popular and most used
  • 10:18
    API on the web right now, allows you to interact with all of the data
  • 10:22
    that Twitter collects from status updates and people's interaction
  • 10:25
    with the service.
  • 10:27
    This is what makes it possible for there to be this huge ecosphere
  • 10:30
    of third-party applications that use Twitter.
  • 10:33
    Because there's a web services API, somebody can write a tool
  • 10:36
    like Buffer, which allows me to create a tweet
  • 10:39
    and schedule it for later.
  • 10:41
    And then it will use these services API on that scheduled date and time
  • 10:45
    to actually post the status update on my behalf.
  • 10:48
    It can do all of this without any internal knowledge of how Twitter
  • 10:52
    is working just by following the API documentation.
  • 10:56
    Anybody that's got a smartphone probably
  • 10:58
    uses that phone on occasion to check the weather.
  • 11:00
    When you're checking the weather on your phone,
  • 11:02
    it's not like your phone just got a thermometer in it and it's saying,
  • 11:05
    look, it's about to start snowing.
  • 11:06
    I mean, the phone's not doing that.
  • 11:08
    There's some other external service that's doing that.
  • 11:11
    And your phone is connecting to that API and saying, hey,
  • 11:15
    I'm located in Minneapolis, Minnesota right now.
  • 11:18
    Could you give me some information about the weather here?
  • 11:20
    And that information comes back as structured data, which the phone
  • 11:23
    can then parse into a meaningful visual representation.
  • 11:29
    Netflix API.
  • 11:30
    If you've ever watched a video on your TV via Netflix,
  • 11:33
    you've interacted with the Netflix API.
  • 11:35
    It's all data from their web service with lots of different applications
  • 11:39
    accessing it.
  • 11:41
    Amazon is another common example of APIs in the wild.
  • 11:45
    They're more infrastructure based a lot of times.
  • 11:47
    There's an API for create me a new computer
  • 11:50
    that I can run a website on, when you spin up a box on EC2.
  • 11:54
    There's literally thousands of examples of APIs out there.
  • 11:58
    These are just a few of the big ones.
  • 12:00
    So all of that put together, why do you want an API?
  • 12:05
    Why should you be using the Services module?
  • 12:08
    What are you going to build here?
  • 12:09
    Some of the common reasons that I hear people talking about when they
  • 12:12
    say they want an API, obviously, the most common
  • 12:15
    is to provide access for mobile devices application,
  • 12:19
    native applications, internet-connected TVs,
  • 12:22
    fitbit trackers, whatever the case is, internet-connected devices then
  • 12:26
    want to make use of the data stored in your web application.
  • 12:29
    It all happens with an API.
  • 12:32
    Another really common one that I hear a lot
  • 12:33
    is digital asset management tools.
  • 12:36
    For example, a company like Martha Stewart,
  • 12:38
    who has hundreds or thousands of photos for all of their recipes,
  • 12:43
    might have a database system or a web service that
  • 12:46
    stores all of the photos with the tags and the date, and copyright
  • 12:51
    information, and so forth, and then delivers them via API
  • 12:55
    to all of the various clients.
  • 12:56
    But think about it, those clients could also
  • 12:58
    be things like an application or a website,
  • 13:02
    or even a print publication.
  • 13:04
    You could access an API to pull in the photo at the 300 DPI resolution
  • 13:09
    that you need for that particular print publication.
  • 13:12
    Or what about if our business has partners
  • 13:15
    that we'd like to share our information with?
  • 13:17
    There's another great reason to have an API.
  • 13:20
    Let me expose that data to you as structured data-- you know,
  • 13:25
    JSON or XML-- as an API that you can request information from,
  • 13:29
    I'll continue to be in charge of the data.
  • 13:31
    We don't have to do things like duplicate entry.
  • 13:33
    If I update the title of a video, I don't have to call you up and say,
  • 13:37
    can you update the title of this video,
  • 13:39
    because we decided to change our naming scheme.
  • 13:41
    There's all kinds of reasons to build APIs for your site.
  • 13:45
    And I'm sure that if you've gotten this far,
  • 13:47
    you've probably got a pretty good reason to build one yourself.
  • 13:50
    So throughout the rest of the series,
  • 13:52
    that's what we're going to be talking about doing.

What are Web Services?

Loading...

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.

Downloads: 
Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.