APIs are a huge part of modern web development. The term gets thrown around a lot, but what exactly is an API?
OK – let’s start with the acronym. API stands for Application Programming Interface. In short, it’s kind of like a babel fish, or translator, for code.
Every large website has some way of storing their data. In many cases, that’s done with a database. Every website’s database is different, depending on who initially set it up, what sort of IT policies are in place, as well as a myriad of other factors. Regardless of how they’re setup, there are also many different types of databases! Some popular ones include MySQL, Microsoft SQL Server, MariaDB, MongoDB and Postgres.
A web developer is unlikely to be able to look at the database of an unfamiliar website and immediately know their way around.
What APIs do is pull data from those different databases and present it in a way that other web applications can understand. This makes the passing of data between applications a lot easier. For companies that provide their data via APIs, it means they’re doing it in a standard way that other websites can understand.
Using APIs to book flights
Let’s imagine you’re searching for a flight on a travel site like Expedia or Skyscanner. When you type in your origin, destination, dates and other preferences, the site communicates with APIs from the airline websites to give you the information you require.
Skyscanner does not, could not and will not ever have access to Virgin Australia’s database. Even if it did, it wouldn’t know where to start looking. BUT, Virgin Australia has an API that is written to a standard, so Skyscanner can access that, and pull data from it. Not only can Skyscanner access it, but so can Expedia, Kayak and anyone else who applies for access.
Using APIs to manage your finances
Another example is online accounting software. Using something like Xero or Freshbooks, you can see your transaction history from your businesses bank account. This information is refreshed regularly via your bank’s API.
In the case of banking, stock trading, or anything else financial, it’s pretty clear why they wouldn’t want external companies poking around in their data. By providing an API, the financial institution is not only proving data in a standardised format, but they’re also in total control of what data is available via that API.
If you can’t access your bank information via your accounting software, it’s likely that your bank does not offer API access (or you need new accounting software).
Explaining an API by ordering food
I’m always a sucker for a good analogy, so try this on for size:
Now before we start, I want to be clear that I didn’t come up with this analogy. I’ve heard variations of it multiple times. I have no idea who originally came up with it. If you do, let me know.
This is my take on why an API is like a restaurant:
Imagine we’re at a restaurant as a customer. There are a few things that are standardised to make the process of ordering food much the same, regardless of the establishment we’re at:
- We are not allowed to enter the kitchen
- We need a menu to know what is available
- We place our order with a person (a waiter) or via an App (eg: UberEats)
- The waiter or app gives our order to the kitchen
- The kitchen provides what we have ordered
- The waiter /delivery person can only deliver what the kitchen can provide
Are you still with me?
I’m guessing your next question is “how does that relate to an API?”
👩🍳 The kitchen is like our database. Just like every restaurant’s kitchen is totally different, so is the database of every website. For security and safety, no customers are ever allowed in here.
💁♂️ The waiter is our API. They are an intermediary that knows how to serve data from the database without disrupting its functioning. Just like a waiter can serve food from the kitchen without getting in the way of the cook, because they know how their kitchen is set up.
🧍 The customer (us) is an external system that wants to get data (or a pizza).
📜 The menu is like our API documentation. It’s a standardised way of saying what information the website (or restaurant) offers. Every restaurant is completely different, but they all have menus. It is the standard way to order food, regardless of what or how that food is prepared or served.
🍕 And finally, our food order is like an API call, where we can request a piece of data.
Whether it be fish n chips, Thai or pizza, each restaurant will have an entirely different kitchen setup. BUT, the role of the menu and the waiter is exactly the same in all three.
As a customer, you can walk into any restaurant and know how to order food because the concept of a menu is the standardised way to do so. And you know that to get food you need to do so through the waiter because it is the standardised way to do so.
Most importantly – you never need to even look at the kitchen to try and work out how to get your food from it yourself.
Getting data from an API is much simpler because it is the standardised way to do so.
Righto. I’m hungry.