Code Samples
Ready-to-run scripts show how to paginate, filter by geography, and join datasets using official APIs.
We provide working code samples in Python, R, and JavaScript that demonstrate common data access patterns. Each sample pulls a standardized dataset—such as median household income by county—and handles pagination automatically using cursor-based continuation tokens embedded in response JSON. The examples avoid external dependencies beyond the standard library and HTTP clients, making them easy to adapt to any project. Parameters like year, dataset ID, and GEOIDs are exposed as variables at the top of each script for quick customization.
Geographic filtering is implemented using FIPS codes or nested queries. One sample downloads all tracts for a given metropolitan area by first fetching the MSA’s GEOID, then iterating over tracts with a WHERE clause on the state and county components. Another sample joins ACS 5-year estimate tables by GEOID and variable code to compute derived metrics like unemployment rate. Each step includes comments explaining the mapping to official Census Bureau documentation, helping developers verify alignment with metadata standards.
All scripts include error handling for network issues and API rate limits, with exponential backoff and retry logic for transient failures. They log request IDs for debugging and output progress to stderr. Output formats are flexible—CSV, NDJSON, or in-memory dictionaries—allowing integration into larger data pipelines. Scripts are tested quarterly against live endpoints and updated when schema changes occur, ensuring they remain valid references for developers starting new integrations.


