{"record":{"id":"1ca57693ea5bad17","repo":"crewAIInc/crewAI","slug":"brave-api-key-environment-variable-is-required","errorCode":null,"errorMessage":"BRAVE_API_KEY environment variable is required","messagePattern":"BRAVE_API_KEY environment variable is required","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/base.py","lineNumber":128,"sourceCode":"        ]\r\n    )\r\n\r\n    def __init__(\r\n        self,\r\n        *,\r\n        api_key: str | None = None,\r\n        headers: dict[str, Any] | None = None,\r\n        requests_per_second: float = 1.0,\r\n        save_file: bool = False,\r\n        raw: bool = False,\r\n        timeout: int = 30,\r\n        **kwargs: Any,\r\n    ):\r\n        super().__init__(**kwargs)\r\n\r\n        self._api_key = api_key or os.environ.get(\"BRAVE_API_KEY\")\r\n        if not self._api_key:\r\n            raise ValueError(\"BRAVE_API_KEY environment variable is required\")\r\n\r\n        self.raw = bool(raw)\r\n        self._timeout = int(timeout)\r\n        self.save_file = bool(save_file)\r\n        self._requests_per_second = float(requests_per_second)\r\n        self._headers = self._build_and_validate_headers(headers or {})\r\n        # Per-instance rate limiting: each instance has its own clock and lock.\r\n        # Total process rate is the sum of limits of instances you create.\r\n        self._last_request_time: float = 0\r\n        self._rate_limit_lock = threading.Lock()\r\n\r\n    @property\r\n    def api_key(self) -> str | None:\r\n        return self._api_key\r\n\r\n    @property\r\n    def headers(self) -> dict[str, Any]:\r\n        return self._headers\r","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/base.py#L110-L146","documentation":"BraveSearchTool resolves its credential at construction: the api_key constructor argument, or else the BRAVE_API_KEY environment variable. If both are empty it raises ValueError immediately — the key is sent as the x-subscription-token header on every request and the API refuses anonymous calls.","triggerScenarios":"Instantiating BraveSearchTool() with no api_key while BRAVE_API_KEY is unset; env var set in a different shell/session/notebook kernel; secret file loaded after construction.","commonSituations":"Forgot to export the key; using BRAVE_SEARCH_API_KEY (wrong name); CI secrets not injected; .env not loaded before tool creation.","solutions":["Export the key: `export BRAVE_API_KEY=BSA...` from the Brave Search API dashboard.","Or pass it explicitly: BraveSearchTool(api_key='BSA...').","Verify in the running process: `python -c \"import os; print(bool(os.getenv('BRAVE_API_KEY')))\"`.","Wire the secret in CI/Docker environment configuration."],"exampleFix":"# before\ntool = BraveSearchTool()\n\n# after\ntool = BraveSearchTool(api_key=os.environ[\"BRAVE_API_KEY\"])  # fails fast with KeyError if unset","handlingStrategy":"validation","validationCode":"import os\n\nif not (os.getenv(\"BRAVE_API_KEY\")):\n    raise SystemExit(\"BRAVE_API_KEY is required; get it from https://api-dashboard.search.brave.com\")","typeGuard":null,"tryCatchPattern":"try:\n    tool = BraveSearchTool()\nexcept ValueError as e:\n    if \"BRAVE_API_KEY\" in str(e):\n        os.environ[\"BRAVE_API_KEY\"] = secret_store.get(\"brave\")\n        tool = BraveSearchTool()\n    else:\n        raise","preventionTips":["Run a startup preflight listing all required env vars (BRAVE_API_KEY, MINDS_API_KEY, APIFY_API_TOKEN, ...).","Use the exact variable name; common aliases like BRAVE_SEARCH_API_KEY silently fail this check.","Prefer passing api_key explicitly when the value comes from a secret manager."],"tags":["authentication","environment-variables","configuration","brave-search"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}