Computer Science Grade 10 20 min

9. Exploring Public APIs

Explore various public APIs that provide access to different types of data (e.g., weather, news, movies).

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a Public API is and explain its role in software communication. Read basic API documentation to identify endpoints and required parameters. Construct a valid API request URL, including a base URL, endpoint, and query parameters. Make a GET request to a public API using a programming library. Parse a JSON response to extract specific pieces of data. Interpret common HTTP status codes (like 200, 404, 401) to handle the outcome of an API call. Ever wonder how your phone's weather app knows the exact temperature right now, or how a travel website shows flights from hundreds of airlines? ✈️ It's all powered by APIs! In this lesson, you'll learn how applications talk to each other using Application Programming Interfaces (APIs). We&#...
2

Key Concepts & Vocabulary

TermDefinitionExample API (Application Programming Interface)A set of rules and protocols that allows one software application to interact with and request data or services from another. It acts as a messenger between your program and a remote server.When you use a weather app, the app sends a request to a weather service's API (e.g., 'What's the temperature in New York?'), and the API sends back the data. EndpointA specific URL where an API can be accessed to perform a certain function or retrieve a specific type of data. Each endpoint corresponds to a different resource.An API might have an endpoint like `https://api.example.com/v1/users` to get a list of users and another one like `https://api.example.com/v1/products` to get a list of products. HTTP GET RequestA met...
3

Core Syntax & Patterns

API Request URL Structure Base URL + Endpoint + ? + Parameter1=Value1 & Parameter2=Value2 This is the fundamental pattern for building a URL to request specific data. The Base URL is the API's main address, the Endpoint points to the resource, and Query Parameters filter the results. The Request-Response Cycle 1. Client constructs URL -> 2. Client sends HTTP GET request -> 3. Server processes request -> 4. Server sends back HTTP status code and data (e.g., JSON) -> 5. Client parses data This sequence outlines the entire interaction with an API. Your program (the client) must follow these steps to successfully retrieve and use data from a server. Accessing Nested JSON Data data['key']['nested_key'][index] JSON data is often str...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A student wants to use the 'Open Library API' to find books by 'J.K. Rowling' and display each book's title and its first publication year. The API returns a list of book objects. Based on the tutorial, what sequence of steps must their program perform after sending the request?
A.Check for a 200 status code, parse the JSON, loop through the list of books, and for each book, print the 'title' and 'first_publish_year' keys.
B.Parse the JSON, print the entire response, then manually find the titles.
C.Loop through the raw text response, searching for the string 'title' and printing the next line.
D.Check for a 200 status code, then print the response directly to the console, as it will be pre-formatted.
Challenging
Your program attempts to connect to a new, secure API and immediately receives a 401 Unauthorized status code. The tutorial mentions not reading documentation is a pitfall. What is the most logical and efficient next step to debug this problem?
A.Try changing the endpoint URL to common alternatives like `/v2/` or `/api/`.
B.Consult the API's documentation, specifically looking for a section on 'Authentication' or 'Getting Started' to find out how to get and use an API key.
C.Assume the API is down and wait for a few hours before trying again.
D.Rewrite the JSON parsing logic, as it is likely incorrect.
Challenging
A student's code `print(data['results'][0]['location']['city'])` is causing a `KeyError`. They print the `data` object and see this JSON: `{"info": {"count": 1}, "results": [{"name": {"first": "John"}, "location": {"country": "USA"}}]}`. What is the error in their access path?
A.They should have used `data['info'][0]` instead of `data['results'][0]`.
B.The `location` object does not contain a `city` key.
C.The `results` key contains an object, not a list, so `[0]` is invalid.
D.They should have used `data('results')[0]('location')('city')`.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from IV. Interacting with APIs: Data on Demand

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.