Home › Programming › APIs

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.

RequestDoes
GET /usersList users
GET /users/42Get one user
POST /usersCreate a user
PATCH /users/42Update a user
DELETE /users/42Delete 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:

Good manners

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.

Keep reading