Restful API Design Sample
Restful API Design Sample
Restful API Design Sample
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
– Phil Karlton¹
Fundamentally, API design is hard. It combines two of the most challenging things in computer
science and asks you to accomplish them in a distributed environment at scale. You might as well
try it with your eyes closed, too.
In this book, we’ll discuss the What, Why, When, and How behind RESTful APIs. We’ll begin
the story by describing what an API is. Then we’ll discuss business and technical scenarios that
might make an API make sense for your organization. Then we’ll talk about how the core concepts
fit together and the design principles that emerge. Throughout, we’ll share practical real-world
examples of all of the above. While many are success stories which can be emulated, many will
be cautionary tales of strategies and implementations to avoid. Finally, while we’ll touch on and
explore the various religious aspects of API design, the goal of this book is to remain relatively
neutral and give the different sides equal time.
While this is a book, we hope it also becomes a conversation. When you find something
interesting, let us know. If you think we’re wrong, say so. If you think we’re right, tell your friends.
If you want us to teach your team more and deeper concepts, please let us know.
D. Keith Casey, Jr. keith@caseysoftware.com
James Higginbotham james@launchany.com
February 2014
¹http://martinfowler.com/bliki/TwoHardThings.html
Chapter 1 - APIs: An Introduction
What is an API
If you read the tech press, everyone knows they need an API but most aren’t really sure what it
is. They treat it as another checkbox like “Web 2.0” was a few years ago or a mobile app was most
recently. In fact, there’s an entire “API-first” movement in development circles that most people
don’t understand or even realize why. There are a variety of reasons for this situation with most
reasons boiling down to a simple misunderstanding on what an API is and its purpose. Let’s get the
obligatory Wikipedia definition out of the way:
An API doesn’t have to be RESTful. It doesn’t have to use SOAP. It doesn’t have to use JSON
or XML or OAuth or be built in Node or Rails or Python. It doesn’t have to have pretty URLs
or even necessarily use URLs at all. It doesn’t have to make use of hypermedia concepts. The only
requirement for an API to be an API is that it allows one program or software component to interact
with another in a repeatable way.
It’s almost disappointing, isn’t it?
All of that said, an API can use all of those things listed above and definitely should use a
number of them. When we speak about APIs in reference to web technology, it usually means a
way of communicating over HTTP in a predictable and repeatable manner using payloads of XML
or JSON to change the state of data within the system. More advanced systems use hypermedia
and OAuth while less advanced or legacy APIs may use hard coded URLs and basic username and
password-based authentication.
If that paragraph means nothing to you, don’t worry about it. We’ll get into it later. Many of the
terms above are powerful concepts that help you build APIs in clean and predictable ways while
also making it easy for your customers and users to adopt them. Because isn’t that what matters?
There are two types of companies that have APIs. First are the type of companies that are
primarily or entirely APIs. In this category, we find companies like SendGrid, Twilio, FullContact,
and a host of others. In their case, the API is their business so it’s absolutely necessary that their APIs
are easy to use and quick to get started. The other category is businesses who have an API as one of
their products. In this group we find Walgreens, Best Buy, FedEx, and many other traditional offline
companies looking to expand their reach and presence online. It’s worthwhile to remember that in
many of these cases, they derive no direct revenue from API usage, which affect their strategy and
implementation in a variety of ways.
²http://en.wikipedia.org/wiki/Application_programming_interface
Chapter 1 - APIs: An Introduction 3
At the end of the day, you must understand which category your organization will occupy.
If usage of your API is directly connected to revenue, the goals are plain and simple: get people
started quickly, make sure they keep using it, and give them reason to embed it throughout their
organization. If an API is a checkbox that doesn’t serve a business or technical case, it is doomed to
failure. Luckily there are numerous business and technical cases your API could satisfy, regardless
of your industry or position in the market.
Business Cases
Mobile Strategy
The first business reason for an API is a mobile or mobile-first strategy. In most organizations, there
are neither the time nor the resources to build tools for every single device. An API can ensure that
additional - either less important or even unexpected - devices are addressed. In addition, it may
parallelize the effort to create a presence much faster or differently than originally planned.
Millions of people love movies via Netflix, making this API an opportunity for all kinds of
developers to add well-known value to any other application.
The company says the API will allow access to data for 100,000 movie and TV episode titles
on DVD as well as Netflix account access on a user’s behalf.
³http://en.wikipedia.org/wiki/Netflix#History
⁴http://readwrite.com/2008/09/30/netflix_api_launches_tomorrow
Chapter 1 - APIs: An Introduction 4
As a result, there were Netflix applications for most common devices within weeks or months.
These initial applications provided customers with the ability to view and manipulate their queue
whether they were online or offline. More importantly, it began to make Netflix a tiny - yet constant -
part of everyday entertainment consumption. By the time Blockbuster entered the streaming market
and later when they launched an API, Netflix was already the defacto standard. By that point, if
a device had a full color screen, no matter its manufacturer, size, or profile, there was a Netflix
application available that could enable watching a movie in just minutes.
Drive Usage
The next business reason for an API is to drive usage of a platform. Notice that this isn’t necessarily
paid usage but usage in general. Why is this scenario useful? By focusing on fast and simple usage
of a platform, you can drive engagement and grow the user base even more.
Defensive
The third business reason for an API is purely defensive. This is somewhat parsing words from the
previous two scenarios but is distinct in a number of ways: when you’re an established player in
the market and competition disrupts your business, it can be a challenge to use your positioning,
information, and infrastructure to make your existing business lines more stable. An API strategy
may be able to launch you into new business lines and growth that was previously unrecognized.
The big box retailers have suffered the brunt of this sort of disruption. They have massive stores
with massive inventories where they suffer from the “showrooming” problem. Potential customers
come in, browse the merchandise, and once they find the item, they use a site like Amazon to find a
⁵http://mashable.com/2011/03/05/sxsw-launches/
Chapter 1 - APIs: An Introduction 5
better price and place the order. The customer walks out without the product but saved money and
the retailer missed a potential sale.
Partner Connectivity
The final business reason is to improve connectivity with partners and suppliers. In the past, many
organizations exchanged spreadsheets via email to update systems such as inventory management.
If they were more advanced, the spreadsheets were uploaded via FTP and parsed automatically.
Either way, these methods were error-prone and often fragile when changes would occur within
either system. When importing a spreadsheet, a missing or new field, or even a misplaced comma,
could cost hours of effort and delay expensive processes. Fundamentally, it was broken.
Technical Cases
Abstraction of Complexity
Powerful and important systems are almost always complex to use and integrate, often out of
necessity. In the earliest days of the web, it took knowledge of the C programming language and
⁶https://bbyopen.com/developer
⁷https://www.internetretailer.com/2013/11/19/best-buys-online-sales-increase-151-q3
Chapter 1 - APIs: An Introduction 6
detailed understanding of SMTP in order to send an email. As these systems have become more
important and widespread, they have become easier and less complex to use and integrate into our
systems. This ease and simplicity comes from APIs.
Simplifying Interfaces
Despite numerous technical standards and long-established players, some systems are still terribly
complex to interact with and use in general. One of the prime examples is your email inbox. Despite
only two standards - POP3 and IMAP - there are so many implementations and variations of those
implementations that what should be a handful of lines of simple code ends up being a deceptively
complex implementation handling dozens of edge cases and unique scenarios. Building yet another
email interface is a practice in futility likely to cause unforeseen schedule delays and unnecessary
complexity. It’s simply not worth the time or energy.
Metered Usage
Many services lend themselves to simple monthly or flat rate pricing. For example, sending email
has a marginal cost that is effectively zero so sending the hundred and first email costs nothing
more than the hundredth. Alternatively, if we consider a resource such as CPU or processing time,
the costs of running a server for two hours is effectively double the costs for one hour. In this case,
a consumption-based or metered pricing model makes more sense. This is also known as “utility
pricing” to represent that you are only billed for the portion of a resource used.
Interested? Visit our publisher’s page to purchase the latest edition of the book and save 10%
today by clicking the following link: http://leanpub.com/restful-api-design/c/friends-save-10⁹
⁹http://leanpub.com/restful-api-design/c/friends-save-10
Ready to learn more? 9
¹⁰http://bit.ly/api-dev-weekly-obs