API (Application Programming Interface)
A set of rules and definitions that allow different software applications to communicate with each other.
Example: A weather app fetches live weather data from a third-party weather service API.
Why Use APIs?
The image below illustrates the fundamental idea behind an API (Application Programming Interface) by showing how a system defines a contract that specifies how a user interacts with it.
System as a Contract Provider:
The system defines a contract that includes:
Browser and JS:
The lower part of the image shows how the Browser exposes various functionalities through Browser APIs to JavaScript (JS) running in the browser:
JavaScript interacts with browser functionalities (like timers, events, etc.) via these APIs.
Example:
setTimeout(fn, t)
is a Browser API that schedules a function (fn
) to run after t
milliseconds.
Here, the Browser defines the contract (the API) that JS follows to interact with features like timers (functions F1, F2, F3 in the diagram).
<aside> 💡
There are recommended practices for implementing APIs. While not following these practices won't break your code unless there are algorithmic issues, it may make your APIs harder for others to read and understand if they don't follow standard conventions.
</aside>
GET
request to https://api.example.com/users
.