APIs Explained Simply

What an API is, and what comes back when you ask

Advanced 9 min

In this lesson

An API is an agreement between two programs: one says what it can be asked for, the other asks. Instead of a web page built for human eyes, you get back plain data your code can use. This lesson covers the vocabulary, endpoints, requests, responses, JSON, and status codes, so the next lesson’s code has something to hang on.

Explain it like I’m 5

An API is a menu for software. It lists what you can order and what turns up on the plate. You don’t get to see the kitchen.

An API is an agreement, not a website

When you visit a website, a server sends HTTP back as HTML: headings, styling, layout, everything a browser needs to draw a page. An API answers the same kind of request with data only, because the thing asking is a program, not a person.

That difference is the whole point. Scraping a web page means digging your data out of presentation you didn’t ask for and that changes whenever a designer touches it. An API hands you the data directly, in a shape the provider has promised to keep stable.

Endpoints, requests, and responses

An endpoint is one URL that does one job. A weather service might expose /current for right now and /forecast for the week ahead. You send a request to an endpoint; the server sends back a response.

A request carries more than a URL. It has a method saying what you want done (GET to read, POST to create, and others you’ll meet in a later lesson), optional query parameters narrowing the question, and headers carrying metadata such as who you are.

Example
https://api.example.com/search?q=python&page=2

  https://api.example.com   the service
  /search                   the endpoint
  ?q=python                 a query parameter
  &page=2                   another query parameter
One endpoint URL, pulled apart.

JSON is what usually comes back

Most APIs answer in JSON. It looks almost exactly like a mix of Python dictionaries and lists, which is why it feels so natural to work with once it reaches your code: objects become dicts, arrays become lists, and the rest are strings, numbers, booleans, and nulls.

Real responses are usually nested. A value can be another object, or a list of objects. Reading them is just repeated indexing, one layer at a time.

Example
{
  "name": "London",
  "main": { "temp": 12.5, "humidity": 81 },
  "weather": [ { "description": "light rain" } ]
}
A trimmed-down weather response. Note the nesting.

Below is a parsed API response. Print the city name, the temperature, and the weather description, one per line. Watch the nesting.

response = {
    "name": "London",
    "main": {"temp": 12.5, "humidity": 81},
    "weather": [{"description": "light rain"}],
}

# TODO: print the city, then the temperature, then the description
print(response["name"])

Status codes tell you what happened

Every response carries a three-digit status code. You do not need to memorize them, only the families:

  • 2xx, it worked. 200 OK is the everyday success.
  • 4xx, you got it wrong. 404 means no such thing; 401 and 403 mean you are not allowed; 429 means you asked too often.
  • 5xx, they got it wrong. The server broke. Retrying later is reasonable.

The distinction that matters: 4xx is your bug, 5xx is theirs. Checking the code before you touch the body is what separates a script that fails clearly from one that crashes on confusing nonsense.

Common mistake: Expecting an API to return a web page

Why it happens:

Both arrive over the same protocol from the same kind of URL, so it’s easy to assume they’re the same thing.

How to fix it:

Read the documentation for the response shape. An API endpoint returns data, normally JSON, not HTML. If you got HTML back, you probably called the website rather than the API.

Common mistake: Treating a JSON response as a string to pick apart

Why it happens:

It arrives as text, so slicing and searching it looks like the obvious move.

How to fix it:

Parse it once into Python objects, then use normal dict and list access. Never hand-parse JSON with string methods, it breaks the moment a field moves or a value contains a comma.

Common mistake: Ignoring the status code

Why it happens:

The happy path works while you’re building, so the check feels like paperwork.

How to fix it:

Check the status before reading the body. A 404 or 500 body will not contain your data, and the error you get later will not point at the real cause.

What does an API return, compared with a normal web page?

A response comes back with status 404. What does that tell you?

Given the response {"main": {"temp": 12.5}}, how do you read the temperature in Python?

Mini exercise (easy)

Write summarize(response) that takes a parsed weather response and returns a single line like London: 12.5C, light rain. Reach into the nested main dict and the weather list.

Now you. Edit the starter code below, then Run it, everything happens in the browser.

response = {
    "name": "Paris",
    "main": {"temp": 18.0, "humidity": 55},
    "weather": [{"description": "clear sky"}],
}

def summarize(response):
    return ""  # TODO: "Paris: 18.0C, clear sky"

print(summarize(response))

What to learn next

You now have the vocabulary: an API is an agreement, an endpoint is one URL doing one job, a request carries a method plus parameters and headers, and the response brings back JSON along with a status code. You practiced reaching into a nested response, and you know that 4xx means your mistake while 5xx means theirs.

Time to make a real call. Calling Web APIs with Requests covers the whole round trip: parameters, timeouts, error handling, and walking paginated results.