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
| Word | What it means | Example |
|---|---|---|
| Database | An organised store of data in tables. | A table of trips |
| API | A door that lets one program ask another program for data. | The trips API |
| 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. | /trips |
| JSON | A text format for data that programs read easily. | {"place":"Zoo"} |
| Permission | A 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
Quick check: the picture
Have a go
Quick check: the picture
3 questionsRequest 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
| Part | Example | What it means |
|---|---|---|
| Request | Give me all trips | The app asks for data |
| Endpoint | /trips | The address of the trips data |
| Response | [{"place":"Zoo"},{"place":"Museum"}] | The answer, written in JSON |
| Another request | Add a trip called Park | This 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 questionsAn 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 asks | May view marks | May change marks |
|---|---|---|
| A student, for their own marks | Yes | No |
| A teacher | Yes | Yes |
| A visitor who is not signed in | No | No |
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
| Part | What it means |
|---|---|
| psql | The tool that talks to the database |
| -h db | The computer that holds the database is called db |
| -U shop | Sign 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
export PGPASSWORD=shopA command for the lesson’s lab terminal. It is shown here and not run.
Extra: Make the trips table
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
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
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
curl -s -w "\n" http://api:3000/tripsA 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
| Part | What it means |
|---|---|
| -X POST | The 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/trips | The endpoint |
The cells students fill in are left blank in this preview.
Extra: Add the Museum
curl -s -X POST -H "Content-Type: application/json" -d '{"place":"Museum"}' http://api:3000/tripsA command for the lesson’s lab terminal. It is shown here and not run.
Extra: Read the trips again
curl -s -w "\n" http://api:3000/tripsA command for the lesson’s lab terminal. It is shown here and not run.
Extra: Ask for one trip
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
- 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).
- 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).
- 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).
- 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).
- 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).
- 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 questionsRemember
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.