views:

475

answers:

3

I'm writing a specification for a RESTful API for a new internal web service. It's not hugely long and fairly simple, but even so, it's my first time using strict REST (as opposed to cheating for practical reasons - avoiding PUT and DELETE because they're a pain in PHP, and so on). I was wondering if there were any standard methods or best practices for documenting a REST interface? I want the rest of the team to understand it at a glance, and for anyone that wants to write a client to be able to do so without understanding the underlying code.

+1  A: 

In Roy's post here he states

A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state, or in defining extended relation names and/or hypertext-enabled mark-up for existing standard media types. Any effort spent describing what methods to use on what URIs of interest should be entirely defined within the scope of the processing rules for a media type (and, in most cases, already defined by existing media types).

Darrel Miller
I think that what you're saying is that the API should be self-documenting and logical enough not to need documentation. That's fair enough. However, somewhere along the line, I'm going to need to give people a list of allowed calls at the very least. I want to know if there's some sort of accepted structure for said list.
Samir Talwar
No. Media-types definitely need documentation. Here are a bunch of examples http://www.iana.org/assignments/media-types/application/What your documentation should not include is a list of URLs. That will just encourage client developers to hardcode those urls into their application.
Darrel Miller
Maybe I can make up a useful example. Imagine going to a foreign country where you do not speak the language and going to a library to look for some information. Imagine if you asked for help and the person explained to you the concept of a bookshelf, ISBN cataloging, that books contained pages and you read the book from front to back. All valid information but you already know that. You need someone to translate the content. On the web we know about Urls and hyperlinks, we know how to use HTTP verbs. We just need to understand the details of your content.
Darrel Miller
Gotcha. This was my gut feeling too: provide detailed explanations of what each call *does*, not what it *is*. I'm thinking there isn't a standard method for doing this, but hell, I guess winging it never hurt anybody (don't quote me on that). Thanks for your help.
Samir Talwar
+1  A: 

At my company, we've been very happy using WADL, Web Application Description Language. Wikipedia describes it as: "an XML-based file format that provides a machine-readable description of HTTP-based web applications". I find raw WADL easy to write, read, and understand, and it maps directly to RESTful concepts. The official project provides a simple spec, XSD and RELAX NG schemata, and Java tools.

A number of tools and resources exist for working with WADL, including:

  • wadl_stylesheets, XSLT stylesheets to create HTML documentation from WADL files
  • Restlet, a Java framework for building RESTful servers and clients, includes a WADL extension

A tip: try including human-readable documentation, such as descriptions, concepts, getting started, usage tips, etc, in the WADL document's doc element by including HTML elements, using the XHTML namespace. It can make a big difference!

Avi Flax
An ongoing discussion in Rest-discuss [1] shows that a number of REST advocates see little advantage to using WADL.[1] http://tech.groups.yahoo.com/group/rest-discuss/message/12680
Darrel Miller
I've looked into WADL, but it seems that it's more oriented towards machine interpretation rather than documentation. I don't need to tell the computer how to deal with the API - that only needs to be written once. I do need to tell people how to write clients, and so people are my primary audience.
Samir Talwar
You can also generate client side stubs with WADL, for strongly typed languages. E.g. see wadl2java
DSO
A: 

A good ReST documentation would mean documenting your media type and only your media type.

In a typical scenario, you'd produce a document like so:

The Acme Corp xml formats

Link discovery Links to various resources are described in a document that can be found by issuing a GET or HEAD request to the server on a bookmark URI (typically the root of the server, http://www.acme.org), and looking for an HTTP Link header.

Link: ;rel="http://rel.acme.org/services";type=application/vnd.acme.services+xml

where the rel part is the link relationship, and the xxx is the URI for which the relationship has been established.

Link relationships This document defines the following relationship names: http://rel.acme.org/services The link relationship describes the list of links that can be navigated. http://rel.acme.org/customers The link for which this relationship is used is the list of customers.

Media Types The application/vnd.acme.services+xml is a document with an xml serialization that describes a list of links an application may want to process.

The applcation/vnd.acme.customers+xml is a document with an xml serialization that describes customers.

Example doucments:

etc...

The point is to give a way to the developer to follow the links you define. First find the link to the index so they can get the list of things they can navigate to.

Once they discover that doucment, they discover that they can see a list of customers at a certain uri, and can do a GET against it.

If they find a customer of interest, they can follow the link defined in /customers/customer/@href and issue a GET to retrieve a represtnation of that customer.

From there, your media type could embed actions that are available to the user, using more links. You also have the additional option of issuing an OPTIONS request on the resource to know if you can allow deleting the resource, or a PUT if you can save the document back after modification.

So a good doucmentation doesnt ever:

  • give link
  • give interaction such as "you can issue POST on Customer with this media type and that will mean the move operation". The client should issue a POST against Custoemr only because your xml doucment has specified it that way.

The point of all this is to achieve maximum loose coupling between the client and the server. The client can be very smart in displaying and idscovering resources (showing forms and god knows what else), but is totally dumb as to what the actual workflow is: the server decides.

serialseb
OK, so I should mainly be using link discovery to document the API. How exactly do I construct such an XML document? Got any links?
Samir Talwar
Yes, you would document how to discover links and actions in your media type.As for how to do it, have a look at ATOM and HTML, that's pretty much all you need: a <link rel= href=> and some way to convey an action that you want to display in your client.
serialseb