improve error handling around API key retrieval