lesson
45 min

Open Data and Public APIs

Where to find free public data, how an API request works, and how to keep keys safe.

Objective

By the end of this lesson, you can find an open dataset, request data from a public API, and store an API key safely.

Where open data lives

Start with sources that publish data on purpose, with clear licenses:

SourceWhat you find
data.govFederal datasets on health, energy, agriculture, education, and more
U.S. Census BureauPopulation, income, housing, internet access
NOAA NCEIWeather and climate records
Bureau of Labor StatisticsJobs, wages, and the cost of living
College ScorecardCollege costs, completion, and earnings by school
U.S. Energy Information AdministrationElectricity and fuel data

Kaggle also hosts many datasets. Check each one's license and original source before you use it.

What an API is

An API (application programming interface) is a way for programs to ask a server for data. You send a request to a web address. The server sends back a response, usually in JSON.

A request is often just a web address with options added after a ?:

https://api.census.gov/data/2022/acs/acs5?get=NAME,B01001_001E&for=state:*

That asks the Census API for each state's name and total population from the 2022 five-year American Community Survey. The B01001_001E code is a variable name listed in the Census API documentation.

Run it yourself

This code needs the internet, so it will not run in the browser boxes on this site. Run it in Google Colab, or install Python from python.org, save it as census.py, and run python census.py.

import os
import requests

# Keep keys out of code. Set CENSUS_KEY in your environment, or skip it for light use.
key = os.environ.get("CENSUS_KEY", "")

url = "https://api.census.gov/data/2022/acs/acs5"
params = {"get": "NAME,B01001_001E", "for": "state:*"}
if key:
    params["key"] = key

response = requests.get(url, params=params, timeout=30)
response.raise_for_status()   # stop with an error if the request failed
rows = response.json()

header, data = rows[0], rows[1:]
print(header)
print(data[:3])

Keep keys secret

An API key is like a password for a data service. Rules:

  • Never paste a key into code you share, a chatbot, a screenshot, or a public notebook.
  • Store it in an environment variable (a setting on your computer that code can read) or Colab's Secrets panel.
  • If a key leaks, cancel it and get a new one.

A national example

Use AI to read API documentation

API docs can be dense. An AI assistant can help you read them. It cannot be trusted to invent variable codes, so always check the real documentation.

Prompt Lab

Get a plain-language explanation of an API request, then check it against the official docs.

Who should the AI act as?

What exactly should it do?

What does it need to know?

What shape should the answer take?

Your prompt

Act as a patient data engineer who explains things to beginners.

Format: A short numbered list, then one line on what I should double-check in the official documentation

Do not paste private details (full name, address, ID numbers) into any AI tool.

Ask. Answer. Build together.