{"record":{"id":"e09166d2adf4075e","repo":"assafelovic/gpt-researcher","slug":"bing-api-key-not-found-please-set-the-bing-api-ke","errorCode":null,"errorMessage":"Bing API key not found. Please set the BING_API_KEY environment variable.","messagePattern":"Bing API key not found\\. Please set the BING_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"gpt_researcher/retrievers/bing/bing.py","lineNumber":35,"sourceCode":"        Initializes the BingSearch object\n        Args:\n            query:\n        \"\"\"\n        self.query = query\n        self.query_domains = query_domains or None\n        self.api_key = self.get_api_key()\n        self.logger = logging.getLogger(__name__)\n\n    def get_api_key(self):\n        \"\"\"\n        Gets the Bing API key\n        Returns:\n\n        \"\"\"\n        try:\n            api_key = os.environ[\"BING_API_KEY\"]\n        except Exception:\n            raise Exception(\n                \"Bing API key not found. Please set the BING_API_KEY environment variable.\")\n        return api_key\n\n    def search(self, max_results=7) -> list[dict[str]]:\n        \"\"\"\n        Searches the query\n        Returns:\n\n        \"\"\"\n        print(\"Searching with query {0}...\".format(self.query))\n        \"\"\"Useful for general internet search queries using the Bing API.\"\"\"\n\n        # Search the query\n        url = \"https://api.bing.microsoft.com/v7.0/search\"\n\n        headers = {\n            'Ocp-Apim-Subscription-Key': self.api_key,\n            'Content-Type': 'application/json'","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/retrievers/bing/bing.py#L17-L53","documentation":"Exception raised by the Bing retriever when the BING_API_KEY environment variable is missing (os.environ lookup fails and the except re-raises with this message). The retriever cannot authenticate to the Bing Search API without it.","triggerScenarios":"Instantiating the Bing retriever (which calls get_api_key() from __init__) in a process where BING_API_KEY is unset or empty-not-present in os.environ.","commonSituations":"Forgot to export the var, .env file not loaded, key set in a different shell/CI environment, or Docker/Kubernetes secrets not wired to the container.","solutions":["export BING_API_KEY=<your key> (get one from Azure portal / Bing Search).","Add BING_API_KEY to your .env and ensure it's loaded before the retriever is constructed.","In Docker/CI, pass it via -e / secrets so it exists in the container environment.","Optionally validate env vars at startup before research begins."],"exampleFix":"# before\nretriever = Bing(query=\"...\")  # raises\n\n# after\nimport os\nassert os.environ.get(\"BING_API_KEY\"), \"Set BING_API_KEY first\"\nretriever = Bing(query=\"...\")","handlingStrategy":"validation","validationCode":"import os\nif not os.environ.get(\"BING_API_KEY\"):\n    raise SystemExit(\"BING_API_KEY missing — get one from Azure portal\")","typeGuard":"null","tryCatchPattern":"try:\n    retriever = Bing(query)\nexcept Exception as e:\n    if \"BING_API_KEY\" in str(e):\n        logger.error(e); sys.exit(1)\n    raise","preventionTips":["Validate required env vars at app startup, not at retriever construction.","Use a centralized .env loader (dotenv) in the entrypoint.","List required env vars in deployment docs/CI templates."],"tags":["bing","missing-env-var","api-key","retriever"],"backgroundTag":"missing-env-var","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}