cenpy-devs / cenpy

Explore and download data from Census APIs
Other
184 stars 44 forks source link

HTTPError 429 Client Error When Accessing ACS #144

Closed lazarust closed 3 years ago

lazarust commented 3 years ago

I'm using cenpy version 1.0.0.post4 within a docker web container. When I run this block of code:

import cenpy
acs = cenpy.ACS(2019)
acs.from_state('KS')

I get the following error:

HTTPError: 429 Client Error: Too Many Requests for url: https://api.census.gov/data/2019/acs/acs5?get=GEO_ID,NAME&for=tract:960600&in=state:08+county:017

This seems to have just started recently and I haven't changed anything with my environment that I can think of. Any help would be greatly appreciated!

ronnie-llamado commented 3 years ago

In the Census API, there is a 500 queries per IP address per day limit without using an API key (see 'Query Limits' in the Census Data API User Guide). An API key can be requested here. Without that key, you'll get the 429 Client Error once the 500 query limit is passed.

Once you have that key, you can use cenpy to save your key to SITEKEY.txt which will make it accessible in future sessions.

Run the following once, to create SITEKEY.txt in a known location.:

import cenpy
KEY = "a4b2eab7c7050050923fffa485fb81e22be63e68"  # Census API key
cenpy.set_sitekey(KEY)

In future sessions, you shouldn't have to pass in the key and cenpy will load it accordingly.

import cenpy
acs = cenpy.ACS(2019)
print(acs._api.apikey)
> a4b2eab7c7050050923fffa485fb81e22be63e68

From there, you'll be able to avoid this error.

lazarust commented 3 years ago

Thank you so much @ronnie-llamado! I'll go ahead and close this issue then.