What is an API?

Published: Sunday, June 12, 2022
Updated: Monday, June 13, 2022

Greetings, friends! The world is filled with all kinds of APIs nowadays. Want to build an app related to Star Wars? You can make API calls to the Star Wars API. Want to make a Pokédex app? You can make API calls to the Pokémon API. Not sure if an API exists? You can search through tons of APIs on RapidAPI. There are so many APIs that exist, but what are they?

What is an API?

According to Wikipedia, an application programming interface (API) is a connection between computers or between computer programs. I like to think of an API as a tool created by developers that other developers can use.

We can build APIs to deliver data to developers who will consume them in their app. We can also build a software development kit (SDK) in the form of a library or module. If you've ever downloaded a module from npm, you've probably worked with an API. Code library authors tend to have documentation that describe how to use their code. They will design an interface that you should adhere to for the code to work properly. Examples include the methods you use in the browser such as Document.getElementById.

When we call Document.getElementById, we are calling a method on the Document interface. The Document interface is actually part of a Web API aka browser API. One or more developers worked on implementing the Document.getElementById method in the browser, so you could use JavaScript to grab an element on the page using an identifier (ID).

This method doesn't actually exist in the ECMAScript specification. That is to say, the Document.getElementById method isn't part of the core JavaScript specification. This is why you don't see some methods in the browser API in other environments that use JavaScript such as Node.js.

There is a group known as the Web Hypertext Application Technology Working Group (WHATWG) that is responsible for evolving HTML and Web APIs, including the Document interface. It's important to know which methods and interfaces are part of the ECMAScript standard and which are part of the WHATWG when deciding if you need a polyfill or not as mentioned in Part 4 of my Babel tutorial series.

Methods for Making API Calls

In JavaScript, there are two tools for making API calls natively (without the use of any external libraries).

  1. XMLHttpRequest (XHR) API
  2. Fetch API
warning
The Fetch API does not work in Internet Explorer.

In Node.js, there are two tools for making API calls natively.

  1. HTTP Module
  2. Fetch API
warning
The Fetch API only works in version 18 and higher of Node.js.

There are also many external libraries that abstract out complexities and make it simpler to work with APIs. However, they internally will make use of the native tools mentioned above. One popular library that works with both the browser and Node.js is Axios.

Conclusion

In the next set of tutorials, I will dive deeper into how we can make API calls using JavaScript in the browser and Node.js, so look forward to them! 🙂