“What’s an API?” is one of those questions that is really powerful, important, and interesting, but hadn’t crossed my mind in a while. Programmers talk about APIs all the time, and increasingly it’s a term that non-programmers will have heard at some point. But what exactly one is is a rich and interesting question.
Application Programming Interface: Breaking it Down
There are three words represented by this acronym, and each is quite useful in understanding the whole acronym. Let’s break down the phrase and example each word in it:
An API is a determined protocol for interacting with a “black boxed” application you don’t control
- Application — in the world of software, an “application” is a bundle of functionality. It’s a unit that we can “black box” — use, but not need to understand — which will do useful work for us. WordPress is an application, Microsoft Word is an application, etc. Very few people in the world can make either of those things, but many many many people do productive work with them.
- Programming — We program systems by assembling things from more rudimentary tools. WordPress and Microsoft Word were both built by using more simplistic constructs called programming languages — which are themselves ways of creating “machine language”, which computers actually speak. In thinking about the term “API”, we’ll want to program — work with — the “black box” applications that are provided for us by others.
- Interface — In the world of application programming an interface exists at the edge. You can actually think of a an interface a bit like one of those shape-sorting does-it-fit-through-the-hole toys that are given to toddlers. To interact with the black box of a application by programming, you need to offer the right kind of input, which will trigger the right type of output or behavior. If you try to feed the wrong input in, you’ll get nothing from the API.
So to tie it together, an API is a determined protocol for interacting with a “black boxed” application you don’t control. Many many kinds of software have many different kinds of APIs, but in all cases what we mean when we talk about an API is the process by which my software can interact with and extract valuable information from another service.
You understand this definition of an API deeply enough and you quickly realize that almost everything about computers can be understood as an API. Programming languages rely on the black box of the lower-level language to make them work. As I made reference to above, your CPU understands “machine language”, and yet we can use languages like PHP, Python, Haskell, or Java to do useful work on them. This relies on abstractions that underpin these languages. PHP, for example, is written in the C programming language, which compiles — is translated — into “machine code”.
Additionally, within those higher-level languages, you rely on a set of functions you didn’t write. Anything you do in a language is built on top of functions, which can be thought of as an API. We can make use of the
sqrt() function that we didn’t write so long as we give it the right form of input.
Then applications can come along and build on top of this basic layer of functions in the specified language that is available. I just wrote a little tutorial on WPShout about the
switch_theme() function that WordPress makes available. The way I interact and work with this is decided by the WordPress project, and I can just treat WordPress’s core as a black box and then use it.
Finally, web services, like Facebook expose APIs to other things on the web. HTML itself is a form of an API, but more typically you talk to something like Facebook talk through a more formal method with a clearer structure for the data you want. (As anyone who has tried can attest, HTML is a very complex and fragile language to programmatically extract data from.) Because of this, most web services exchange data in a clearer and simpler format, XML — extensible markup language, which looks a lot like HTML — was the default in the wild for about a decade, but it’s quickly becoming supplanted by the super-simple JSON format.
A joke and the future of APIs
A few months ago a tweet from @markjaquith stuck in my head:
BusinessWeek: “API essentially means ‘speaks (and reads) JSON’”
Um, well, that’s not exac— no, actually, I’ll allow it!
As I hope you understand now, and as Mark alludes to, that’s both really wrong and spot-on. On the web, and generally in the cross-platform world of apps, and webpages, and web services you can just understand that Twitter or a different service you hear the term “API” used around are sending and receiving JSON (hopefully in a RESTful way, but that’s a whole different topic). But it’s also a vast oversimplification of the rich and complex concept on an Application Programming Interface.
In computing almost everything is an API. An API is a useful abstraction that makes it easier to write software that’s useful to people. If everyone who made computers do something had to traverse the distance all the way to machine code, we’d have almost no cool technology. But we’ve built a series of abstraction, worked with through APIs, that have made an idea like “software is eating the world” worth considering. All the things we rely on computers for, APIs make possible.
Image Credits: simplyla