Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

6.11 (2 of 4): Why we use an API

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

40 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
APIA door that lets one program ask another program for data.A weather app asks a weather server
DatabaseAn organised store of data in tables.A table of trips
PlatformA kind of program that uses the data.A phone app
ServerA computer that answers requests from other computers.The weather company computer
RequestA message that asks an API for something.Give me all trips
ResponseThe answer an API sends back.A list of trips
EndpointThe web address of one kind of data on an API.weather.example.com/forecast
JSONA text format for data that programs read easily.{"place":"Zoo"}
PermissionSomething a caller is allowed to do.Read only
AuthorisationDeciding what a caller may do.May this app add a trip?
Status codeA 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

A flowchart: the "Phone app" and the "Website" each send a "request" to the "API"; the API "checks permission" on the way to the "Database"; the database sends "data" back to the API; the API sends a "response in JSON" to the phone app.A flowchart: the "Phone app" and the "Website" each send a "request" to the "API"; the API "checks permission" on the way to the "Database"; the database sends "data" back to the API; the API sends a "response in JSON" to the phone app.

Quick check: what is an API?

Have a go

Quick check: what is an API?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Extra: Read an API address, part by part

PartExampleWhat it tells you
Starthttps://Talk to the server in a safe way
Serverweather.example.comWhich computer answers
Endpoint/forecastWhich kind of data you want
Filter?city=LeedsOnly 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Why 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 typeWhat it doesExamplePermission needed
GETRead dataRead the bus timesRead
POSTAdd new dataBook a seatWrite
DELETERemove dataCancel a bookingWrite. 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Extra: What the status code says

CodeMeaningExample
200OK. Here is the data.The bus times arrive
201Created. The new row is added.Your booking is saved
403Not allowed. You have no permission.A student tries to delete a mark
404Not found. There is nothing at this address.A wrong endpoint
500The 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

Code: python
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.

Code: python
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

  1. 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).
  2. 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.
  3. 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.
  4. 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).
  5. 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.