What Is an API? The Waiter Between Apps, Explained
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.
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 restaurant | In an API | In GitHub's API |
|---|---|---|
| A dish on the menu | Endpoint | A profile, at /users/ plus a username |
| What you tell the waiter | Parameters | The username, such as octocat |
| The dish that arrives | Response | Fields such as login and public_repos |
| "We don't have that" | Error | Not 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.
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
- System Design – A feature-rich Rate Limiter system: how a service counts requests per IP address or API key and turns away the excess.