# What a Web API Is

A web API is a program on a server that answers requests sent to its web addresses, and every request and answer has the same parts, a URL with a domain, a path and a query string, a method, a status code and, usually, JSON that Python reads into dictionaries and lists.

Apprentice · Python · REST API · Web Development · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Name the parts of a web API request, the domain, path and query string of its URL and its method, and say what each one does
- Read a web API's answer, what its status code says and where a value sits in its JSON once Python has read it

## Key takeaways

- A web API is a program on a server that answers requests sent to its web addresses, like an order window with a fixed menu.
- A URL names the server (the domain), what you want (the path) and any options (the query string, after the question mark).
- The method says what you want done (GET reads, POST sends something new), and the status code says how it went (200 worked, 404 not found, 500 the server broke).
- Most web APIs answer in JSON. Python reads it into dictionaries and lists, and you find a value by following its keys from the outside in.

The forecast in your phone's weather app, live game scores, the map on a delivery order: none of that data lives in the app you're looking at. The app asks another computer for it, over the internet. That computer is a **web API**, and a Python program can ask one too: a script that checks the forecast before a trip, a report that pulls in today's numbers, a class project that "must use a public API".

This chapter plans a weekend at Pine Ridge Campground: the forecast, the chance of rain, and what to pack. The forecast comes from the US National Weather Service's API, which is free, needs no key, and whose data anyone may use. (Pine Ridge is made up; its coordinates are a real state park's in Oregon, so the forecast is real.)

## An order window for data

Picture a takeout window. A menu on the wall says what you can ask for, you order in a set way ("number 4, no onions"), and you get your food or "sorry, we're out of that." A web API is an order window for data: the menu is its documentation, your order is a **request** your program sends to an address on the internet, and the bag is the **response**.

**API** stands for *application programming interface*: the rules one program uses to ask another for something. The program that asks is the **client**, and the computer that answers is the **server**; your textbook may call this the *client-server model*.

## The address: a URL and its query string

Every request goes to a **URL** (read "U-R-L", for *uniform resource locator*), the kind of address you type into a browser. This one asks about the campground's spot on the map:

```text
https://api.weather.gov/points/44.8783,-122.6534
└──────┘└─────────────┘└───────────────────────┘
scheme  domain         path
```

The scheme says how to talk to the server (the `s` means encrypted), the domain which server to ask, and the path what you're asking for, like the item on the menu. Each address an API offers is an **endpoint**. Part of this path is a value you choose, a **path parameter**: the campground's latitude and longitude, `44.8783,-122.6534`.

"No onions" isn't a different menu item; it's an option on one. A URL carries options in its **query string**: everything after a `?` (read "question mark"), each option a `key=value` pair, with `&` (read "and") between two options:

```text
https://api.weather.gov/gridpoints/PQR/107,74/forecast?units=si
```

With `units=si`, the forecast comes back in Celsius instead of Fahrenheit. The path is the same; the query string changes what comes back.

## The answer: JSON

**Try it.** Paste the points address into your browser's address bar. Instead of a web page, you get text. Shortened, it looks like this:

```json
{
  "properties": {
    "gridId": "PQR",
    "gridX": 107,
    "gridY": 74,
    "forecast": "https://api.weather.gov/gridpoints/PQR/107,74/forecast"
  }
}
```

That's **JSON**, the text format *JSON Files: Saving Your Data* uses for files. It names the forecast office for the spot (`PQR`) and the square of its map that holds it (107, 74), which the Weather Service redraws now and then. Open the `forecast` address, and its `properties` hold a list of `periods`, one for each half day of the week ahead.

To find a value, follow the keys from the outside in. Once `json.loads()` has turned the text into dictionaries and lists, each key is a pair of square brackets and each list position a number:

```python
import json

text = """
{
  "properties": {
    "periods": [
      {"name": "Saturday", "temperature": 43, "probabilityOfPrecipitation": {"value": 74}},
      {"name": "Saturday Night", "temperature": 38, "probabilityOfPrecipitation": {"value": 74}}
    ]
  }
}
"""
data = json.loads(text)
saturday = data["properties"]["periods"][0]

print(saturday["name"], saturday["temperature"])
print(saturday["probabilityOfPrecipitation"]["value"])
print(data["properties"]["periods"][1]["name"])
```

```text
Saturday 43
74
Saturday Night
```

`probabilityOfPrecipitation` is the Weather Service's name for the chance of rain or snow, in percent.

## The method and the status code

Every request carries a **method**, a verb that says what the client wants. The language of web requests is **HTTP** (*hypertext transfer protocol*), so these are **HTTP methods**; `PUT`, `PATCH` and `DELETE` change and remove things the same way:

| Method | It asks the server to | At the campground |
|---|---|---|
| `GET` (read "get") | send data back, changing nothing | show the weekend's forecast |
| `POST` (read "post") | save something new | book a campsite |

A browser's address bar sends a `GET`, and the Weather Service only answers `GET`: it shares forecasts and takes nothing in.

Every response starts with a **status code**, a three-digit number that says how the request went. The first digit tells you the kind of answer:

| Code | Name | What it means |
|---|---|---|
| `200` | OK | It worked; codes from 200 to 299 mean success. |
| `404` | Not Found | Nothing is at that address; codes from 400 to 499 mean the request had a problem. |
| `500` | Internal Server Error | Something broke on the server; codes from 500 to 599 mean the server had a problem, not you. |

**Try it.** Open `https://api.weather.gov/points/0,0`, a spot in the ocean the Weather Service doesn't forecast. The status is `404`, and the JSON's `title` says what went wrong: `Data Unavailable For Requested Point`.

## Do it: read the forecast's answer for the trip

The trip's packing helper reads each period's chance of rain out of the forecast, and says from a status code how a request went.

- `forecast`: the forecast's answer as Python read it. Its `"properties"` hold a list of `"periods"`, each a dictionary with a `"name"` and a `"probabilityOfPrecipitation"` whose `"value"` is the chance of rain.
- `name`: one period's name, such as `"Saturday Night"`.
- `status`: a status code, such as `200`.

**Your job:** write two functions.

- `rain_chance(forecast, name)` returns the chance of rain (the `"value"`) of the period whose `"name"` is `name`.
- When no period has that name, `rain_chance` returns `None`.
- `how_it_went(status)` returns `"It worked"` for a code from 200 to 299, `"The request had a problem"` for one from 400 to 499, and `"The server had a problem"` for one from 500 to 599.

Worked through: in the starter's `forecast`, the period named `"Saturday"` has `"probabilityOfPrecipitation": {"value": 74}`, so `rain_chance(forecast, "Saturday")` returns `74`. And `how_it_went(404)` returns `"The request had a problem"`, because 404 is between 400 and 499.

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

A URL to ask, a method for what to do, a status code for how it went and JSON for the answer: that's the same for a weather service, a payment company and your own server, so once you can read one API, you can read them all.

## Key terms

- [Web API](https://www.ask-codey.com/glossary/web-api)
- [URL](https://www.ask-codey.com/glossary/url)
- [Query String](https://www.ask-codey.com/glossary/query-string)
- [HTTP Method](https://www.ask-codey.com/glossary/http-method)
- [Status Code](https://www.ask-codey.com/glossary/status-code)
- [JSON](https://www.ask-codey.com/glossary/json)

## Related

- [JSON Files: Saving Your Data](https://www.ask-codey.com/lessons/python-files-json)
- [Getting Data with requests](https://www.ask-codey.com/lessons/python-requests-getting-json)

Source: https://www.ask-codey.com/lessons/python-web-apis-what-is-an-api
