What is an API?

In short
An API, or Application Programming Interface, is a set of rules and features that lets one piece of software use another without a human clicking through a screen. It acts like a published contract that says what you can ask for and what you will get back. Developers use APIs every day to reuse work that others have already built.
What an API is
An API, short for Application Programming Interface, is a set of features and rules that a program offers to other programs. A person uses a normal user interface with buttons and menus. An API is the same kind of door, but it is built for code instead of people.
Think of an API as a contract. The program that offers it says, in effect, "If you send me a request in this form, I will do this job and return that result." The other side agrees to follow those rules. Neither side needs to know how the other is built inside.
In web development, the term covers a wide range of things. An API can be a set of functions in a browser, a way to read data from the computer a page runs on, or a set of web addresses (URLs) offered by a service on another server. The documentation describes all of these as code features a developer can call from an app.
How it works
An API lists the actions that are allowed. Each action usually has a name, the inputs it needs, and the output it returns. The documentation tells you what those are, so you can use the API without reading the code behind it.
Some examples from the web platform show the idea well. The getUserMedia API can capture audio and video from a user's webcam once the user gives permission. Your app then receives that media and can record it, send it to someone in a call, or grab a still image from it. The Geolocation API can read location information from sources on the device, such as GPS. You can then combine that location with a map service to show the user where they are and what is nearby. The Web Animations API lets you move or rotate elements on a page from code.
In each case, your code does not need to know how the camera hardware works or how the browser computes a position. It calls the API, and the API handles the details.
When to use it
Use an API when someone else has already solved a problem you do not want to solve yourself. Reading a camera, finding a location, sending a payment, or translating text are all tasks that are better handled by a tested interface than by custom code.
APIs also let separate parts of a system talk to each other. A mobile app might call a server API to fetch a user's orders. A dashboard might call a weather API to show forecasts. Splitting work this way lets each part change without breaking the others, as long as the contract stays the same.
Before you rely on an API, check whether it is available in the environment you target. Browser APIs may differ between browsers, and some need the user's permission before they work.
A simple example
Here is a small example of calling a web API with JavaScript. The browser's fetch function sends a request to a URL and returns the response. The URL here is a placeholder, so replace it with a real address from the service you are using.
async function loadUser() {
const response = await fetch("https://api.example.com/users/42");
if (!response.ok) {
throw new Error("Request failed with status " + response.status);
}
const user = await response.json();
console.log(user.name);
}
loadUser();
The code sends a request, checks the status, reads the data, and uses it. You did not need to know how the server stores users. You only needed to follow the rules the API published.
If you want to understand how services like this are protected when apps act on a user's behalf, our guide on how OAuth 2.0 works explains one common approach.
Common mistakes
Beginners often treat an API as if it were always free or always stable. Many APIs have usage limits, require a key, or change over time. Read the documentation for limits and version notes before you build on it.
Another common mistake is assuming a call will return right away. Network requests take time and can fail, so your code should handle slow or failed responses. Checking the response status, as the example does, is a good habit.
Finally, do not expose secret keys in front-end code, since anyone can read them in the browser. Keep keys on a server when the API requires them.
Alternatives and related ideas
An API is a broad idea, and several related terms are worth knowing. A library is code you import into your project and run yourself. A web service is a server that answers requests over the network, and many web services publish an API. A user interface is what people see and click. An API is what programs use.
When you work with data that an API returns, you may later store it in a database. Our explainer on what a database index is covers one way to make that data faster to find.
FAQ
Is an API the same as a library?
No. A library is code you add to your project and run inside it. An API is a set of rules for interacting with a program, which may live somewhere else, such as on a server or inside a browser.
Do I need to know how an API is built to use it?
Usually no. The documentation describes the names, inputs and outputs you need. You call the API and use the results without looking at its internal code.
What is the difference between a web API and a browser API?
A browser API is a feature built into the browser, such as access to a webcam. A web API is usually a service on another server that your app calls over the network, often by URL.
Why do some APIs need a key?
A key lets the provider identify who is making requests, so it can limit usage or control access. Keep keys private and do not place them in code that users can read.
Sources
This page is based on the official documentation below. Read it for full detail.
API — official documentation ↗