Software • APIs • 6 min read

What Is an API?

Summary

An API (Application Programming Interface) is a defined way for one program to ask another program to do something. It lists what you can ask for, how to ask, and what you get back, without showing how the work is done inside. Web APIs do this over the internet. Library and operating system APIs do it inside a single computer.

An API, short for Application Programming Interface, is a defined way for one piece of software to use the features of another. It states what requests are allowed, what format they must follow, and what response comes back.

APIs exist because programs rarely work alone. A weather app does not measure the weather. A shopping site does not process card payments itself. These programs ask other software to do that work. The API is the agreed method for asking.

Each word in the name has a meaning:

  • Application: any software, such as an app, a website backend, or a library.
  • Programming: it is used by programs and programmers, not by end users.
  • Interface: it is the point where two systems meet. You use the interface and never see the inside.

How It Works

The Core Idea: A Contract

An API is a contract. The provider promises that if you send a correctly formed request, it will return a specific kind of result. The caller does not need to know how the provider works inside.

A restaurant is a useful comparison. You read the menu (the API’s list of allowed requests) and give your order to a waiter (the interface). The kitchen (the provider’s internal code) prepares the food. You never enter the kitchen, and the kitchen can change its recipes without changing the menu.

This hiding of internal details is called abstraction. It lets the provider improve its code without breaking the programs that depend on it, as long as the contract stays the same.

APIs Exist at Many Levels

The word “API” is used for several different things:

  • Library APIs: the functions a code library offers. When you call math.sqrt(16) in Python, you use the math module’s API.
  • Operating system APIs: the functions an operating system offers for tasks such as opening files, creating threads, or drawing windows.
  • Web APIs: services you reach over a network, usually with HTTP. This is what most people mean today.

All three work on the same principle: a defined request goes in and a defined result comes out.

How a Web API Call Works

A web API call is a request followed by a response.

  1. The client (your program) sends an HTTP request to a URL called an endpoint.
  2. The server checks the request, does the work, and prepares a result.
  3. The server sends back an HTTP response. It contains a status code and usually data.

A request has four main parts:

  • Method: the kind of action. GET reads data, POST creates data, PUT or PATCH updates data, and DELETE removes data.
  • URL: the endpoint, such as https://api.example.com/v1/weather.
  • Headers: extra information, such as the data format or login credentials.
  • Body: data sent to the server. It is used mainly with POST, PUT, and PATCH.

Here is a request for the weather in London:

GET /v1/weather?city=London HTTP/1.1
Host: api.example.com
Accept: application/json

The server replies with a status code and data:

{
  "city": "London",
  "temperature_c": 14,
  "condition": "Cloudy"
}

Data Formats

Web APIs send data in a text format that both sides understand. JSON is the most common because it is simple and readable. XML is older and still used in some systems.

Status Codes

The status code tells the client whether the request worked.

CodeMeaning
200 OKThe request succeeded.
201 CreatedA new resource was created.
400 Bad RequestThe request was malformed.
401 UnauthorizedThe caller did not prove who they are.
403 ForbiddenThe caller is known but not allowed to do this.
404 Not FoundThe requested resource does not exist.
429 Too Many RequestsThe caller sent too many requests too quickly.
500 Internal Server ErrorThe server failed while handling a valid request.

Real-World Application

Here is a small JavaScript program that calls a weather API. The URL is an example and not a real service.

async function getWeather(city) {
  const url = `https://api.example.com/v1/weather?city=${encodeURIComponent(city)}`;

  const response = await fetch(url, {
    headers: { Accept: "application/json" },
  });

  if (!response.ok) {
    throw new Error(`Request failed with status ${response.status}`);
  }

  return response.json();
}

getWeather("London").then((data) => {
  console.log(`${data.city}: ${data.temperature_c}°C, ${data.condition}`);
});

The program does these things:

  • It builds the endpoint URL and sends a GET request.
  • It checks the status code before using the data.
  • It reads the JSON response and prints the values.

The program knows nothing about how the weather service gets its data. It only needs to follow the contract.

Common uses of web APIs include:

  • Payments: a store sends the order total to a payment provider and receives approval or rejection.
  • Maps: an app sends an address and receives coordinates.
  • Login: a site asks an identity provider to confirm who a user is.
  • Internal services: large systems split into smaller services that talk to each other through APIs.

Authentication

Most APIs need to know who is calling. The usual methods are:

  • API keys: a secret string sent with each request to identify the caller.
  • Tokens: a temporary credential issued after login, often sent in an Authorization header.
  • OAuth: a standard that lets a user give an app limited access to their account on another service without sharing their password.

[!WARNING] Never put secret API keys in code that runs in a user’s browser or in a public code repository. Anyone can read them and use them. Keep secrets on the server side.

Trade-offs and Alternatives

Why Use APIs

  • Reuse: a feature built once can serve many programs.
  • Separation: teams can work on different parts without reading each other’s code.
  • Replaceability: the provider can change its internals without breaking callers, as long as the contract holds.

What They Cost

  • Network delay: a call across a network is much slower than a call inside the same program.
  • Failure: the network or the server can fail. Callers must handle errors, timeouts, and retries.
  • Dependency: if the provider changes or removes a feature, your program can break.
  • Limits: providers often cap how many requests you can make in a given time.

Network cost shows up quickly in design. Suppose you need data for nn users. If you make one request per user, you send nn requests. If the API offers a bulk endpoint, you send one. This is the difference between O(n)O(n) requests and O(1)O(1) requests, and it often decides how fast a program feels.

[!TIP] Fetch data in bulk where the API allows it, and cache results you reuse. Fewer requests means faster programs

Related articles in Software

Image preview

Fit