What Is an API — The Agreed Rules Programs Use to Exchange Data, and JSON

This article is part of the IT Foundations course, which builds up from scratch the practical IT knowledge you need at a minimum for programming and vibe coding.
Calling an API means sending a request to a set URL and receiving a response. Diagrams cover how to read JSON and status codes.

This article covers APIs.

An API is the agreed rules programs use to exchange data.

It goes through the three parts of a call and JSON, the text format that comes back.

  • What "calling an API" actually sends, and where
  • The three parts of a call — URL, method, response
  • How to read the keys and values of the JSON that comes back
  • The status code that shows success or failure, and what to do next for each number

"Calling an API" means sending a request to a set URL and receiving a response

The weather data is not held by your own program but by an external service's server.

An API called with HTTP requests and responses is a Web API, and each URL it accepts is an endpoint.

An external service can be the provider, and so can the my-app server.

And one screen does not necessarily call just one endpoint.

One screen calls three endpoints
Open theweather page/weatherCurrent temp/forecastWeekly forecast/newsNotices3 JSON responsesto one screen
To show one weather page, the browser sends three separate requests. The three JSON responses that come back are assembled into one screen.

Three request-and-response round trips happen before the page appears.

The only thing the browser connects to directly is the my-app server.

The same server is the provider from the browser's side and the caller from the weather API's side.

Calling an API means sending and receiving

Calling an API means sending a request to a set URL and receiving the response that comes back.

Each of those URLs is an endpoint; the caller can be the browser or your own server, and the provider is not necessarily an external service.

The three parts of a call — URL, method, response

Setup instructions and documentation give you only three things.

Where to send it (URL), what you want to do (method), and what comes back (response).

The first of these, the URL, splits into two roles at the ?.

What is inside one URL that calls the weather API
https://api.example.com/weather?city=tokyo&units=metric
https://api.example.com/weather
  • Before the ?. The endpoint that accepts the request
  • Change /weather to /forecast and it is a different endpoint
?city=tokyo&units=metric
  • After the ?. Write the conditions for the data you want here
city=tokyo
  • The first condition. Written as name=value
units=metric
  • The second condition. Joined to the previous one with &
The outer box is one whole URL. Before the ? is where the request goes; after the ? are the conditions for the data you want. You can join any number of conditions with &.

What follows the ? is a parameter — a condition for the data you want, added to the end of the URL as name=value — also called a query parameter.

The second part is the method — a set word such as GET or POST that says what you want the request to do.

You write it at the start of the line, and it changes the behavior even when you send to the same URL.

GET does not change the other side's data, but POST changes it the moment it is sent.

Setup instructions write this as a method followed by a URL.

Lines starting with # are explanation, and the line below each one is how to call it.

# Get the weather. The method comes first, the URL after (parameters at the end)
GET https://api.example.com/weather?city=tokyo

# Register one booking. Same URL, different method, different behavior
POST https://example.com/reservations

The document that collects how to call it and the shape of the response that comes back is the API documentation.

When instructions say "see the API documentation", that is where you look up the three parts.

You decide only two things before sending

Before sending, you decide only two things: where to send it and what you want to do.

Where to send it is the URL, with the part before the ? being the endpoint; what you want to do is the method. The third part, the response, is the one you do not decide — you read whatever comes back.

JSON is a text format that writes data as key-and-value pairs

The "JSON that came back" in the instructions is the body of the response.

JSON — a format made of text only, which writes data as key-and-value pairs — is the format most commonly used for Web API bodies.

The JSON the weather API returns looks like this.

{
  "city": "Tokyo",
  "temp": 23.5,
  "rain": false,
  "forecast": [
    { "day": "mon", "high": 25 },
    { "day": "tue", "high": 22 }
  ]
}

These are key and value pairs — the key is the name given to a value, and the value is the content that name points to.

In "temp": 23.5, temp is the key and 23.5 is the value; a key and value are joined with :, and pairs are separated with ,.

Pairs are listed inside { }, and a value can itself be a [ ] or a { }.

What is inside one JSON from the weather API
One JSON (from { to })
"city": "Tokyo"
  • The key is city, the value is the string Tokyo
"temp": 23.5
  • The key is temp, the value is the number 23.5 (not wrapped in " ")
"rain": false
  • The key is rain, the value is false (either true or false)
"forecast": [ … ]
  • The key is forecast, the value is an array
{ "day": "mon", "high": 25 }
  • The first item in the array. Its contents are again key-and-value pairs
{ "day": "tue", "high": 22 }
  • The second item in the array. It has the same keys as the first
The outer box is one JSON, from { to }. The four inside are key-and-value pairs, and the fourth value is an array whose contents are again pairs wrapped in { }.

The fourth value is an array — several values listed in order inside [ ] — and each item is again wrapped in { }.

{ } and [ ] can be nested to any depth.

To take one item out of an array you point at it by number, such as [0], and the numbering starts at 0, not 1.

Values that are not [ ] or { } are written in three ways, and you tell them apart by whether " " is used.

Telling apart the three kinds of value other than [ ] and { }
==="Tokyo"23.5falseStringNumberEither trueor falseWrapped in " "Not wrapped in " "No " ",written lowercase
The top row is the value as written in JSON, the middle row is its kind, and the bottom row is how to tell it apart. The two joined by a double line are the same value said two ways.

As received, the body is one long string, so naming a key does not get you the value.

In introductory books you first convert it into a form whose keys can be read, with a single line such as JSON.parse, and then point at a value by key name, such as data.city.

The line you wroteWhere in the JSON it looksValue you get
data.cityThe outer key cityTokyo
data.tempThe outer key temp23.5
data.forecast[0].highThe key high in the first array item [0]25
data.tmpThere is no key with that nameundefined

Only the bottom row has the key spelled wrong on purpose, by one character.

A misspelled key does not raise an error, so when you get undefined, first compare the spelling against the JSON.

The name contains JavaScript, but it is used for API responses because any language can read and write it.

JSON is just a list of name-and-content pairs

JSON is nothing more than a sequence of text listing pairs of a name and its content.

The name is the key and the content is the value; as received it is one long string, so you convert it into a form readable by key before taking values out.

A response is not always a success — the status code decides what you do

The weather API runs outside your own program, so a call can fail.

That is why a response does not arrive as a body alone.

Look at what comes back as separate parts.

What is inside one response that comes back
One response
The three-digit number at the front
  • 200 is success, 404 is failure
  • Look here first, before reading the body
The body (JSON)
  • Even from the same weather API, the contents swap between success and failure
The body on success
  • { "city": "Tokyo", "temp": 23.5 }
  • You can read the temperature from the key temp
The body on failure
  • { "error": "city not found" }
  • There is no temp key, only the reason
The outer box is one response. A three-digit number comes first, and the body follows it. What is in the body swaps over between success and failure.

The three digits at the front are the status code — a number that shows whether it succeeded or failed and of what kind.

Look at the number first, and read keys from the body only on success.

You do not memorize all three digits; the first digit decides what you do next.

The first digit and what to do next
===Starts with 2200 and othersStarts with 4404, 401, othersStarts with 5500 and othersCame back asrequestedA problem inwhat was sentA fault insidethe providerRead on fromkeys in the bodyFix the URL andconditions, resendWait a little,then call again
The top row is the first digit of the number, the middle row is what happened, and the bottom row is what the caller does. The two joined by a double line are the number and its meaning — the same thing said two ways.

Even with the same weather API, the number that comes back changes with what you sent and the state of the provider.

Besides 404, a 500 comes back when a fault occurs on the provider's side.

Send something different and both the number back and what you do next change
?city=tokyo?city=tokioMisspelled?city=tokyoProvider fault200404500temp isincludedOnly error detailsMay not evenbe JSONShow the tempShow "cannotbe retrieved"Wait a little,then call again
Each row is one call. From left to right: the parameter sent, the number that comes back, what is in the body, and what my-app does.

404 is not only the number for "no such page": with an API it also comes back when no data matches the parameter conditions.

When you call it as instructed and get a 404, compare the spelling and the parameters against the API documentation.

Among the numbers starting with 4, 401 is the one for when there is nothing showing who the caller is, or what is there is not correct.

The API key is what you attach to the request to show that.

Read the number before the body

When a response comes back, look at the number before reading the body.

If it starts with 2 the body holds the key you want; if it starts with 4, fix the URL and conditions you sent; if it starts with 5, wait a little and call again.

QUIZ

Knowledge Check

Answer each question one by one.

Q1In the instructions, what does "call the weather API" mean the program does?

Q2In the JSON fragment "temp": 23.5, what is temp?

Q3When you call the weather API and status code 404 comes back, what do you check first?