API's the Great Stereo of the Web?

API’s The Great Stereo Of The Web

What’s the first thing you do when you hop in your car? Well if you’re anything like me you usually grab the AUX cord, find your phone and plug that sucker right in to listen to some tunes. As you turn the knobs left and right on the stereo, electricity starts pulsing through the wires changing the amplitude in the speakers to deliver that crisp but clear audio of Justin Bieber’s Baby.

When you touch the knobs on your car’s stereo what you’re really doing is interacting with an interface. The knobs are the interface to the volume on the stereo. They provide a unit of functionality to you the “user” or “client”.

APIs or Application Programming Interfaces are no different, they provide a unit of functionality to the underlying “service.” APIs are great because they abstract away the internals of our programs and allow us to expose functionality. Just like the knobs expose the volume up or down functionality from the stereo. Think of how much of a pain it would be everytime you hopped in your car you had to physically manipulate the wires in order to change the sound!

This brings us to why APIs are so important. The real reason is just like ice-cream, software is much, much better when shared. APIs allow us to share functionality with the world, through an “interface”.

Today many APIs expose this functionality via the web and the API. The internet is the “interface” of the APIs. More concretely we use URLs and URIs as the interface to specify specific units of functionality. If you’re unfamiliar with a URL it’s the thing you see in the bar on top of your screen, which is the name of the website.

You can think of this like an exact address for your house. A URI is a more specific address or piece of your address for example maybe your house or apartment, the state, and so on. It’s a piece of your address not the whole thing.

To explain this let’s look at a real API “endpoint” for a more concrete example, this below is the full URL for the Official Carbon Intensity API for Great Britain:

https://api.carbonintensity.org.uk/intensity

We identify the URI as the parts after the first slash:

/intensity

The URI is like the state, city, zip, street, and house number, whereas everything before the first slash is like the country where it’s from. Technically the part before the slash is called a ”hostname” but we won’t get into that for now, but if you’re curious I suggest you pause and look up what a hostname is!

If you copy the full URL above: https://api.carbonintensity.org.uk/intensity into your browser bar you’ll get some funny squiggly lines back with words something like:

{
  "data": [
    {
      "from": "2020-06-02T00:30Z",
      "to": "2020-06-02T01:00Z",
      "intensity": {
        "forecast": 201,
        "actual": 201,
        "index": "moderate"
      }
    }
  ]
}

Well congratulations you’ve just interacted, knowingly, with your first API! Next we’ll explore what these squiggly lines are, how to interact with more APIs, and the tools software engineers use to talk to APIs outside the browser. Stay tuned!

Related Posts