{"record":{"id":"1d0b9399ebe6f86c","repo":"crewAIInc/crewAI","slug":"api-key-is-required-or-set-browserbase-api-key-in","errorCode":null,"errorMessage":"api_key is required (or set BROWSERBASE_API_KEY in env).","messagePattern":"api_key is required \\(or set BROWSERBASE_API_KEY in env\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/stagehand_tool/stagehand_tool.py","lineNumber":246,"sourceCode":"        self._session_id = session_id\n\n        if not self._testing:\n            log_level = {1: \"INFO\", 2: \"WARNING\", 3: \"DEBUG\"}.get(self.verbose, \"ERROR\")\n            configure_logging(\n                level=log_level, remove_logger_name=True, quiet_dependencies=True\n            )\n\n        self._check_required_credentials()\n\n    def _check_required_credentials(self) -> None:\n        \"\"\"Validate that required credentials are present.\"\"\"\n        if not self._testing and not _HAS_STAGEHAND:\n            raise ImportError(\n                \"`stagehand` package not found, please run `uv add stagehand`\"\n            )\n\n        if not self.api_key:\n            raise ValueError(\"api_key is required (or set BROWSERBASE_API_KEY in env).\")\n        if not self.project_id:\n            raise ValueError(\n                \"project_id is required (or set BROWSERBASE_PROJECT_ID in env).\"\n            )\n\n    def __del__(self) -> None:\n        \"\"\"Ensure cleanup on deletion.\"\"\"\n        try:\n            self.close()\n        except Exception:  # noqa: S110\n            pass\n\n    def _get_model_api_key(self) -> str | None:\n        \"\"\"Get the appropriate API key based on the model being used.\"\"\"\n        model_str = str(self.model_name)\n        if \"gpt\" in model_str.lower():\n            return self.model_api_key or os.getenv(\"OPENAI_API_KEY\")\n        if \"claude\" in model_str.lower() or \"anthropic\" in model_str.lower():","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/stagehand_tool/stagehand_tool.py#L228-L264","documentation":"StagehandTool requires a Browserbase API key. During construction it resolves api_key from the constructor argument or the BROWSERBASE_API_KEY environment variable; if neither yields a value it raises ValueError at init (fail-fast), before any browser session is created. Stagehand runs on Browserbase cloud browsers, so the key is mandatory.","triggerScenarios":"StagehandTool() with no api_key while BROWSERBASE_API_KEY is unset/empty; env var name typos (e.g. BROWSERBASE_APIKEY); .env file not loaded before construction; CI secrets not injected.","commonSituations":"Missing or misspelled environment variable; secrets configured in the CI UI but not exported to the job; local .env files not loaded with python-dotenv before the tool is constructed.","solutions":["Set the env var: export BROWSERBASE_API_KEY=your_key (add it to .env and load it before constructing the tool).","Or pass it explicitly: StagehandTool(api_key=\"...\", project_id=\"...\").","Verify the variable is actually visible in the process (print(os.environ.get('BROWSERBASE_API_KEY')) is None-check) in CI."],"exampleFix":"# before\ntool = StagehandTool()  # ValueError: api_key is required\n\n# after\nimport os\ntool = StagehandTool(\n    api_key=os.environ[\"BROWSERBASE_API_KEY\"],\n    project_id=os.environ[\"BROWSERBASE_PROJECT_ID\"],\n)","handlingStrategy":"validation","validationCode":"import os\napi_key = os.environ.get(\"BROWSERBASE_API_KEY\") or provided_api_key\nif not api_key:\n    raise ValueError(\"Set BROWSERBASE_API_KEY or pass api_key= to StagehandTool\")","typeGuard":"def has_browserbase_api_key(api_key: str | None) -> bool:\n    return bool(api_key or os.environ.get(\"BROWSERBASE_API_KEY\"))","tryCatchPattern":"try:\n    tool = StagehandTool()\nexcept ValueError as e:\n    if \"api_key is required\" in str(e):\n        raise RuntimeError(\"Missing BROWSERBASE_API_KEY; add it to .env / CI secrets\") from e\n    raise","preventionTips":["Load .env (python-dotenv) before constructing the tool.","Add BROWSERBASE_API_KEY to CI/Docker secrets and fail startup fast when it is absent."],"tags":["stagehand","browserbase","credentials","environment"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}