APIs
An API is how one program talks to another. On the web that usually means sending HTTP requests and getting JSON back.
API stands for Application Programming Interface — a defined set of operations one program exposes for another to use. A web API is just that, delivered over HTTP: you send a request to a URL and get structured data (usually JSON) back.
REST, the common style
Most web APIs follow a REST-ish convention: resources live at URLs, and HTTP methods act on them.
| Request | Does |
|---|---|
GET /users | List users |
GET /users/42 | Get one user |
POST /users | Create a user |
PATCH /users/42 | Update a user |
DELETE /users/42 | Delete a user |
Calling one
const res = await fetch("https://api.example.com/users/42", {
headers: { "Authorization": "Bearer YOUR_TOKEN" }
});
const user = await res.json();
console.log(user.name);
Always check res.ok (or the status code) before trusting the body — see the HTTP tutorial for what the codes mean.
Sending data
await fetch("https://api.example.com/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ada" })
});
Authentication
Most APIs need to know who you are. Common schemes:
- API keys — a secret string, often in a header.
- Bearer tokens —
Authorization: Bearer ..., frequently a JWT. - OAuth — a flow that lets users grant limited access without sharing their password.
Good manners
- Respect rate limits — a
429means slow down. - Version your API (
/v1/...) so you can change it without breaking callers. - Return useful status codes and error messages.
Never put secrets in front-end code. An API key shipped in browser JavaScript is visible to anyone. Keep it on a server and proxy the request.