How does a weather app know?
Your weather app shows rain for tomorrow. The weather company does not send your phone all its data.
Your phone sends one small question. The company sends back one small answer. The part that takes the question and gives the answer is an API (a door that lets one program ask another program for data).
Key words
| Word | What it means | Example |
|---|---|---|
| API | A door that lets one program ask another program for data. | A weather app asks a weather server |
| Database | An organised store of data in tables. | A table of trips |
| Platform | A kind of program that uses the data. | A phone app |
| Server | A computer that answers requests from other computers. | The weather company computer |
| Request | A message that asks an API for something. | Give me all trips |
| Response | The answer an API sends back. | A list of trips |
| Endpoint | The web address of one kind of data on an API. | weather.example.com/forecast |
| JSON | A text format for data that programs read easily. | {"place":"Zoo"} |
| Permission | Something a caller is allowed to do. | Read only |
| Authorisation | Deciding what a caller may do. | May this app add a trip? |
| Status code | A number in the response that says how the request went. | 200 means OK |
The cells students fill in are left blank in this preview.
An API sits in the middle
Quick check: what is an API?
Have a go
Quick check: what is an API?
3 questionsExtra: Read an API address, part by part
| Part | Example | What it tells you |
|---|---|---|
| Start | https:// | Talk to the server in a safe way |
| Server | weather.example.com | Which computer answers |
| Endpoint | /forecast | Which kind of data you want |
| Filter | ?city=Leeds | Only the data for Leeds |
The cells students fill in are left blank in this preview.
Extra: Quick check: read an address
Have a go
Extra: Quick check: read an address
3 questionsWhy use an API?
One database can serve many platforms: a phone app, a website and a partner company. Each one asks the same API.
The API is a safe door. Nobody touches the database directly. The API checks every request. It answers only what the caller may see.
A request can read data, such as the bus times. Or it can write data: add, change or remove it, such as booking a seat. Writing needs a stronger permission than reading.
Extra: The names of the request types
| Request type | What it does | Example | Permission needed |
|---|---|---|---|
| GET | Read data | Read the bus times | Read |
| POST | Add new data | Book a seat | Write |
| DELETE | Remove data | Cancel a booking | Write. This is often a strong permission |
The cells students fill in are left blank in this preview.
Extra: Quick check: the request types
Have a go
Extra: Quick check: the request types
3 questionsExtra: What the status code says
| Code | Meaning | Example |
|---|---|---|
| 200 | OK. Here is the data. | The bus times arrive |
| 201 | Created. The new row is added. | Your booking is saved |
| 403 | Not allowed. You have no permission. | A student tries to delete a mark |
| 404 | Not found. There is nothing at this address. | A wrong endpoint |
| 500 | The server has a fault. | The database is down |
The cells students fill in are left blank in this preview.
Who may read? Who may write?
Important
A real API checks who is calling, on every request. A public weather API lets everybody read. It lets nobody write. A school app may let a teacher write marks and a student only read them.
Try it: a pretend API
database = [{"id": 1, "place": "Zoo"}, {"id": 2, "place": "Museum"}]
permissions = {"student": ["read"], "teacher": ["read", "write"]}
def api(who, action):
# "not in" asks: is the action missing from this list?
if action not in permissions[who]:
return "Not allowed"
if action == "read":
return "OK: " + str(database)
return "OK: done"
print(api("student", "read"))
print(api("student", "write"))
print(api("teacher", "write"))
# Add "write" to the student list and run it again.
Make a free account to save your work and track your progress.
Match the API word to its job
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Your turn: read the answer of an API
An API sent back a list of trips. In Python it is a list of dictionaries. Write three functions that read it. Press Run Tests to check your work.
def place_names(rows):
# rows is the answer from an API, as a Python list of dictionaries.
# Each dictionary has "id" and "place".
# Give back a list of the place names, in the same order.
return None
def count_rows(rows):
# Give back how many dictionaries are in the list.
return -1
def find_by_place(rows, place):
# Give back the whole dictionary that has this place name.
# If there is none, give back None.
return "none"
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: why we use an API
Press Play on the short film below. It starts with the weather app. Then it shows one database serving many platforms, reading and writing, and what Not allowed means. Listen for the two myths.
Film: why we use an API
Why we use an API
Every scene, as text
- How does a weather app know? The company does not send all its data. Your phone sends one small question (request: the message sent to ask for data). /forecast (endpoint: the address of the forecast data). API (API: takes the question, gives the answer). The company sends back one small answer (response: the answer that comes back).
- One database can serve many platforms. A phone app, a website and a partner company (platforms: programs that use the data), each with an arrow to the same API (does: checks permission), and an arrow on to one database.
- Myth (false): the API is the database. That is not true. Database (keeps: all the weather data). API (sends: one small answer). Nobody touches the database directly.
- Writing needs a stronger permission. Read data (read: look at data without changing it; needs: read permission). Write data (write: add, change or remove data; needs: a stronger permission). A public weather API (may read: everybody; may write: nobody).
- Myth (false): a refusal means the server is broken. That is not true. A student asks to write (permissions: read). Not allowed (means: you have no permission). A teacher asks to write (permissions: read, write). OK: done (so: the same server works).
Final check
Have a go
Final check
5 questionsRemember
Tip
An API is a door to the data. It answers requests with responses, often in JSON. It must check permission on every request: who may read, who may write.