What an API is

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

What an API is

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does API stand for?
A Application Programming Interface
B Automated Program Instruction
2. What is the main job of an API?
A Makes a program run faster on any computer
B Lets one program use the features or data of another program
3. Which is the correct order for an API interaction?
A Your program sends a request, then the API sends back a response
B The API sends a response, then your program sends a request
4. What is an endpoint?
A The last line of code in a program
B A specific URL where an API receives requests
5. Why is an API described as a contract?
A It defines which requests are allowed and what responses will be returned
B It lets the caller change the other program's source code

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1API AAn endpoint: a specific address that a program sends a request to in order to use one feature or get one set of data
2https://api.example.com/weather Bresponse = requests.get(url)
3A request and response: your program asks an API for data, and the API sends an answer back CThe endpoint is not a full address. An endpoint is a specific URL, such as https://api.example.com/weather, so the request cannot reach the API.
4An API is a contract: it states what you must send and what you will get back, so both programs can rely on it DGET /weather?city=Melbourne returns JSON
5An API lets you open the other program's source code and change how it works EAn API does not give access to the other program's internal code. It only exposes defined requests and responses that you can use.
6requests.get("weather") # sends a request to the endpoint FAn application programming interface: a defined set of requests and responses that lets one program use the features or data of another

Definitions

API

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import ________
2.
url = "https://api.example.com/weather" response = requests.get(___)
3.
response = requests.___(url)
4.
if response.___________ == 200: print("Request succeeded")
5.
response = requests.get(url) data = response.____()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import requests
B url = "https://x.io/users"
C response = requests.fetch(url)
D print(response.status_code)
Question 2
A import request
B endpoint = "https://x.io/pets"
C reply = requests.get(endpoint)
D print(reply.status_code)
Question 3
A response = requests.get[url]
B if response.status_code == 200:
C print("Request worked")
Question 4
A import requests
B url = "https://x.io/weather
C reply = requests.get(url)
D print(reply.text)
Question 5
A import requests
B url = "https://x.io/pets"
C reply = requests.get(url)
D data = reply.json
E print(data["name"])

Answer Key

What an API is
Section A: Quick Questions
Q1
A — Application Programming Interface
Q2
B — Lets one program use the features or data of another program
Q3
A — Your program sends a request, then the API sends back a response
Q4
B — A specific URL where an API receives requests
Q5
A — It defines which requests are allowed and what responses will be returned
Section B: Match the Pairs
Q1
API → An application programming interface: a defined set of requests and responses that lets one program use the features or data of another
Q2
https://api.example.com/weather → An endpoint: a specific address that a program sends a request to in order to use one feature or get one set of data
Q3
A request and response: your program asks an API for data, and the API sends an answer back → response = requests.get(url)
Q4
An API is a contract: it states what you must send and what you will get back, so both programs can rely on it → GET /weather?city=Melbourne returns JSON
Q5
An API lets you open the other program's source code and change how it works → An API does not give access to the other program's internal code. It only exposes defined requests and responses that you can use.
Q6
requests.get("weather") # sends a request to the endpoint → The endpoint is not a full address. An endpoint is a specific URL, such as https://api.example.com/weather, so the request cannot reach the API.
Section C: Definitions
Q1
API — An application programming interface: a defined set of requests and responses that lets one program use the features or data of another program.
Section D: Fill in the Blanks
Q1
import requests
Q2
url = "https://api.example.com/weather" response = requests.get(url)
Q3
response = requests.get(url)
Q4
if response.status_code == 200: print("Request succeeded")
Q5
response = requests.get(url) data = response.json()
Section E: Spot the Error
Q1
C — The requests library has no fetch() function. A request to an API endpoint is made with requests.get(url), so the line should be response = requests.get(url).
Q2
A — The module is called requests, with an s. Importing request causes a ModuleNotFoundError, so the program cannot send its request to the endpoint. It should be import requests.
Q3
A — get is a function, so the endpoint URL must be passed in round brackets. Square brackets are for indexing. It should be response = requests.get(url).
Q4
B — The string holding the endpoint URL is missing its closing quote, which is a syntax error. It should be url = "https://x.io/weather".
Q5
D — json is a method, so it needs brackets to run. Without them data holds the method itself and data["name"] fails. It should be data = reply.json().