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:
| Source | What you find |
|---|---|
| data.gov | Federal datasets on health, energy, agriculture, education, and more |
| U.S. Census Bureau | Population, income, housing, internet access |
| NOAA NCEI | Weather and climate records |
| Bureau of Labor Statistics | Jobs, wages, and the cost of living |
| College Scorecard | College costs, completion, and earnings by school |
| U.S. Energy Information Administration | Electricity 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.
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
Discussion
Ask. Answer. Build together.
Loading…