APIs, also known as Application Programming Interfaces are the foundation of how computer systems interact with one another. In this blog post, we’re going to explore what an API is, how it works, why it’s beneficial, and finally an example by looking at the Twitter API.
Most blog posts jump into a technical definition and follow it up with a definition. I’m opinionated that its best to learn through an example. So lets explore how APIs are involved with an example we can all relate to.
If you’re just getting started in your programming career you’ve probably already heard the term API. Programmers talk about it all the time. Want to make a twitter bot? Use the Twitter API. Want to put a google map on your website? Use the google API. APIs are all around us, and in this post, I’m going to explain what they are, why their useful, how to use one, and what they look like in real life by checking out the twitter API
An API in Action
Let’s pretend you need to pay a bill on your bank’s website. You sign on to your account, enter the amount you want to pay, and click submit. Magically the banks somehow know that me, Daniel, paid my credit card card bill. But how do they actually know? I clicked a button on a website, sure, but how does that button translate into paying a bill?
The key lies in clicking that submit button and the series of events that took place immediately after. So lets pull the layers apart a little bit and get into this a bit deeper.
When you click that Submit button, what is actually happening is that information about you, the user, and the account you are acting on, is transmitted as an instruction to another computer on the banking system’s network. Now inside this instruction contains all this information about the bill you’re trying to pay, who you are, and the amount. And conveniently this machine on the other end received, understood, and submitted the transaction for processing. But how does this computer know how to interpret all this information? How does it even know the information your sending is about a bank account, let alone the fact that you’re trying to pay a bill? This mystery is solved by the API, also known as the Application Performing Interface.
What is an API?
The textbook definition of an API is as follows: An API is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow. That was a lot of words, lets go back to our example to understand further.
An API as a Contract
The banking system in this example knows how to interpret the passed information through an API that the system creates and exposes. You can think of an API as toy with different shaped holes in it, and you need to match the small shapes into the right holes. So in this example, the banking system might create a ‘payBill’ API, and when it is created, the programmer may say that this API will take a String called accountNumber, an integer called ‘amount’ , and an integer representing the destination account number called ‘receiver’. When this pay bill API is invoked, it returns the balance of the users account in an integer field called balance.
Now, other applications can look at the APIs that the banking system exposes, and decide to use them in any which way. In this example we saw the usage of a hypothetical payBill api, but there are a whole other number APIs that are created that both provide functionality, and provide information.
You can think of an API a contract. The creator defines the name of the API, what the API does, and the arguments it will take in, and the output that it will spit out. This pattern, of clearly defining our input and our input is what makes the API so special. It brings order to this madness on the internet where information is flying all over the place between systems. It forces developers to carefully think about what they want their system to do, and encode those behaviors into APIs that it will expose to clients.
This API can also define other behaviors. For example, what should this API do if a user tries to call payBill and provides an invalid accountId? Should it simply reject the request? Or should it create an account first and then pay the bill? APIs can also specify what they will do in certain scenarios through established agreements.
Other API Benefits
So to recap we learned that an API is in essence, a contract that defines the relationship between two systems in order to exchange information. I mentioned that they’ve also given structure to some of the madness, but what else do they do and what are some of the benefits?
- Well, firstly they provide a layer of abstraction between two systems. A caller, or client of the API does not need to know HOW the API is implemented, it just needs to know WHAT that API promises to do. It takes a certain input, performs a job, and produces a certain output. Its predictable and consistent. By combining APIs together with other APIs, we can build some really powerful systems. In fact, this is basically how modern technology has evolved so quickly, developers standing on each other’s shoulders and building onto of past work.
- Secondly, they allow users to retrieve information and submit instructions. Like we saw in the banking example, we performed an action by submitting an instruction to the payBill API and provided it some specific information. Similarly, there may be an API that exists called getBalance that provides information. So you see here that there’s a couple main types of APIs, one for access to a resource and one for performing an action.
Summary
In this video, we learned that an Application Programming Interface, or API, is a contract in which systems agree to in order to exchange information. We learned the APIs help give systems a semblance of organization in order to deliver functionality to users.