Using the Census API to get county FIPS codes
I came across a .csv file recently that had some data I wanted to see on a map but the .csv only had county names (formatted as 'some county (state)'). To get it on a map, I'd need to get FIPS codes for each county alongside this data.
It'd been a while since I'd done something like this. I grabbed some shapefiles from the Census but got about half-way through before I decided I wanted to see if there was a Census API I could hit for FIPS codes. Luckily, it wasn't too hard to find what I needed. Here's the URL to get FIPS codes for every county in the US:
https://api.census.gov/data/2010/dec/sf1?get=NAME&for=county:*
Note that there are no state names in that response, but there are FIPS codes for states. There's probably a way to get state name in the response, but I found it easier to go get state names and FIPS codes for each state then combine that with the counties info. Here's the URL to get info about all US states:
https://api.census.gov/data/2010/dec/sf1?get=NAME&for=state:*
With both of those responses saved to disk via a script I built a county (state)
to FIPS object to get a FIPS code next to the data in question so I could put it on a map.
Once I had a lookup, I wrote a node script to transform my .csv to a new .csv that includes FIPS code for each row. I mapped that file over on Datawrapper.