Backend / What an API is

What an API is

A weather app asks a weather service for the temperature in Oslo. Seven steps show the contract between the two programs: request, response, endpoints and keys.

Beginner7 steps~6 min
App — the program that asks
Service — the program that holds the data
API — the only way in, one opening per endpoint
// THE SHORT VERSION

In 30 seconds

An API (application programming interface) is an agreed way for one program to ask another for something. It lists what you may ask for, what you must send, and what you get back.

The asking program sends a request; the other returns a response. On the web, each kind of request has its own address, called an endpoint.

The caller never sees how the answer is produced, so the inside can change freely. Many web APIs also require a key that identifies the calling app.

What an API is, step by step

STEPS01 / 07

The weather API here is invented. Real APIs differ in detail: the header that carries the key has no single standard name, and some refuse a missing key with 403 instead of 401.

STEP 01 / 07 Two programs, one question
THE MATH
app needs: temp in Oslo service has: the data
  1. Step 1: Two programs, one question. A weather app wants to show the temperature in Oslo. The measurements live inside another program, a weather service run by someone else. The app cannot open that program and look around, so the two need an agreed way to talk.
  2. Step 2: The API is a contract. The service publishes an API, short for application programming interface: a precise description of what other programs may ask for, what they must send, and what they will get back. It is a contract. The service promises to honour it; the app only has to follow it.
  3. Step 3: Request and response. The app sends a request shaped exactly as the contract says. The service does its work and sends back a response: a status (200 means it worked) and the data, here as JSON, a plain-text format that programs can read easily. Every use of an API is this pair: one request, one response.
  4. Step 4: Endpoints. A web API usually offers several things. Each one has its own address, called an endpoint: /weather for the conditions now, /forecast for the coming days, /alerts for storm warnings. The app picks the endpoint that matches its question and adds the details, such as the city, as parameters.
  5. Step 5: The inside stays hidden. The app sees only the contract, never how an answer is produced. So the service can replace its database, add a cache or be rewritten from scratch, and the app keeps working without a single change, as long as the same request still gets the same kind of response.
  6. Step 6: Library APIs and web APIs. Not every API crosses a network. A library is code written by someone else that runs inside your own program; its API is the list of functions you may call. A web API belongs to a program on another computer, reached with HTTP requests. Both are the same idea: a public contract in front of hidden details.
  7. Step 7: API keys. Most web APIs want to know which program is calling. The service gives each developer an API key, a long random string that the app sends with every request, often in a header. The key lets the service count calls, enforce limits and cut off a misbehaving app. It identifies the app, not the person using it, and must stay secret.
VALUES SO FAR
questiontemp in Osloneed
ask····contract
get····contract
request····sent
response····received
endpoint····public
inside····hidden
API key····secret
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?