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 (1 of 4): How an API gives access to data

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.

30 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

Where does the trip list live?

A phone app shows a list of school trips. The list does not live on the phone. It lives in a database (an organised store of data) on a server (a computer that shares data with other computers).

How does the app get it? It sends a request to an API (a door that lets one program ask another for data). In this lesson you see how that door works.

Key words

WordWhat it meansExample
DatabaseAn organised store of data in tables.A table of trips
APIA door that lets one program ask another program for data.The trips API
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./trips
JSONA text format for data that programs read easily.{"place":"Zoo"}
PermissionA rule that says who may view data and who may change it.Only a teacher may change marks

The cells students fill in are left blank in this preview.

The app, the API and the database

A flowchart: "Phone app" sends a "request" to the "API"; the API "checks permission, then asks" the "Database"; the database sends "data" back to the API; the API sends a "response" to the phone app.A flowchart: "Phone app" sends a "request" to the "API"; the API "checks permission, then asks" the "Database"; the database sends "data" back to the API; the API sends a "response" to the phone app.

Quick check: the picture

Have a go

Quick check: the picture

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

Request in, response out

A request asks for something at an address. The address is an endpoint, such as /trips.

The response is the answer. It is often written in JSON, a text format that programs read easily.

A request can ask to read data. It can also ask to change data.

One request and its response

PartExampleWhat it means
RequestGive me all tripsThe app asks for data
Endpoint/tripsThe address of the trips data
Response[{"place":"Zoo"},{"place":"Museum"}]The answer, written in JSON
Another requestAdd a trip called ParkThis changes data, so permission is checked

The cells students fill in are left blank in this preview.

Quick check: read the example

Have a go

Quick check: read the example

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

An API checks permission

An API does not give data to everybody. On every request it checks permission. Permission says who may view data and who may change it.

A student may read their own marks. Only a teacher may change them. Authorisation means checking this rule on each request.

Who may do what

Who asksMay view marksMay change marks
A student, for their own marksYesNo
A teacherYesYes
A visitor who is not signed inNoNo

The cells students fill in are left blank in this preview.

Match the word to its meaning

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Extra: Lab: make your own API

Note

Now you can make a real API. Press Start the lab on the bar at the top and wait until it says it is running. Each Run button types its command into your lab terminal. The lab is private to you.

Extra: Lab step 1: make a trips table

The lab has a database called shop. You make a table called trips. psql is a tool that talks to the database directly.

The first command stores the password, so the lab does not ask for it. The second command makes the table. The table has two columns: an id that counts up by itself, and the place.

Extra: Read the psql command

PartWhat it means
psqlThe tool that talks to the database
-h dbThe computer that holds the database is called db
-U shopSign in as the user shop
-c "..."Run this one command, then stop

The cells students fill in are left blank in this preview.

Extra: Store the password

Command
export PGPASSWORD=shop

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Make the trips table

Command
psql -h db -U shop -c "create table if not exists trips (id serial primary key, place text not null)"

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Lab step 2: put a trip in it

The first command adds one row: the place Zoo. The second command tells the API to look at the database again. The API read the database when it started, before trips existed.

Extra: Add the Zoo

Command
psql -h db -U shop -c "insert into trips (place) values ('Zoo')"

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Tell the API to look again

Command
psql -h db -U shop -c "notify pgrst, 'reload schema'"

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Lab step 3: read it over the web

curl is a tool in the terminal that sends a request and prints the response. A GET request asks for data. The API turned your table into the endpoint /trips. The answer comes back as JSON. The -s part tells curl to print only the answer. The -w "\n" part ends it with a new line, so it sits on its own line.

Extra: Read the trips

Command
curl -s -w "\n" http://api:3000/trips

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Lab step 4: write, then ask for less

A POST request adds a row. A POST prints nothing when it works, so run the GET again to see the new row. Then ask for only one trip. The filter ?place=eq.Museum means: the place equals Museum.

Extra: Read the POST command

PartWhat it means
-X POSTThe request type is POST: add data
-H "Content-Type: application/json"A label that says the data is JSON
-d '{"place":"Museum"}'The data to add: the new row
http://api:3000/tripsThe endpoint

The cells students fill in are left blank in this preview.

Extra: Add the Museum

Command
curl -s -X POST -H "Content-Type: application/json" -d '{"place":"Museum"}' http://api:3000/trips

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Read the trips again

Command
curl -s -w "\n" http://api:3000/trips

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Ask for one trip

Command
curl -s -w "\n" "http://api:3000/trips?place=eq.Museum"

A command for the lesson’s lab terminal. It is shown here and not run.

Watch: how an API gives access to data

Press Play on the short film below. It starts with the school trips app. Then it goes through the app, the API and the database, one request and its response, and who may view and change marks. Listen for the myth.

Film: how an API gives access to data

How an API gives access to data

Every scene, as text

  1. Where does the trip list live? A phone app shows a list of school trips. The list lives in a database on a server (database: an organised store of data). How does the app get it? A request to an API (API: a door to data).
  2. The app, the API and the database. Phone app (sends: a request). API (does: checks permission, then asks). Database (sends: the data). API (sends: a response).
  3. Myth (false): the app reads the database directly. That is not true. The API sits in the middle and checks permission (why: everything goes through it). A person can skip the app and ask the API directly (so: the API must check, not only the app).
  4. Request in, response out. Give me all trips (request: a message that asks for something). /trips (endpoint: the address of the trips data). [{"place":"Zoo"},{"place":"Museum"}] (response: the answer, written in JSON).
  5. A request can read or change data. Give me all trips (reads: the app asks for data). Add a trip called Park (changes: data, so permission is checked).
  6. An API checks permission. A student, for their own marks (may view marks: yes; may change marks: no). A teacher (may view marks: yes; may change marks: yes). On every request (authorisation: checking it on each request).

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 data. A request goes in and a response comes out. A real API also checks permission on every request: who may view, who may change.

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.