Course contents · Lesson 3 of 17

What Is an API? The Waiter Between Apps, Explained

How Apps Are Built · Lesson 3 of 17 · 8 min Foundations

Your phone's weather app says rain is likely at 4 pm, yet your phone has no weather station. The app asked a weather service's computer for the forecast and drew the answer on screen. This lesson answers what is an API, how requests and responses work, and the rules that come with them.

What is an API?

An API (application programming interface) is a defined way for one program to ask another to do something or share data. An interface is where two things meet, and this one is for programs, unlike a user interface, the screens and buttons people use. The program that asks is the client: a phone app, a website's frontend or another server.

The API lists what may be asked and hides everything else: the code, the database and the business rules, which Frontend and Backend described as the rules of how the service works. That protects the system and lets its owners rebuild the inside freely. In the diagram, notice that the client only ever talks to the API.

Three cards in a row. The client app, the guest, is a phone app, a website's frontend or another server. The API, the waiter and the menu, holds a menu: get a forecast, get a profile, take a payment. The server and database, the kitchen, keep their code, rules and data hidden behind a lock. A request goes from the client to the API, which passes the order to the kitchen; the result comes back to the API, which sends the response to the client. An illustrative example below: the request asks for the forecast for Pune and the response says rain is likely at 4 pm. A banner says you see the menu, never the kitchen
The client sees only the menu, so the kitchen can change unnoticed.

Two corrections to the analogy. The waiter is usually not a separate machine: the API is the set of requests the server's own code agrees to answer. And it never improvises: a request that is off the menu, or worded wrongly, gets an error, not a guess, since computers do exactly what is written (lesson 1).

This course is about web APIs, which travel over HTTP (HyperText Transfer Protocol), the language of browsers and servers from lesson 2. Other APIs stay inside one computer, such as the one an app uses to ask the operating system to save a file.

Request and response: ordering from the menu

Using an API is a round trip. The client sends a request asking for one specific thing, and the server sends back a response, its answer. Each item on the menu is an endpoint: one thing you can ask for, at its own web address, or URL (uniform resource locator).

The details you add to an order are parameters, like "large, no onions": for a weather API, perhaps the city and the number of days. They can be part of the address or sent along with the request.

The response comes back in an agreed format, a fixed layout, so the client knows where to find each piece. Most web APIs use JSON (JavaScript Object Notation), the curly-brace text from lesson 2, covered in REST APIs and JSON. It holds either the data or an error saying what went wrong.

The table lines up the menu with the API words, using GitHub's real profile endpoint.

At the restaurantIn an APIIn GitHub's API
A dish on the menuEndpointA profile, at /users/ plus a username
What you tell the waiterParametersThe username, such as octocat
The dish that arrivesResponseFields such as login and public_repos
"We don't have that"ErrorNot Found for an unknown name

Where you meet APIs every day

  • Weather in a phone app. The app sends your location to a weather service's API and draws the forecast it gets back.
  • A payment page. The shop's backend asks a payment company's API to take the money, and gets back paid or declined.
  • Sign in with Google. Google checks your password on its own pages, so the site never sees it, then, with your permission, tells the site who you are.
  • A frontend and its backend. Lesson 2's "order one kettle" request was an API call; one backend API can serve a website and a phone app.

The contract: documentation others build on

An API's documentation, or docs, is its printed menu: it lists each endpoint, the parameters it takes and the responses it returns, errors included. GitHub's "Get a user" page, for example, says every profile response contains login and public_repos.

The docs are a contract, a promise to everyone who builds on the API, because their programs look for those exact names. Rename public_repos overnight and every app reading it breaks, though nobody touched those apps: a breaking change.

So owners keep the contract stable. Adding a field is usually safe, since old clients ignore what they do not look for. Breaking changes go into a new version while the old one keeps running: GitHub supports the previous version for at least 24 months after releasing a new one.

Access rules: keys, tokens and limits

Most APIs want to know who is asking. An API key is a long secret string that the API's owner issues to you, and your program sends it with every request. A token works the same way, but usually stands for one signed-in person, and often expires or allows only certain actions.

The key lets the API check who you are (authentication) and what you may do (authorization); Authentication vs Authorization goes deeper. Treat keys like passwords: anyone who copies one can act as you. Keep them on the backend, never in frontend code, which anyone can read.

A rate limit caps how many requests you may make in a period, so that no single client overloads the servers. GitHub allows 60 requests an hour without a token, counted per IP address (your connection's address on the internet), and 5,000 with a personal access token. Go over, and you get errors until the limit resets.

The flowchart shows the usual order: checks first, work second. Some APIs, like GitHub's public one, also accept requests without a key, at a lower limit.

Flowchart. The app sends a request with its key or token. In a box labelled API checks, the first question is key or token OK? If no, an error, not allowed, is sent back at once with no work done. If yes, the next question is under the limit? If no, an error, too many requests, is sent back so the app can try again later. If yes, the server does the work, reading or saving data, and the API returns the response: the data, or an error such as not found. A note says rejected requests never reach the kitchen
The checks come first, so rejected requests never reach the kitchen.

Check yourself

A weather company moves to a new database, and your weather app keeps working without an update. How?

The app depends only on the API's contract: its endpoints and the shape of its responses. The kitchen changed; the menu did not.

A developer puts a paid API key in a website's JavaScript, and the site works. What is the problem?

The browser downloads that code, so anyone can read the key and make requests as the developer, running up the bill. The key belongs on the backend.

With no token, a program asks GitHub's API for a profile every 30 seconds. After about 30 minutes it gets errors. Why?

That is 120 requests an hour against a limit of 60, so the allowance runs out halfway through the hour. It works again once the limit resets.

Go deeper