What is an API?

CS Fundamentals 2 min read
Short answer

An API is a defined set of operations one program exposes for another to call. On the web that usually means HTTP endpoints that accept and return JSON.

text
GET  /api/videos          → list videos
GET  /api/videos/12       → one video
POST /api/videos          → create one
PUT  /api/videos/12       → replace one
PATCH /api/videos/12      → update part of one
DELETE /api/videos/12     → remove one

That shape — nouns as URLs, HTTP verbs as actions — is REST.

#The pieces of a request

Method — GET, POST, PUT, PATCH, DELETE.

URL — including a query string: ?track=python&limit=10.

Headers — metadata: Content-Type, Authorization, Accept.

Body — the payload, for POST/PUT/PATCH. Usually JSON.

#Status codes worth knowing

200OK
201Created
204Success, no content to return
400Bad request — your payload was malformed
401Not authenticated
403Authenticated, but not allowed
404Not found
422Understood, but validation failed
429Rate limited — slow down
500The server broke

The 4xx/5xx split matters: 4xx means you sent something wrong, 5xx means they broke. Retrying a 400 will never help; retrying a 503 might.

#Calling one

bash
curl -H "Authorization: Bearer TOKEN" https://api.example.com/videos
python
import requests
r = requests.get(url, headers={"Authorization": f"Bearer {token}"}, timeout=10)
r.raise_for_status()
data = r.json()

Always set a timeout. Without one, a hung server hangs your program indefinitely.

#Read the rate limits

Most APIs cap requests per minute and return 429 when you exceed it, often with a Retry-After header. Respect it, and cache responses you will need again.