Added error handling for none valid api key cast