Hi,
I’m a first-time user of IPUMS API and I keep getting an “Invalid API key” error while making a request.
I double-checked the key I’m using and also revoked an existing one and created a new one but the issue persists.
Hi,
I’m a first-time user of IPUMS API and I keep getting an “Invalid API key” error while making a request.
I double-checked the key I’m using and also revoked an existing one and created a new one but the issue persists.
Please share your code so that we can help you debug, thanks!
I’m glad that we were able to connect and troubleshoot the issue with your code. I’m sharing the resources that I mentioned in our conversation to help those using Python to submit their API calls better navigate this issue.
We provide a language-specific library in the form of ipumspy that allows Python users to simplify their API calls. Our Getting Started guide to ipumspy begins by explaining how to initialize an API client using your API key before discussing how to create an extract definition. While discussing how to get an API key, the guide notes that we recommend storing your key in an environment variable in order to prevent it from being shared. The example code on the page therefore assumes that the API key is stored in an environment variable called IPUMS_API_KEY. If you have done so, then the IPUMS_API_KEY in:
IPUMS_API_KEY = os.environ.get(“IPUMS_API_KEY”)
will be the string “IPUMS_API_KEY” that is saved as an environmental variable, not to be replaced with your actual key. If you decide not to hard-code your key, then this line can be replaced with:
IPUMS_API_KEY = “REPLACE-WITH-YOUR-KEY”
We encourage ipumspy users who have reviewed our documentation and who are still having trouble to share their code so that we can help better troubleshoot their issue.
Hi! Can I ask what the solution was here? I’m having the same issue.
Once you’ve obtained your API key, you should store it in an environment variable. See these instructions for saving environment variables in conda environments for different operating systems. The sample code provided on our Getting Started with ipumspy page assumes that the API key is stored in an environment variable called IPUMS_API_KEY. Running the sample code without doing this will result in an error. You can alternatively directly input your API key by replacing
IPUMS_API_KEY = os.environ.get(“IPUMS_API_KEY”)
with
IPUMS_API_KEY = “REPLACE_WITH_YOUR_ACTUAL_KEY”