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

Free lesson

API Basics 2

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab9 min read11 commands

What you will do

In API Basics you read and added notes. A web API does four things to data: create, read, update and delete, each a different kind of request. Here you do update and delete: change a note and delete one, and see the status code the API answers each with: a number saying how it went.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The first start takes a few seconds, because the database starts first. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Make sure there are notes to change

This makes the notes table if API Basics has not, puts two notes in it, and tells the API to read the database again. If you did the first lesson, the table is already there: psql says so with a NOTICE, carries on and adds two more notes. That is fine. ?order=id lists the notes in order of their id, and -w "\n" ends the answer with a new line so it sits under the command.

Command
export PGPASSWORD=shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

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

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
psql -h db -U shop -c "insert into notes (body) values ('one'), ('two')"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

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

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
curl -s -w "\n" "http://api:3000/notes?order=id"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

2. Change a note

A PATCH request changes part of a row. ?id=eq.1 says which: the row whose id equals 1. The API answers 204, which means "done, and there is nothing to send back". Read the table again and the note has changed.

Command
curl -s -o /dev/null -w "%{http_code}\n" -X PATCH -H "Content-Type: application/json" -d '{"body":"changed"}' "http://api:3000/notes?id=eq.1"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
curl -s -w "\n" "http://api:3000/notes?id=eq.1"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

3. Delete a note

A DELETE request removes the rows that match, here the one with id 2. Again 204. Read the table again and it is gone.

Command
curl -s -o /dev/null -w "%{http_code}\n" -X DELETE "http://api:3000/notes?id=eq.2"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
curl -s -w "\n" "http://api:3000/notes?order=id"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

4. Read the status codes

Every answer starts with a status code. 200 means "here it is", 201 "I made it", 204 "done", and 404 "there is no such thing". Ask for a table that does not exist and you get a 404; ask for the one that does and you get a 200.

Command
curl -s -o /dev/null -w "%{http_code}\n" http://api:3000/notes

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
curl -s -o /dev/null -w "%{http_code}\n" http://api:3000/nothing

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson uses the same database and API lab as API Basics. The database keeps its files in a volume the platform saves, so a student's notes are still there next time. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

More in this lab

About this lab and its source: PostgreSQL and PostgREST lab: a database and a REST API in the browser

Teaching a class? Set these lessons for your students with marking and a lab for every student.