The Principle of Audience states that an API must be designed with its audience in mind. The audience of an API is the people that will be writing code against it.
All too commonly, APIs are designed primarily as a way to expose some underlying functionality at some level of abstraction. This style of API design results in APIs that are either too general or too large for easy use - they’re functionality-oriented instead of audience-oriented.
Let’s say that you’re writing an API around a large, complex piece of software. You could ask one of two questions in order to drive the API design:
1) What functional areas of this software do I want to expose?
2) What kinds of users wish to interact with this software, and what are they trying to get done with it?
Driving an API with the first question can result in an API that is complete but not easy to use and learn. Driving with the second question results in an API that is scenario-focused and was written with purpose and intention. It all boils down to one fact: people are writing to an API because they’re trying to get some work done and then move on.
How Audience Drives API Design
Once you’ve identified your audience for your API, you can start to make audience-focused decisions to drive the design of the API.
One issue to consider is whether the API should be high-level or low-level. A low-level API may do little more than wrap existing code and provide a consistent interface to it. This low-level API usually has an interface style that’s identical to the code being wrapped (for example, procedural code wrapped by a procedural API), and it often maps one-to-one with concepts in the underlying code. The main purpose of such a low-level API is to provide a level of indirection so that future changes can be made to the underlying code without breaking existing clients of the API.
A higher-level API may provide a totally different way of working with the existing functionality. A high-level API may have a completely different interface style than the wrapped code (like an object-oriented interface to a procedural library). It may not map one-to-one with concepts in the wrapped code: the API may invent concepts of its own and expose them to clients, and it may hide or suppress concepts in the underlying code that clients of the API shouldn’t be concerned with.
The high-level or low-level decision comes down to audience. If your audience is already familiar with the software the API is being written for, a low-level API probably makes sense. If the audience is unfamiliar with the existing software, a high-level API provides a needed conceptual level of indirection.
Another audience issue to consider is whether an API is going to be internal (to an organization) or external. An external API probably needs to be versioned, and definitely needs different documentation than an internal API. People using an internal API probably have access to the authors of the API, and often can make changes to the API to fix bugs or add additional functionality.
Audience Considerations
When you write an audience-oriented API, there are some considerations to keep in mind. Be sure to provide starting points. Think about the API from the perspective of a user who is coming to it for the first time. Where should they start? There should be jumping-off points both in terms of the documentation and the API itself. The documentation should at least have an index, a 5-minute tutorial section, and a FAQ. The FAQ should consist of real questions that are frequently asked - it’s going to be hard to come up with this list until the API has been used by people other than its authors.
As an author of an API, you will find it difficult to predict where these jumping-off points should be, or even if there are enough of them. The best way is to team up with another developer you know who is completely new to the API, and have them attempt to write a program against it. Observe where they start, and at what points they struggle with the API. Getting another person to sit down with your API and critique it is going to be much more valuable than trying to predict where the pain points are by guessing.
A great way to provide jumping-off points is a suite of tests that ship with your API and exercise all of its functionality. These tests could be very simple (they probably aren’t unit tests), just calling the API and verifying the result, perhaps mocking up the back-end to some degree. Developers using your API can consult the tests to see how various things are done.
Ensure that your API ships with as many development aids as possible. Understand what IDEs and environments your audience will be using to write against your API and deploy code in. Make sure that your API ships with whatever help and documentation is appropriate for these environments. For example, if your audience is going to be using an IDE that offers code completion or contextual code hints, make sure that your API ships with the necessary hooks to integrate into this IDE and provide that kind of support. The whole idea is to make it as easy as possible for your audience to code against your API.
Multiple APIs
So what happens when you’ve thought about your audience, and you realize that there are multiple audiences each with different needs? Instead of trying to build a single, one-size-fits-all API that covers all the needs, consider writing multiple, distinct APIs for your product.
Let’s say that you’re writing an API for a some software that manages a large amount of data and performs interesting computations on it. One audience of the API might be people who want to write functionally equivalent client applications (perhaps on unsupported platforms), so you would want to write an API that exposes most of your core functionality and concepts. However, another audience may simply want to get data in or out of your data store. For this audience, an import / export oriented API would make a lot more sense.
A lot of business integration is about just moving data out of one system and into another. Users of an API for this reason don’t care about all the features and concepts of your application - they simply want a clean API to either get their data out of your system, or put their data into your system. Given that business data far outlasts the applications that process it, every application that manages customer data should have an API for importing and exporting that data in as neutral a form as possible.
Web Services
The Principle of Audience is one reason I believe a currently popular approach to web services won’t work. I call this concept the “build it and they will come” approach to web services. The idea being that a company will invest lots of time and money into writing web services that expose their data or services. This is done without a clear customer (audience) in mind, with the hope of it being useful for future business integration. I’d be willing to bet that most of the time these web services are only used internally in a company (probably with both endpoints on the same platform), and are either scrapped or completely revised if integration with external entities is ever needed.
The problem with “build it and they will come” web services is that no one knows exactly who “they” are. These web service projects are often written either because web services are popular right now, or because having web service support becomes a marketing bullet point. Without a clear audience in mind, these web services become at best toys to try out new technology, and at worst a maintenance time sink with little business value.
A final note - one counter-point to an audience-oriented API might be phrased: Isn’t the whole point of an API that we can write a library and then people will use it in all sorts of interesting ways we’ve never thought of? My response is that an API will only be useful for an unforeseen purpose by coincidence. You can certainly take steps to make the API flexible and extendable, but in the end the best APIs are those that are designed ahead of time for their audience.