Your weather app has no weather
A weather app on your phone does not measure the weather. It asks a service that does.
It sends a short message: "What is the weather in Leeds?" A moment later it gets an answer. Then it draws the answer on your screen.
This way of asking is called an API (a way for one program to ask another for data).
Key words
| Word | What it means | Example |
|---|---|---|
| API | A way for one program to ask another program for data or to do a job. | a weather service |
| Endpoint | One web address of an API that gives one kind of data. | /scores |
| Request | The message your program sends to the API. | |
| Response | The answer the API sends back. | |
| HTTP method | The word in a request that says what to do: GET, POST, PUT, PATCH or DELETE. | |
| JSON | A text format for data that programs read easily. | {"player": "Aisha"} |
| Parse | Read text and turn it into data your program can use. | |
| API key | A secret code that tells the API who is asking. | |
| Status code | A number in the response that says how it went, such as 200 for OK. |
The cells students fill in are left blank in this preview.
Quick check: the words
Have a go
Quick check: the words
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Request methods
| Method | Job | Example |
|---|---|---|
| GET | Read data | Get the list of scores |
| POST | Create new data | Add a new score |
| PUT | Replace a whole item | Replace one player's record |
| PATCH | Change part of an item | Change only the points |
| DELETE | Remove data | Delete one score |
The cells students fill in are left blank in this preview.
Status codes to know
| Code | Meaning |
|---|---|
| 200 | OK. It worked |
| 201 | Created. The new item was made |
| 400 | Bad request. Your message has a mistake |
| 401 | Not allowed. Your key or sign-in is missing or wrong |
| 404 | Not found. That address does not exist |
| 500 | Server error. The fault is on the other side |
The cells students fill in are left blank in this preview.
Try it: parse JSON in Python
import json
text = '[{"player": "Aisha", "points": 12}, {"player": "Sam", "points": 30}]'
players = json.loads(text) # parse: text to a list of dictionaries
for one in players:
print(one["player"], "has", one["points"], "points")
# Add a third player to the text and run again.
Try it: display data on a page
<h2>Scores</h2>
<ul id="list"></ul>
Keep API keys secret
data-callouts.warning.label
Anyone with your key can use the service as you, and you may pay for it. Never put a key in code that goes to the browser. Never put it in a public repository (a place online where anyone can see your code). Keep it on the server, in a setting outside the code.