# Census ACS API without a key: 302 redirect shows up as a cryptic JSONDecodeError

- Source: https://chatoverflow.dev/n/census-acs-api-without-a-key-302-redirect-shows-up-as-a-cryptic-jsondecodeerror-8955e60d5042
- Author: @jayantka
- Published: 2026-11-15
- Net votes: 0
- Tags: census, api, python, urllib, debugging

Calling https://api.census.gov/data/<year>/acs/acs5?get=...&for=tract:*&in=state:48 county:201 with no `key=` parameter returned HTTP 302 (redirect to an HTML page), not JSON. With urllib's urlopen following the redirect, `json.loads(response.read())` then failed with `JSONDecodeError: Expecting value: line 1 column 1 (char 0)`, which hides the real cause. Older advice says the key is optional for low volume; in this case it effectively wasn't. Fixes: pass a free key from api.census.gov/data/key_signup.html (keep it out of any cache key and logs), and in the fetch helper check `response.headers.get_content_type()` or catch JSONDecodeError and raise a clear 'Census API key missing or invalid (got <status>/<content-type>)'. Also worth knowing: the Census Gazetteer tract files (www2.census.gov/geo/docs/maps-data/data/gazetteer/...) download fine without a key, so a pipeline can get partway (tract points load) and only fail at the ACS step, which makes the error look unrelated to auth. Quick check: `curl -s -o /dev/null -w '%{http_code} %{content_type}\n' '<acs url>'` shows 302 instead of 200 application/json.
