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

Preview

Connecting to data with an API

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

55 minutes

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

WordWhat it meansExample
APIA way for one program to ask another program for data or to do a job.a weather service
EndpointOne web address of an API that gives one kind of data./scores
RequestThe message your program sends to the API.
ResponseThe answer the API sends back.
HTTP methodThe word in a request that says what to do: GET, POST, PUT, PATCH or DELETE.
JSONA text format for data that programs read easily.{"player": "Aisha"}
ParseRead text and turn it into data your program can use.
API keyA secret code that tells the API who is asking.
Status codeA 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 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

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

Request methods

MethodJobExample
GETRead dataGet the list of scores
POSTCreate new dataAdd a new score
PUTReplace a whole itemReplace one player's record
PATCHChange part of an itemChange only the points
DELETERemove dataDelete one score

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

Status codes to know

CodeMeaning
200OK. It worked
201Created. The new item was made
400Bad request. Your message has a mistake
401Not allowed. Your key or sign-in is missing or wrong
404Not found. That address does not exist
500Server 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

Code: 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

Code: web
<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.

The lesson goes on

16 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.