How APIs work · Demo 4 of 4
A tiny API of your own
So far the browser only asked for data. An API can also change data. Add, edit and delete team members, and read the log: every click is one request with a verb that says what should happen.
1 The interface: the team
- Loading…
Try adding someone with no name. The page does not stop you; the server does.
2 The request log: newest first
The four verbs
- GETRead. “Give me the team.” Changes nothing.
- POSTCreate. “Add this person.” The server picks the id.
- PUTUpdate. “Change person 2 to this.”
- DELETERemove. “Delete person 2.” Answers 204: done, nothing to send back.
What to notice
- The address names the thing (
/api/team/2). The verb names the action. - POST and PUT carry a body: the data being sent to the server.
- The data lives on the server. Open this page in a second tab and reload the list: same team.
- The server checks what it receives. A bad request gets 400 and a reason.
The whole API is one short file. Read server.js ↗
Explain it back
- Which requests had a body? Which did not? Why?
- Who decided the new person's id: the page or the server?
- What came back when you added someone with no name?
- Design the confirmation for “Delete”. What happens if the server says 404?